-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntity.cpp
More file actions
96 lines (71 loc) · 1.73 KB
/
Entity.cpp
File metadata and controls
96 lines (71 loc) · 1.73 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
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
#include "Entity.hpp"
//constexpr float sum_animation_frame_arr(AnimationFrame frames[], uint8_t length) {
// float total = 0.0f;
//
// for (uint8_t i = 0; i < length; i++) {
// total += frames[i].first;
// }
//
// return total;
//}
Entity::Entity() {
sprite_index = 0;
x = y = 0.0f;
x_vel = y_vel = 0.0f;
}
Entity::Entity(uint16_t sprite_index, float x, float y) {
this->sprite_index = sprite_index;
this->x = x;
this->y = y;
x_vel = y_vel = 0.0f;
}
void Entity::update(float dt) {
}
void Entity::render(Spritesheet& spritesheet) {
spritesheet.sprite_scaled(sprite_index, x, y);
}
float Entity::get_x() {
return x;
}
float Entity::get_y() {
return y;
}
void Entity::set_x(float x) {
this->x = x;
}
void Entity::set_y(float y) {
this->y = y;
}
// AnimatedEntity?
// has Frame::IDLE, WALK, FALL, JUMP etc? then render() picks best?
AnimationHandler::AnimationHandler() {
current_frame = 0;
animation_timer = 0.0f;
}
AnimationHandler::AnimationHandler(AnimationFrames animation_frames) {
this->animation_frames = animation_frames;
current_frame = 0;
animation_timer = 0.0f;
}
void AnimationHandler::update(float dt) {
// Increment timer
animation_timer += dt;
// Check if we need to increment current_frame
if (animation_timer >= animation_frames[current_frame].second) {
animation_timer -= animation_frames[current_frame].second;
current_frame++;
// Reset current_frame to 0 if we've reached the end of the animation
if (current_frame == animation_frames.size()) {
current_frame = 0;
}
}
}
void AnimationHandler::reset() {
// Reset timer
animation_timer = 0.0f;
// Reset current frame
current_frame = 0;
}
uint16_t AnimationHandler::get_sprite_index() {
return animation_frames[current_frame].first;
}