-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOrb.cpp
62 lines (48 loc) · 1.55 KB
/
Orb.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
#include "Orb.hpp"
Orb::Orb() : Entity() {
}
Orb::Orb(uint16_t sprite_index, uint8_t type, float x, float y) : Entity(sprite_index, x, y) {
this->type = type;
}
void Orb::update(float dt) {
if (collected) {
fade_age += dt;
}
}
void Orb::render(Spritesheet& spritesheet) {
if (collected) {
if (fade_age < GAME::ORB::FADE::MAX_AGE) {
// Calculate age ratio
float age_ratio = (fade_age / GAME::ORB::FADE::MAX_AGE);
// Get alpha from age
uint8_t alpha = (1.0f - age_ratio) * 0xFF;
// Get scale from age
float scale = age_ratio * GAME::ORB::FADE::MAX_SCALE_BOOST + spritesheet.get_scale();
// Calculate scale ratio
float scale_ratio = spritesheet.get_scale() / scale;
// Get offset to keep sprites centered
float offset = SPRITES::SIZE * (scale - spritesheet.get_scale()) / 4.0f;
//float offset = spritesheet.get_sprite_size() * (scale - spritesheet.get_scale()) / 4.0f;
//float offset = (spritesheet.get_sprite_size() / 2.0f) * (scale - spritesheet.get_scale()) / 2.0f; // Same as line above but less optimized.
uint8_t old_alpha = spritesheet.get_alpha();
spritesheet.set_alpha(alpha);
spritesheet.sprite(sprite_index, (x - offset) * scale_ratio, (y - offset) * scale_ratio, scale, 0.0f, NULL, SDL_FLIP_NONE);
spritesheet.set_alpha(old_alpha);
}
}
else {
Entity::render(spritesheet);
}
}
void Orb::set_collected() {
collected = true;
}
bool Orb::get_collected() {
return collected;
}
bool Orb::get_finished() {
return collected && fade_age >= GAME::ORB::FADE::MAX_AGE;
}
uint8_t Orb::get_type() {
return type;
}