-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.c
More file actions
89 lines (76 loc) · 2.84 KB
/
Copy pathplayer.c
File metadata and controls
89 lines (76 loc) · 2.84 KB
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
#include "entities.h"
Player createPlayer(int type, Texture2D *texture) {
int id = 0; // TODO: hashmap or list or array
Vector2 position = { 0.0f, 0.0f };
Vector2 velocity = { 0.0f, 0.0f };
return (Player) { id, position, velocity, texture, 0 };
}
void deletePlayer(Player *player) {
// ??
}
void inputPlayer(Player *player) {
if(IsKeyDown(KEY_RIGHT) && !IsKeyDown(KEY_LEFT)) {
// currentFrame = 0;
if(player->velocity.x < 10) {
player->velocity.x = 10;
}
} else if(IsKeyDown(KEY_LEFT) && !IsKeyDown(KEY_RIGHT)) {
// currentFrame = 0;
if(player->velocity.x > -10) {
player->velocity.x = -10;
}
} else {
if(player->velocity.x <= 10 && player->velocity.x >= -10) {
player->velocity.x = 0;
}
}
if(IsKeyDown(KEY_SPACE) && player->wasOnGround) {
// currentFrame = 0;
player->velocity.y = -20;
}
if(IsKeyPressed(KEY_LEFT_SHIFT)) {
player->velocity.x *= 10;
} else if(player->velocity.x > 10) {
player->velocity.x -= player->velocity.x * player->velocity.x / 150;
if(player->velocity.x < 10.1f) {
player->velocity.x = 10;
}
} else if(player->velocity.x < -10) {
player->velocity.x += player->velocity.x * player->velocity.x / 150;
if(player->velocity.x > -10.1f) {
player->velocity.x = -10;
}
}
}
void moveAndCheckPlayer(Player *player, int *levelDim, int *floors, int *walls) {
player->position.x += player->velocity.x;
int neighborTiles[8];
getNeighboringTiles(&player->position, levelDim[0], 0, neighborTiles); // TODO: compute only x neighboringTiles
// printf("%d,%d,%d,%d\n", neighborTiles[0], neighborTiles[1], neighborTiles[2], neighborTiles[3]);
if(walls[neighborTiles[0]] || walls[neighborTiles[7]]) {
player->position.x = (neighborTiles[0] % levelDim[0] + 1) * 40;
player->velocity.x = 0;
}
if(walls[neighborTiles[3]] || walls[neighborTiles[4]]) {
player->position.x = (neighborTiles[3] % levelDim[0] - 1) * 40;
player->velocity.x = 0;
}
player->position.y += player->velocity.y;
getNeighboringTiles(&player->position, levelDim[0], 0, neighborTiles);
// printf("%f, %f\n", position.y, velocity.y);
if(floors[neighborTiles[1]] || floors[neighborTiles[2]]) {
player->position.y = (neighborTiles[1] / levelDim[0] + 1) * 40;
player->velocity.y = 0;
}
// printf(">%d, %d\n", neighborTiles[5], neighborTiles[6]);
if(floors[neighborTiles[5]] || floors[neighborTiles[6]]) {
player->position.y = (neighborTiles[5] / levelDim[0] - 1) * 40;
player->velocity.y = 0;
player->wasOnGround = 1;
} else {
player->wasOnGround = 0;
}
if(!player->wasOnGround) {
player->velocity.y += 1;
}
}