feat: implemented player jump

This commit is contained in:
Sara 2024-01-15 20:33:09 +01:00
parent 559c86ddaf
commit 96acaa0a24
4 changed files with 71 additions and 2 deletions

View file

@ -45,6 +45,9 @@ void Internal_PlayerInitInput(Player* self) {
playerinput_add(self->playerInput,
CompositeAxis1D_as_InputAxis(compositeaxis1d_from_keys(SDL_SCANCODE_S, SDL_SCANCODE_W)),
(InputDelegateFn)PlayerVerticalInput);
playerinput_add(self->playerInput,
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_SPACE)),
(InputDelegateFn)PlayerJumpInput);
playerinput_add(self->playerInput,
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_J)),
(InputDelegateFn)PlayerLightAttackInput);
@ -59,11 +62,14 @@ Player* MakePlayer() {
*self = (Player) {
.transform = IdentityTransform,
.height = 0.f,
.rigidbody = NULL,
.physicsCollider = NULL,
.hitbox = NULL,
.verticalVelocity = 0.f,
.playerInput = playerinput_new(self, -1),
.moveInput = ZeroVector,
.attackInput = 0,
@ -75,6 +81,7 @@ Player* MakePlayer() {
.idle = NULL,
.walk = NULL,
.jump = NULL,
.jab_a = NULL,
.jab_b = NULL,
.kick_a = NULL,
@ -100,6 +107,7 @@ Player* MakePlayer() {
self->idle = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Idle.png", IVectorFrom(512)), 1.5f, LoopMode_Loop);
self->walk = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Walk.png", IVectorFrom(512)), 5.f, LoopMode_Loop);
self->jump = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Jumping.png", IVectorFrom(512)), 1.f, LoopMode_Stop);
self->jab_a = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Jab_A.png", IVectorFrom(512)), 10.f, LoopMode_Stop);
self->jab_b = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Jab_B.png", IVectorFrom(512)), 10.f, LoopMode_Stop);
self->kick_a = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Kick_A.png", IVectorFrom(512)), 10.f, LoopMode_Stop);
@ -142,10 +150,15 @@ void PlayerStart(Player* self) {
void PlayerUpdate(Player* self, float deltaTime) {
state_machine_update(self->animationStateMachine, deltaTime);
self->height += self->verticalVelocity * deltaTime;
if(self->height <= 0.f)
self->height = 0.f;
}
void PlayerDraw(Player* self) {
animation_sprite_draw(self->currentAnimation, &self->transform);
Transform trans = self->transform;
trans.position.y -= self->height;
animation_sprite_draw(self->currentAnimation, &trans);
}
void PlayerHorizontalInput(Player* self, InputEvent value) {
@ -156,6 +169,10 @@ void PlayerVerticalInput(Player* self, InputEvent value) {
self->moveInput.y = -value.as_float;
}
void PlayerJumpInput(Player* self, InputEvent value) {
self->jumpInput = value.as_bool;
}
void PlayerLightAttackInput(Player* self, InputEvent value) {
if(value.as_bool)
self->attackInput = 1;

View file

@ -16,24 +16,28 @@ extern const Vector PLAYER_SPEED;
typedef struct Player {
Transform transform;
float height;
RigidBody* rigidbody;
Collider* physicsCollider;
Collider* hitbox;
float verticalVelocity;
PlayerInput* playerInput;
Vector moveInput;
int attackInput;
int jumpInput;
size_t animationTriggers;
int facing;
Sprite* sprite;
Transform sprite_local_transform;
AnimationSprite* idle;
AnimationSprite* walk;
AnimationSprite* jump;
AnimationSprite* jab_a;
AnimationSprite* jab_b;
AnimationSprite* kick_a;
@ -53,6 +57,7 @@ void PlayerDraw(Player* self);
void PlayerHorizontalInput(Player* self, InputEvent value);
void PlayerVerticalInput(Player* self, InputEvent value);
void PlayerJumpInput(Player* self, InputEvent value);
void PlayerLightAttackInput(Player* self, InputEvent value);
void PlayerHeavyAttackInput(Player* self, InputEvent value);

View file

@ -19,6 +19,7 @@ void PlayerAnimationExit(Player* self) {
void PlayerIdleEnter(Player* self) {
self->currentAnimation = self->idle;
rigidbody_set_velocity(self->rigidbody, ZeroVector);
animation_sprite_play_from(self->currentAnimation, 0.f);
}
@ -29,6 +30,8 @@ const State* PlayerIdleUpdate(Player* self, float deltaTime) {
return PlayerJabA();
if(self->attackInput == 2)
return PlayerKickA();
if(self->jumpInput)
return PlayerJump();
return PlayerIdle();
}
@ -46,6 +49,8 @@ const State* PlayerWalk_Update(Player* self, float deltaTime) {
return PlayerJabA();
if(self->attackInput == 2)
return PlayerKickA();
if(self->jumpInput)
return PlayerJump();
InternalSpriteFlipWithMovement(self);
return PlayerWalk();
}
@ -156,3 +161,27 @@ const State* PlayerKickA_Update(Player* self, float deltaTime) {
return PlayerIdle();
return PlayerKickA();
}
void PlayerJump_Enter(Player* self) {
self->currentAnimation = self->jump;
self->verticalVelocity = 2.f;
animation_sprite_play_from(self->currentAnimation, 0.f);
}
const State* PlayerJump_Update(Player* self, float deltaTime) {
self->verticalVelocity += .3f * deltaTime;
if(self->verticalVelocity > 0.f)
return PlayerFall();
return PlayerJump();
}
void PlayerFall_Enter(Player* self) {
self->currentAnimation = self->jump;
}
const State* PlayerFall_Update(Player* self, float deltaTime) {
self->verticalVelocity -= 3.f * deltaTime;
if(self->height == 0.f)
return PlayerIdle();
return PlayerFall();
}

View file

@ -54,4 +54,22 @@ DefineState(PlayerKickA, Player,
PlayerAnimationExit
)
void PlayerJump_Enter(Player *self);
const State* PlayerJump_Update(Player* self, float deltaTime);
DefineState(PlayerJump, Player,
PlayerJump_Enter,
PlayerJump_Update,
PlayerAnimationExit
)
void PlayerFall_Enter(Player* self);
const State* PlayerFall_Update(Player* self, float deltaTime);
DefineState(PlayerFall, Player,
PlayerFall_Enter,
PlayerFall_Update,
PlayerAnimationExit
)
#endif // !FIGHT_PLAYER_STATES_H