-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cpp
312 lines (248 loc) · 11.7 KB
/
Player.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "Player.hpp"
Player::Player() {
// TODO: remove literals ( 0 and 4 )
blue = Square(TILE_ID::PLAYER::BLUE, 0, 0);
pink = Square(TILE_ID::PLAYER::PINK, 0, 0);
}
Player::Player(uint16_t blue_x, uint16_t blue_y, uint16_t pink_x, uint16_t pink_y) {
// TODO: remove literals
blue = Square(0, blue_x, blue_y);
pink = Square(4, pink_x, pink_y);
}
bool Player::update(InputHandler& input_handler, AudioHandler& audio_handler, LevelHandler& level_handler, float dt) {
bool reset = false;
std::vector<Tile> tiles = level_handler.get_tiles();
// Handle inputs
if (KeyHandler::is_down(input_handler.get_key_union().keys.RIGHT)) {
blue.accelerate(GAME::SQUARE::ACCELERATION, GAME::SQUARE::VELOCITY_MAX, dt);
pink.accelerate(-GAME::SQUARE::DECELERATION, GAME::SQUARE::VELOCITY_MAX, dt);
}
else if (KeyHandler::is_down(input_handler.get_key_union().keys.LEFT)) {
blue.accelerate(-GAME::SQUARE::ACCELERATION, GAME::SQUARE::VELOCITY_MAX, dt);
pink.accelerate(GAME::SQUARE::DECELERATION, GAME::SQUARE::VELOCITY_MAX, dt);
}
else {
// Handle deceleration
blue.decelerate(GAME::SQUARE::DECELERATION, dt);
pink.decelerate(GAME::SQUARE::DECELERATION, dt);
}
if (KeyHandler::is_down(input_handler.get_key_union().keys.SPACE)) {
blue.jump(GAME::SQUARE::JUMP_STRENGTH);
pink.jump(GAME::SQUARE::JUMP_STRENGTH);
// Play jump sfx
if (blue.get_can_jump() || pink.get_can_jump()) {
audio_handler.play_sound(audio_handler.sound_samples.at(AUDIO::SFX::JUMP));
}
}
// Update player physics
blue.gravity(GAME::ENVIRONMENT::GRAVITY::ACCELERATION, GAME::ENVIRONMENT::GRAVITY::MAX, dt);
pink.gravity(GAME::ENVIRONMENT::GRAVITY::ACCELERATION, GAME::ENVIRONMENT::GRAVITY::MAX, dt);
// Draw players towards finishes if in both are in range
if (std::abs(blue.get_x() - level_handler.level_finish_blue_x) < level_handler.get_sprite_size() &&
std::abs(blue.get_y() - (level_handler.level_finish_blue_y - GAME::FINISH::HEIGHT)) < level_handler.get_sprite_size() / 2 &&
std::abs(pink.get_x() - level_handler.level_finish_pink_x) < level_handler.get_sprite_size() &&
std::abs(pink.get_y() - (level_handler.level_finish_pink_y - GAME::FINISH::HEIGHT)) < level_handler.get_sprite_size() / 2 &&
!blue.get_finished() && !pink.get_finished()) {
//float blue_vel_x = GAME::SQUARE::FINISH_PULL_VELOCITY * (level_handler.level_finish_blue_x - blue.get_x());
//float blue_vel_y = 0.0f;//SQAURE_FINISH_PULL_VELOCITY * (level_handler.level_finish_blue_y - blue.get_y());
//float pink_vel_x = GAME::SQUARE::FINISH_PULL_VELOCITY * (level_handler.level_finish_pink_x - pink.get_x());
//float pink_vel_y = 0.0f;//GAME::SQUARE::FINISH_PULL_MIN_VELOCITY * (level_handler.level_finish_pink_y - pink.get_y());
/*if (std::abs(blue_vel_x) < GAME::SQUARE::FINISH_PULL_MIN_VELOCITY) {
blue_vel_x = GAME::SQUARE::FINISH_PULL_MIN_VELOCITY * blue_vel_x > 0 ? 1 : -1;
}
if (std::abs(pink_vel_x) < GAME::SQUARE::FINISH_PULL_MIN_VELOCITY) {
pink_vel_x = GAME::SQUARE::FINISH_PULL_MIN_VELOCITY * pink_vel_x > 0 ? 1 : -1;
}*/
//blue.add_velocity(blue_vel_x, blue_vel_y);
//pink.add_velocity(pink_vel_x, pink_vel_y);
//blue.add_velocity(GAME::SQUARE::FINISH_PULL_VELOCITY * (level_handler.level_finish_blue_x - blue.get_x() > 0 ? 1 : -1), 0.0f);
//pink.add_velocity(GAME::SQUARE::FINISH_PULL_VELOCITY * (level_handler.level_finish_pink_x - pink.get_x() > 0 ? 1 : -1), 0.0f);
float blue_vel_x = GAME::FINISH::PULL_VELOCITY * (level_handler.level_finish_blue_x - blue.get_x());
float blue_vel_y = GAME::FINISH::PULL_VELOCITY * (level_handler.level_finish_blue_y - GAME::FINISH::HEIGHT - blue.get_y());
float pink_vel_x = GAME::FINISH::PULL_VELOCITY * (level_handler.level_finish_pink_x - pink.get_x());
float pink_vel_y = GAME::FINISH::PULL_VELOCITY * (level_handler.level_finish_pink_y - GAME::FINISH::HEIGHT - pink.get_y());
if (std::abs(blue_vel_x) < GAME::FINISH::PULL_MIN_VELOCITY) {
blue_vel_x = GAME::FINISH::PULL_MIN_VELOCITY * blue_vel_x > 0 ? 1 : -1;
}
if (std::abs(pink_vel_x) < GAME::FINISH::PULL_MIN_VELOCITY) {
pink_vel_x = GAME::FINISH::PULL_MIN_VELOCITY * pink_vel_x > 0 ? 1 : -1;
}
blue.add_velocity(blue_vel_x, blue_vel_y);
pink.add_velocity(pink_vel_x, pink_vel_y);
}
// Launch players if on spring at right time
for (Spring& spring : level_handler.get_springs()) {
if (spring.should_launch()) {
if (spring.check_collision(blue.get_x(), blue.get_y())) {
// Reset velocity first to hopefully fix 'super-jump' bug
blue.reset_y_vel();
blue.add_velocity(0.0f, -GAME::SPRING::LAUNCH_VELOCITY);
// Play spring sound
audio_handler.play_sound(audio_handler.sound_samples.at(AUDIO::SFX::SPRING));
}
if (spring.check_collision(pink.get_x(), pink.get_y())) {
pink.reset_y_vel();
pink.add_velocity(0.0f, -GAME::SPRING::LAUNCH_VELOCITY);
// Play spring sound
audio_handler.play_sound(audio_handler.sound_samples.at(AUDIO::SFX::SPRING));
}
}
}
// Get buttons currently pressed, check if it's different after updating the buttons.
// Note: player.update(...) doesn't handle releasing of buttons, so no sfx will be played when buttons unpress themselves.
uint16_t a = 0; // Bad things happen if more than 16 buttons!
uint8_t i = 0;
for (Button& button : level_handler.get_buttons()) {
a += button.get_pressed() ? std::pow(2, i) : 0;
i++;
}
bool should_die = false;
should_die |= blue.update(tiles, level_handler.get_springs(), level_handler.get_buttons(), level_handler.get_doors(), dt);
should_die |= pink.update(tiles, level_handler.get_springs(), level_handler.get_buttons(), level_handler.get_doors(), dt);
uint16_t b = 0;
i = 0;
for (Button& button : level_handler.get_buttons()) {
b += button.get_pressed() ? std::pow(2, i) : 0;
i++;
}
// Play button click sfx
// rather hacky using a and b vars
if (a != b) {
audio_handler.play_sound(audio_handler.sound_samples.at(AUDIO::SFX::BUTTON));
}
// Update player position to allow walking over finish
if (is_colliding(level_handler.level_finish_blue_x + GAME::FINISH::BORDER, level_handler.level_finish_blue_y + SPRITES::SIZE - GAME::FINISH::HEIGHT,
blue.get_x(), blue.get_y(), GAME::FINISH::WIDTH, GAME::FINISH::HEIGHT, SPRITES::SIZE, SPRITES::SIZE)) {
blue.set_y(level_handler.level_finish_blue_y - GAME::FINISH::HEIGHT);
blue.reset_y_vel();
}
else if (is_colliding(level_handler.level_finish_pink_x + GAME::FINISH::BORDER, level_handler.level_finish_pink_y + SPRITES::SIZE - GAME::FINISH::HEIGHT,
blue.get_x(), blue.get_y(), GAME::FINISH::WIDTH, GAME::FINISH::HEIGHT, SPRITES::SIZE, SPRITES::SIZE)) {
blue.set_y(level_handler.level_finish_pink_y - GAME::FINISH::HEIGHT);
blue.reset_y_vel();
}
if (is_colliding(level_handler.level_finish_blue_x + GAME::FINISH::BORDER, level_handler.level_finish_blue_y + SPRITES::SIZE - GAME::FINISH::HEIGHT,
pink.get_x(), pink.get_y(), GAME::FINISH::WIDTH, GAME::FINISH::HEIGHT, SPRITES::SIZE, SPRITES::SIZE)) {
pink.set_y(level_handler.level_finish_blue_y - GAME::FINISH::HEIGHT);
pink.reset_y_vel();
}
else if (is_colliding(level_handler.level_finish_pink_x + GAME::FINISH::BORDER, level_handler.level_finish_pink_y + SPRITES::SIZE - GAME::FINISH::HEIGHT,
pink.get_x(), pink.get_y(), GAME::FINISH::WIDTH, GAME::FINISH::HEIGHT, SPRITES::SIZE, SPRITES::SIZE)) {
pink.set_y(level_handler.level_finish_pink_y - GAME::FINISH::HEIGHT);
pink.reset_y_vel();
}
// Blue collision with finishes
if (!blue.get_can_jump()) {
if (check_on_top(level_handler.level_finish_blue_x + GAME::FINISH::BORDER, level_handler.level_finish_blue_y + SPRITES::SIZE - GAME::FINISH::HEIGHT, blue.get_x(), blue.get_y(), GAME::FINISH::WIDTH, GAME::FINISH::HEIGHT, SPRITES::SIZE, SPRITES::SIZE)) {
blue.set_can_jump();
}
}
if (!blue.get_can_jump()) {
if (check_on_top(level_handler.level_finish_pink_x + GAME::FINISH::BORDER, level_handler.level_finish_pink_y + SPRITES::SIZE - GAME::FINISH::HEIGHT, blue.get_x(), blue.get_y(), GAME::FINISH::WIDTH, GAME::FINISH::HEIGHT, SPRITES::SIZE, SPRITES::SIZE)) {
blue.set_can_jump();
}
}
// Pink collision with finishes
if (!pink.get_can_jump()) {
if (check_on_top(level_handler.level_finish_blue_x + GAME::FINISH::BORDER, level_handler.level_finish_blue_y + SPRITES::SIZE - GAME::FINISH::HEIGHT, pink.get_x(), pink.get_y(), GAME::FINISH::WIDTH, GAME::FINISH::HEIGHT, SPRITES::SIZE, SPRITES::SIZE)) {
pink.set_can_jump();
}
}
if (!pink.get_can_jump()) {
if (check_on_top(level_handler.level_finish_pink_x + GAME::FINISH::BORDER, level_handler.level_finish_pink_y + SPRITES::SIZE - GAME::FINISH::HEIGHT, pink.get_x(), pink.get_y(), GAME::FINISH::WIDTH, GAME::FINISH::HEIGHT, SPRITES::SIZE, SPRITES::SIZE)) {
pink.set_can_jump();
}
}
// If players are already on finish, keep them locked
if (std::abs(blue.get_x() - level_handler.level_finish_blue_x) < GAME::FINISH::MIN_DISTANCE &&
std::abs(blue.get_y() - (level_handler.level_finish_blue_y - GAME::FINISH::HEIGHT)) < GAME::FINISH::MIN_DISTANCE &&
std::abs(pink.get_x() - level_handler.level_finish_pink_x) < GAME::FINISH::MIN_DISTANCE &&
std::abs(pink.get_y() - (level_handler.level_finish_pink_y - GAME::FINISH::HEIGHT)) < GAME::FINISH::MIN_DISTANCE) {
blue.set_x(level_handler.level_finish_blue_x);
blue.set_y(level_handler.level_finish_blue_y - GAME::FINISH::HEIGHT);
pink.set_x(level_handler.level_finish_pink_x);
pink.set_y(level_handler.level_finish_pink_y - GAME::FINISH::HEIGHT);
blue.set_finished();
pink.set_finished();
pink.reset_x_vel();
blue.reset_x_vel();
pink.reset_y_vel();
blue.reset_y_vel();
}
// Handle orb collisions
uint8_t old_orb_count = orb_count;
orb_count += level_handler.handle_orb_collisions(blue.get_x(), blue.get_y(), 0);
orb_count += level_handler.handle_orb_collisions(pink.get_x(), pink.get_y(), 1);
// If orb_count has changed, play sfx:
if (orb_count != old_orb_count) {
// Play orb collect sfx
audio_handler.play_sound(audio_handler.sound_samples.at(AUDIO::SFX::ORB));
}
// Handle spike collisions
if ((level_handler.handle_spike_collisions(blue.get_x(), blue.get_y() + SPRITES::SIZE - GAME::SQUARE::HEIGHT) ||
level_handler.handle_spike_collisions(pink.get_x(), pink.get_y() + SPRITES::SIZE - GAME::SQUARE::HEIGHT) ||
should_die) && !(blue.get_dead() || blue.get_dead())) {
// Blue or pink hit spikes
// Start square death animation
blue.set_dead();
pink.set_dead();
// Play death sfx
audio_handler.play_sound(audio_handler.sound_samples.at(AUDIO::SFX::DEATH));
}
// Check if square death animations have completed
if (blue.get_fade_finished() && pink.get_fade_finished()) {
// Reset position
//reset_players(level_handler);
// Reset all!
reset = true;
// Add to death count
death_count++;
}
return reset;
}
void Player::render(Spritesheet& spritesheet) {
blue.render(spritesheet);
pink.render(spritesheet);
}
//void Player::set_spawns(int blue_x, int blue_y, int pink_x, int pink_y) {
// blue = Square(0, blue_x, blue_y);
// pink = Square(4, pink_x, pink_y);
//}
void Player::reset_orb_count() {
//death_count = 0;
orb_count = 0;
}
void Player::reset_players(LevelHandler& level_handler) {
// Reset blue position
blue.set_x(level_handler.level_spawn_blue_x);
blue.set_y(level_handler.level_spawn_blue_y);
// Reset pink position
pink.set_x(level_handler.level_spawn_pink_x);
pink.set_y(level_handler.level_spawn_pink_y);
// Reset velocities
blue.reset_x_vel();
blue.reset_y_vel();
pink.reset_x_vel();
pink.reset_y_vel();
// Reset fade variables
blue.reset_dead_fade();
pink.reset_dead_fade();
}
uint8_t Player::get_death_count() {
return death_count;
}
uint8_t Player::get_orb_count() {
return orb_count;
}
float Player::get_blue_x() {
return blue.get_x();
}
float Player::get_blue_y() {
return blue.get_y();
}
float Player::get_pink_x() {
return pink.get_x();
}
float Player::get_pink_y() {
return pink.get_y();
}