summaryrefslogtreecommitdiff
path: root/src/player.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/player.c')
-rw-r--r--src/player.c111
1 files changed, 85 insertions, 26 deletions
diff --git a/src/player.c b/src/player.c
index 9340ffd..ca5d3c0 100644
--- a/src/player.c
+++ b/src/player.c
@@ -3,27 +3,22 @@
#include "constants.h"
#include "game.h"
#include "player.h"
+#include "player_data.h"
#include "physics.h"
#include "assets.h"
#include "utils.h"
extern Game game;
-#define PLAYER_SPEED 300.0f
-
-const Rectangle shadowDestRect = (Rectangle) {
- .x = 0,
- .y = 100,
- .width = 100,
- .height = 40
-};
+static void AddPlayerSprites(Entity* player);
+static inline Sprite* GetBodySprite(Entity* player) {
+ return &player->sprites[player->bodySpriteIndex];
+}
-const Rectangle physicsCollider = (Rectangle) {
- .x = 0,
- .y = 0,
- .width = 100,
- .height = 100
-};
+typedef enum PlayerStates {
+ PLAYER_IDLE = 0,
+ PLAYER_WALKING = 1,
+} PlayerStates;
Vector2 GetMovementDirection() {
Vector2 movementDirection = {0.0f, 0.0f};
@@ -51,13 +46,33 @@ void CameraFollow(const Entity* player) {
(Vector2) {fmaxf(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f};
}
+void UpdateAnimationState(Entity* player) {
+ Sprite* bodySprite = GetBodySprite(player);
+
+ if (Vector2Length(player->velocity) > 0.0)
+ bodySprite->currentAnimation = PLAYER_WALKING;
+ else
+ bodySprite->currentAnimation = PLAYER_IDLE;
+}
+
+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 UpdatePlayer(Entity* player, float deltaTime) {
player->velocity =
Vector2Scale(GetMovementDirection(), PLAYER_SPEED);
+ UpdateAnimationState(player);
+ UpdateBodyFacing(player);
MoveAndSlide(player, deltaTime);
-
CameraFollow(player);
+ UpdateCurrentSpriteAnimation(GetBodySprite(player), deltaTime);
}
void AddPlayer(float xpos, float ypos) {
@@ -73,18 +88,62 @@ void AddPlayer(float xpos, float ypos) {
player.physicsColliderColor = RED;
#endif
- Asset* shadowTexture = GetMatchingAssetWithType("human-shadow", Texture_Asset);
+ AddPlayerSprites(&player);
+ AddEntity(&player);
+}
- if (shadowTexture == NULL) {
- TraceLog(LOG_ERROR, "Failed to find texture asset human-shadow, exitting!");
- exit(EXIT_FAILURE);
- }
+static void AddPlayerSprites(Entity* player) {
+ Asset* shadowTexture = GetMatchingAssetExitOnFail("human-shadow", Texture_Asset);
- AddSpriteToEntity(&player, (Sprite){
- .texture = shadowTexture->texture,
- .layer = Foreground_Layer,
- .destRect = shadowDestRect
- });
+ Sprite shadowSprite = (Sprite) {
+ .texture = shadowTexture->texture,
+ .layer = Foreground_Layer,
+ .destRect = shadowDestRect,
- AddEntity(&player);
+ .numAnimations = 0, //Not an animated sprite
+ };
+ AddSpriteToEntity(player, shadowSprite);
+
+ Asset* bodyTexture = GetMatchingAssetExitOnFail("player-body", Texture_Asset);
+
+ Sprite bodySprite = (Sprite) {
+ .texture = bodyTexture->texture,
+ .layer = Foreground_Layer,
+
+ .destRect = bodyDestRect,
+ .numAnimations = 0
+ };
+
+ //The body texture devided into a 10x10 grid
+ //with one cell representing a frame of animation
+ Animation idleAnimation =
+ AnimationFromIndices((AnimationFromIndicesParams){
+ .texture = bodyTexture->texture,
+ .isLooping = true,
+ .numFrames = 4,
+
+ .framesX = 10,
+ .framesY = 10,
+
+ .indices = (int[]) {0, 1, 2, 3},
+ .frameTimes = (float[]) {0.1f, 0.1f, 0.1f, 0.1f}
+ });
+ bodySprite.animations[PLAYER_IDLE] = idleAnimation;
+
+ Animation walkingAnimation =
+ AnimationFromIndices((AnimationFromIndicesParams){
+ .texture = bodyTexture->texture,
+ .isLooping = true,
+ .numFrames = 7,
+
+ .framesX = 10,
+ .framesY = 10,
+
+ .indices = (int[]) {10, 11, 12, 13, 14, 15, 16, 17},
+ .frameTimes = (float[]) {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f}
+ });
+ bodySprite.animations[PLAYER_WALKING] = walkingAnimation;
+
+ bodySprite.numAnimations = 2;
+ player->bodySpriteIndex = AddSpriteToEntity(player, bodySprite);
}