-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpring.cpp
More file actions
57 lines (41 loc) · 1.59 KB
/
Spring.cpp
File metadata and controls
57 lines (41 loc) · 1.59 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
#include "Spring.hpp"
Spring::Spring() : Entity() {
}
Spring::Spring(uint16_t sprite_index, float x, float y) : Entity(sprite_index, x, y) {
}
void Spring::update(float dt) {
// launch_player is only set for 1 frame
launch_player = false;
just_changed_frame = false;
animation_timer += dt;
if (animation_timer >= GAME::SPRING::ANIMATION[current_frame_offset].second) {
animation_timer -= GAME::SPRING::ANIMATION[current_frame_offset].second;
current_frame_offset++;
just_changed_frame = true;
if (current_frame_offset == GAME::SPRING::FRAME_COUNT) {
current_frame_offset = 0;
}
else if (current_frame_offset == GAME::SPRING::LAUNCH_FRAME) {
// Launch player upwards (unfortunately happens next frame)
launch_player = true;
}
}
}
void Spring::render(Spritesheet& spritesheet) {
spritesheet.sprite_scaled(GAME::SPRING::ANIMATION[current_frame_offset].first, x, y);
}
bool Spring::should_launch() {
return launch_player;
}
bool Spring::check_collision(float player_x, float player_y) {
return player_x < x + GAME::SPRING::BORDER + GAME::SPRING::WIDTH && player_x + SPRITES::SIZE > x + GAME::SPRING::BORDER && player_y < y + SPRITES::SIZE && player_y + SPRITES::SIZE > get_top();
}
bool Spring::check_on_top(float player_x, float player_y){
return player_x < x + GAME::SPRING::BORDER + GAME::SPRING::WIDTH && player_x + SPRITES::SIZE > x + GAME::SPRING::BORDER && player_y + SPRITES::SIZE == get_top();
}
float Spring::get_top() {
return y + SPRITES::SIZE - GAME::SPRING::ANIMATION_HEIGHTS[current_frame_offset];
}
bool Spring::get_just_changed_frame() {
return just_changed_frame;
}