-
Notifications
You must be signed in to change notification settings - Fork 5
/
player.c
135 lines (117 loc) · 4.41 KB
/
player.c
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
#include <furi.h>
#include "floopper-bloopper/floopper-bloopper.h"
#include "floopper-bloopper/player_0/player_0_0.xbm"
#include "floopper-bloopper/player_0/player_0_1.xbm"
#include "floopper-bloopper/player_0/player_0_2.xbm"
#include "floopper-bloopper/player_0/player_0_3.xbm"
#include "floopper-bloopper/player_0/player_0_4.xbm"
#include "floopper-bloopper/player_0/player_0_5.xbm"
#include "floopper-bloopper/player_0/player_0_6.xbm"
void render_player(GameState* state, Canvas* canvas) {
unsigned char* player_sprite = NULL;
if(state->player_v.y > 40) {
player_sprite = (unsigned char*)player_0_2_bits;
} else if(state->player_v.y < -40) {
player_sprite = (unsigned char*)player_0_3_bits;
} else {
if(state->player_v.x < 0) {
if(state->player_anim == 0) {
player_sprite = (unsigned char*)player_0_0_bits;
} else {
player_sprite = (unsigned char*)player_0_1_bits;
}
} else if(state->player_v.x > 0) {
if(state->player_anim == 0) {
player_sprite = (unsigned char*)player_0_5_bits;
} else {
player_sprite = (unsigned char*)player_0_6_bits;
}
} else {
player_sprite = (unsigned char*)player_0_4_bits;
}
}
if(player_sprite != NULL) {
canvas_draw_xbm(
canvas,
state->player.x / SCALE,
state->player.y / SCALE,
player_0_0_width,
player_0_0_height,
(unsigned char*)player_sprite);
}
}
void handle_player_input(GameState* state, InputEvent* input) {
if(input->type == InputTypePress) {
if(input->key == InputKeyRight) {
state->player_v.x = SPEED_X;
} else if(input->key == InputKeyLeft) {
state->player_v.x = -SPEED_X;
}
} else if(input->type == InputTypeRelease) {
if(input->key == InputKeyRight || input->key == InputKeyLeft) {
state->player_v.x = 0;
}
}
if(input->key == InputKeyUp) {
if(input->type == InputTypePress) {
state->player_v.y = JUMP_SPEED;
}
}
}
void update_player_coordinates(GameState* state, uint32_t dt) {
state->player.y += state->player_v.y * dt;
state->player_global.y += state->player_v.y * dt;
if(state->player.x < BONDARIES_X_LEFT * SCALE) {
state->player.x = BONDARIES_X_LEFT * SCALE;
} else if(state->player.x > (BONDARIES_X_RIGHT - PLAYER_WIDTH) * SCALE) {
state->player.x = (BONDARIES_X_RIGHT - PLAYER_WIDTH) * SCALE;
} else {
state->player.x += state->player_v.x * dt;
}
if(state->player.x <= BONDARIES_X_LEFT * SCALE) {
if(!state->in_boundaries) {
state->player_global.x += state->player_v.x * dt;
}
} else if(state->player.x >= (BONDARIES_X_RIGHT - PLAYER_WIDTH) * SCALE) {
if(!state->in_boundaries) {
state->player_global.x += state->player_v.x * dt;
}
} else {
state->player_global.x += state->player_v.x * dt;
}
// gravity + floor
int32_t floor_height = SCREEN_HEIGHT * SCALE -
HEIGHT_MAP[(abs(state->player_global.x) / SCALE) % WORLD_WIDTH] -
PLAYER_HEIGHT * SCALE;
int32_t f_h;
for(size_t i = 1; i < PLAYER_WIDTH; i++) {
f_h = SCREEN_HEIGHT * SCALE -
HEIGHT_MAP[(abs(state->player_global.x) / SCALE + i) % WORLD_WIDTH] -
PLAYER_HEIGHT * SCALE;
if(f_h < floor_height) {
floor_height = f_h;
}
}
if(state->player_global.y >= floor_height && !state->next_level) {
state->player.y = floor_height;
state->player_global.y = floor_height;
state->player_v.y = 0;
} else {
state->player_v.y += 5;
}
if(state->player.y > (SCREEN_HEIGHT - PLAYER_HEIGHT - 4) * SCALE) {
state->player.y = (SCREEN_HEIGHT - PLAYER_HEIGHT - 4) * SCALE;
}
// global
state->screen.x = state->player_global.x - state->player.x;
state->screen.y = state->player.y - state->player_global.y;
if(state->player_global.x < BONDARIES_X_LEFT * SCALE) {
state->player_global.x += WORLD_WIDTH * SCALE;
}
/*
if (state->player_global.x > (WORLD_WIDTH - BONDARIES_X_RIGHT * SCALE)) {
state->player_global.x -= WORLD_WIDTH * SCALE;
}
*/
state->player_anim = (state->player_global.x / (SCALE * 4)) % 2;
}