diff options
Diffstat (limited to 'src/player.c')
-rw-r--r-- | src/player.c | 53 |
1 files changed, 43 insertions, 10 deletions
diff --git a/src/player.c b/src/player.c index 896f33e..97b269f 100644 --- a/src/player.c +++ b/src/player.c @@ -18,6 +18,7 @@ static inline Sprite* GetBodySprite(Entity* player) { typedef enum PlayerStates { PLAYER_IDLE = 0, PLAYER_WALKING = 1, + PLAYER_ATTACK = 2, } PlayerStates; Vector2 GetMovementDirection() { @@ -46,7 +47,16 @@ void CameraFollow(const Entity* player) { (Vector2) {fmaxf(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f}; } -void UpdateAnimationState(Entity* player) { +void UpdateBodyFacing(Entity* player) { + Sprite* bodySprite = GetBodySprite(player); + + if (player->velocity.x < 0) + bodySprite->flipX = true; + else if (player->velocity.x > 0) + bodySprite->flipX = false; +} + +void HandleMovement(Entity* player) { Sprite* bodySprite = GetBodySprite(player); if (Vector2Length(player->velocity) > 0.0) @@ -55,24 +65,33 @@ void UpdateAnimationState(Entity* player) { bodySprite->currentAnimation = PLAYER_IDLE; } -void UpdateBodyFacing(Entity* player) { +void HandleInput(Entity* player) { + player->velocity = + Vector2Scale(GetMovementDirection(), PLAYER_SPEED); +} + +void UpdateAnimation(Entity* player, float deltaTime) { Sprite* bodySprite = GetBodySprite(player); + UpdateCurrentSpriteAnimation(bodySprite, deltaTime); +} - if (player->velocity.x < 0) +void FlipBody(Entity* player) { + Sprite* bodySprite = GetBodySprite(player); + + if (player->velocity.x < 0) { bodySprite->flipX = true; - else if (player->velocity.x > 0) + } else if (player->velocity.x > 0) { bodySprite->flipX = false; + } } void UpdatePlayer(Entity* player, float deltaTime) { - player->velocity = - Vector2Scale(GetMovementDirection(), PLAYER_SPEED); - - UpdateAnimationState(player); - UpdateBodyFacing(player); + HandleInput(player); + HandleMovement(player); + FlipBody(player); MoveAndSlide(player, deltaTime); + UpdateAnimation(player, deltaTime); CameraFollow(player); - UpdateCurrentSpriteAnimation(GetBodySprite(player), deltaTime); } void AddPlayer(float xpos, float ypos) { @@ -151,6 +170,20 @@ static void AddPlayerSprites(Entity* player) { }); bodySprite.animations[PLAYER_WALKING] = walkingAnimation; + Animation punchAnimation = + AnimationFromIndices((AnimationFromIndicesParams){ + .texture = bodyTexture->texture, + .isLooping = false, + .numFrames = 4, + + .framesX = 10, + .framesY = 10, + + .indices = (int[]) {20, 21, 22, 23}, + .frameTimes = (float[]) {0.05f, 0.05f, 0.05f, 0.05f} + }); + bodySprite.animations[PLAYER_ATTACK] = punchAnimation; + bodySprite.numAnimations = 2; player->bodySpriteIndex = AddSpriteToEntity(player, bodySprite); } |