diff options
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; |