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, playerinput_add(self->playerInput,
CompositeAxis1D_as_InputAxis(compositeaxis1d_from_keys(SDL_SCANCODE_S, SDL_SCANCODE_W)), CompositeAxis1D_as_InputAxis(compositeaxis1d_from_keys(SDL_SCANCODE_S, SDL_SCANCODE_W)),
(InputDelegateFn)PlayerVerticalInput); (InputDelegateFn)PlayerVerticalInput);
playerinput_add(self->playerInput,
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_SPACE)),
(InputDelegateFn)PlayerJumpInput);
playerinput_add(self->playerInput, playerinput_add(self->playerInput,
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_J)), KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_J)),
(InputDelegateFn)PlayerLightAttackInput); (InputDelegateFn)PlayerLightAttackInput);
@ -59,11 +62,14 @@ Player* MakePlayer() {
*self = (Player) { *self = (Player) {
.transform = IdentityTransform, .transform = IdentityTransform,
.height = 0.f,
.rigidbody = NULL, .rigidbody = NULL,
.physicsCollider = NULL, .physicsCollider = NULL,
.hitbox = NULL, .hitbox = NULL,
.verticalVelocity = 0.f,
.playerInput = playerinput_new(self, -1), .playerInput = playerinput_new(self, -1),
.moveInput = ZeroVector, .moveInput = ZeroVector,
.attackInput = 0, .attackInput = 0,
@ -75,6 +81,7 @@ Player* MakePlayer() {
.idle = NULL, .idle = NULL,
.walk = NULL, .walk = NULL,
.jump = NULL,
.jab_a = NULL, .jab_a = NULL,
.jab_b = NULL, .jab_b = NULL,
.kick_a = 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->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->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_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->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); 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) { void PlayerUpdate(Player* self, float deltaTime) {
state_machine_update(self->animationStateMachine, 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) { 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) { void PlayerHorizontalInput(Player* self, InputEvent value) {
@ -156,6 +169,10 @@ void PlayerVerticalInput(Player* self, InputEvent value) {
self->moveInput.y = -value.as_float; self->moveInput.y = -value.as_float;
} }
void PlayerJumpInput(Player* self, InputEvent value) {
self->jumpInput = value.as_bool;
}
void PlayerLightAttackInput(Player* self, InputEvent value) { void PlayerLightAttackInput(Player* self, InputEvent value) {
if(value.as_bool) if(value.as_bool)
self->attackInput = 1; self->attackInput = 1;

View file

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

View file

@ -19,6 +19,7 @@ void PlayerAnimationExit(Player* self) {
void PlayerIdleEnter(Player* self) { void PlayerIdleEnter(Player* self) {
self->currentAnimation = self->idle; self->currentAnimation = self->idle;
rigidbody_set_velocity(self->rigidbody, ZeroVector);
animation_sprite_play_from(self->currentAnimation, 0.f); animation_sprite_play_from(self->currentAnimation, 0.f);
} }
@ -29,6 +30,8 @@ const State* PlayerIdleUpdate(Player* self, float deltaTime) {
return PlayerJabA(); return PlayerJabA();
if(self->attackInput == 2) if(self->attackInput == 2)
return PlayerKickA(); return PlayerKickA();
if(self->jumpInput)
return PlayerJump();
return PlayerIdle(); return PlayerIdle();
} }
@ -46,6 +49,8 @@ const State* PlayerWalk_Update(Player* self, float deltaTime) {
return PlayerJabA(); return PlayerJabA();
if(self->attackInput == 2) if(self->attackInput == 2)
return PlayerKickA(); return PlayerKickA();
if(self->jumpInput)
return PlayerJump();
InternalSpriteFlipWithMovement(self); InternalSpriteFlipWithMovement(self);
return PlayerWalk(); return PlayerWalk();
} }
@ -156,3 +161,27 @@ const State* PlayerKickA_Update(Player* self, float deltaTime) {
return PlayerIdle(); return PlayerIdle();
return PlayerKickA(); 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 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 #endif // !FIGHT_PLAYER_STATES_H