summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/game.c4
-rw-r--r--src/player.c33
2 files changed, 24 insertions, 13 deletions
diff --git a/src/game.c b/src/game.c
index b15c85f..4a10830 100644
--- a/src/game.c
+++ b/src/game.c
@@ -183,8 +183,10 @@ void UpdateCurrentSpriteAnimation(Sprite* sprite, float dt) {
}
if (currentAnimation->currentFrame >= currentAnimation->numFrames) {
- if (currentAnimation->isLooping)
+ if (currentAnimation->isLooping) {
currentAnimation->currentFrame = 0;
+ currentAnimation->currentFrameTimer = 0.0f;
+ }
else {
currentAnimation->currentFrame = currentAnimation->numFrames - 1;
currentAnimation->isComplete = true;
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;