From 8886377977fbc79bb30310543e2978ff0c7febfe Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Fri, 5 Sep 2025 08:55:43 +0300 Subject: Player module refactor - Also changed the background to something cozier --- include/player_data.h | 8 ++++---- include/utils.h | 6 ++++++ src/assets.c | 4 ++-- src/main.c | 2 +- src/player.c | 53 +++++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 56 insertions(+), 17 deletions(-) diff --git a/include/player_data.h b/include/player_data.h index bcd3bae..f8e937a 100644 --- a/include/player_data.h +++ b/include/player_data.h @@ -6,10 +6,10 @@ #define PLAYER_SPEED 300.0f static const Rectangle shadowDestRect = (Rectangle) { - .x = -30, - .y = 100, - .width = 140, - .height = 40 + .x = -20, + .y = 115, + .width = 128, + .height = 20 }; static const Rectangle physicsCollider = (Rectangle) { diff --git a/include/utils.h b/include/utils.h index cb1770e..7437860 100644 --- a/include/utils.h +++ b/include/utils.h @@ -76,5 +76,11 @@ static inline Rectangle GetEntityHurtboxGlobal(const Entity* e, int hbIndex) { return hurtBoxGlobal; } +static inline void ResetAnimation(Animation* animation) { + animation->currentFrame = 0; + animation->currentFrameTimer = 0; + animation->isComplete = false; +} + #endif // UTILS_H_ diff --git a/src/assets.c b/src/assets.c index 7fb6477..d71caca 100644 --- a/src/assets.c +++ b/src/assets.c @@ -7,8 +7,8 @@ Asset assets[] = { { .type = Texture_Asset, - .name = "bar-background", - .filePath = "assets/art/backgrounds/bar-background.png" + .name = "street-background", + .filePath = "assets/art/backgrounds/street-background.png" }, { .type = Texture_Asset, diff --git a/src/main.c b/src/main.c index 7032ca9..bb658ce 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ int main() { LoadAssets(); InitGame(); - AddBackground("bar-background"); + AddBackground("street-background"); AddPlayer(0, 400); AddWall(0, 160, 1000000, 100); AddBarrel(200, 400); 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); } -- cgit v1.2.3