-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButton.cpp
More file actions
56 lines (44 loc) · 1.41 KB
/
Button.cpp
File metadata and controls
56 lines (44 loc) · 1.41 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
#include "Button.hpp"
Button::Button() : Entity() {
}
Button::Button(uint16_t sprite_index, float x, float y, uint8_t type) : Entity(sprite_index, x, y) {
this->type = type;
}
void Button::update(float dt) {
if (release_timer > 0.0f) {
release_timer -= dt;
if (release_timer <= 0.0f) {
release_timer = 0.0f;
pressed = false;
}
}
}
void Button::render(Spritesheet& spritesheet) {
spritesheet.sprite_scaled(sprite_index + (pressed ? 1 : 0), x, y);
}
void Button::set_pressed(uint8_t type) {
if (type == this->type) {
pressed = true;
release_timer = 0.0f;
}
}
void Button::set_released(uint8_t type) {
if (type == this->type && release_timer == 0.0f) {
release_timer = GAME::BUTTON::RELEASE_DELAY;
}
}
bool Button::get_pressed() {
return pressed;
}
bool Button::check_collision(float player_x, float player_y) {
return player_x < x + GAME::BUTTON::BORDER + GAME::BUTTON::WIDTH && player_x + SPRITES::SIZE > x + GAME::BUTTON::BORDER && player_y < y + SPRITES::SIZE && player_y + SPRITES::SIZE > get_top();
}
bool Button::check_on_top(float player_x, float player_y) {
return player_x < x + GAME::BUTTON::BORDER + GAME::BUTTON::WIDTH && player_x + SPRITES::SIZE > x + GAME::BUTTON::BORDER && player_y + SPRITES::SIZE == get_top();
}
float Button::get_top() {
return y + SPRITES::SIZE - (pressed ? GAME::BUTTON::HEIGHT_PRESSED : GAME::BUTTON::HEIGHT_RELEASED);
}
uint8_t Button::get_type() {
return type;
}