1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include <stdlib.h>
#include <math.h>
#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;
static void AddPlayerSprites(Entity* player);
static inline Sprite* GetBodySprite(Entity* player) {
return &player->sprites[player->bodySpriteIndex];
}
typedef enum PlayerStates {
PLAYER_IDLE = 0,
PLAYER_WALKING = 1,
} PlayerStates;
Vector2 GetMovementDirection() {
Vector2 movementDirection = {0.0f, 0.0f};
if (IsKeyDown(KEY_W)) {
movementDirection.y = -1.0f;
} else if (IsKeyDown(KEY_S)) {
movementDirection.y = 1.0f;
}
if (IsKeyDown(KEY_A)) {
movementDirection.x = -1.0f;
} else if (IsKeyDown(KEY_D)) {
movementDirection.x = 1.0f;
}
if (Vector2Length(movementDirection) > 0.0f)
return Vector2Normalize(movementDirection);
return movementDirection;
}
void CameraFollow(const Entity* player) {
game.camera.target =
(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) {
Entity player = {0};
player.type = Player_Entity;
player.position = (Vector2) {xpos, ypos};
player.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE);
player.physicsCollider = physicsCollider;
#ifdef BEATEMUP_DEBUG
player.physicsColliderColor = RED;
#endif
AddPlayerSprites(&player);
AddEntity(&player);
}
static void AddPlayerSprites(Entity* player) {
Asset* shadowTexture = GetMatchingAssetExitOnFail("human-shadow", Texture_Asset);
Sprite shadowSprite = (Sprite) {
.texture = shadowTexture->texture,
.layer = Foreground_Layer,
.destRect = shadowDestRect,
.srcRect = {
0,
0,
shadowTexture->texture.width,
shadowTexture->texture.height
},
.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);
}
|