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
|
#include <stdlib.h>
#include <math.h>
#include "constants.h"
#include "game.h"
#include "player.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
};
const Rectangle physicsCollider = (Rectangle) {
.x = 0,
.y = 0,
.width = 100,
.height = 100
};
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 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;
player.position = (Vector2) {xpos, ypos};
player.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE);
player.physicsCollider = physicsCollider;
#ifdef BEATEMUP_DEBUG
player.physicsColliderColor = RED;
#endif
Asset* shadowTexture = GetMatchingAssetWithType("human-shadow", Texture_Asset);
if (shadowTexture == NULL) {
TraceLog(LOG_ERROR, "Failed to find texture asset human-shadow, exitting!");
exit(EXIT_FAILURE);
}
AddSpriteToEntity(&player, (Sprite){
.texture = shadowTexture->texture,
.layer = Foreground_Layer,
.destRect = shadowDestRect
});
AddEntity(&player);
}
|