diff options
author | BoredGuy <osome3717@gmail.com> | 2025-08-23 09:26:24 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-08-23 09:26:24 +0300 |
commit | 978c543891af8dbe9e95b27c4e2c46645d45138c (patch) | |
tree | 3bbf8b3397ab50929a442be67cf334ffdcf0e3ef /src/player.c | |
parent | 0f25d9fb178e9fb32793331aa1ed724d9747702d (diff) |
Animation system bugfix and player refactor
Diffstat (limited to 'src/player.c')
-rw-r--r-- | src/player.c | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/src/player.c b/src/player.c index 62675f4..9340ffd 100644 --- a/src/player.c +++ b/src/player.c @@ -25,32 +25,41 @@ const Rectangle physicsCollider = (Rectangle) { .height = 100 }; -void UpdatePlayer(Entity* player, float deltaTime) { - player->velocity = (Vector2) {0.0f, 0.0f}; +Vector2 GetMovementDirection() { + Vector2 movementDirection = {0.0f, 0.0f}; - if (IsKeyDown(KEY_W)) { - player->velocity.y = -1.0f; + if (IsKeyDown(KEY_W)) { + movementDirection.y = -1.0f; } else if (IsKeyDown(KEY_S)) { - player->velocity.y = 1.0f; + movementDirection.y = 1.0f; } if (IsKeyDown(KEY_A)) { - player->velocity.x = -1.0f; + movementDirection.x = -1.0f; } else if (IsKeyDown(KEY_D)) { - player->velocity.x = 1.0f; + movementDirection.x = 1.0f; } - if (Vector2Length(player->velocity) > 0.0f) { - player->velocity = Vector2Normalize(player->velocity); - } + if (Vector2Length(movementDirection) > 0.0f) + return Vector2Normalize(movementDirection); - player->velocity = Vector2Scale(player->velocity, PLAYER_SPEED); - MoveAndSlide(player, deltaTime); + return movementDirection; +} +void CameraFollow(const Entity* player) { game.camera.target = (Vector2) {fmaxf(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f}; } +void UpdatePlayer(Entity* player, float deltaTime) { + player->velocity = + Vector2Scale(GetMovementDirection(), PLAYER_SPEED); + + MoveAndSlide(player, deltaTime); + + CameraFollow(player); +} + void AddPlayer(float xpos, float ypos) { Entity player = {0}; player.type = Player_Entity; |