-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfmaw_character.fds
154 lines (119 loc) · 4.14 KB
/
fmaw_character.fds
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
// Copyright 2015 Lluís Ulzurrun de Asanza Sàez
#include <vector>
#include <functional>
#include "./fmaw_character.h"
#include "./FMAW.h"
#define ANIMATION_STEP_TIME 5
namespace FMAW {
void Character::init() {}
void Character::setXPosition(FixedReal x) {
this->x = x;
}
void Character::setYPosition(FixedReal y) {
this->y = y;
}
void Character::setPosition(FixedReal x, FixedReal y) {
this->x = x;
this->y = y;
}
void Character::setPosition(Point position) {
this->x = FixedReal(position.x, 8);
this->y = FixedReal(position.y, 8);
}
FixedReal Character::getXPosition() {
return this->x;
}
FixedReal Character::getYPosition() {
return this->y;
}
animation_id Character::animateToPosition(Point position,
unsigned int duration) {
return this->animateToPosition(position, duration, NULL);
}
animation_id Character::animateToPosition(Point position, unsigned int duration,
std::function<void(bool)> callback) {
return this->animateToPosition(position, duration, ATLinear, callback);
}
animation_id Character::animateToPosition(Point position, unsigned int duration,
AnimationType type,
std::function<void(bool)> callback) {
animation_id id = this->animations.size();
std::function<void(int)> func = [this, id](int callback_id) {
this->animations[id].remainingDuration -= ANIMATION_STEP_TIME;
if (this->animations[id].remainingDuration <= 0) {
// Set final position.
this->setPosition(this->animations[id].finalPosition);
// Dequeue enqueued function.
Timer::dequeue_function(callback_id);
// Call completion callback.
if (this->animations[id].callback != NULL)
this->animations[id].callback(true);
} else {
Point finalPosition = this->animations[id].finalPosition;
Point initialPosition = this->animations[id].initialPosition;
// Set position computed on the fly.
int deltaX = finalPosition.x - initialPosition.x;
int deltaY = finalPosition.y - initialPosition.y;
int deltaDuration = this->animations[id].duration -
this->animations[id].remainingDuration;
FixedReal deltaTimePercent = FixedReal(deltaDuration, 8);
FixedReal dtd = FixedReal(static_cast<int>
(this->animations[id].duration),
8);
deltaTimePercent = deltaTimePercent / dtd;
this->setXPosition(deltaTimePercent * deltaX + initialPosition.x);
this->setYPosition(deltaTimePercent * deltaY + initialPosition.y);
}
};
int callback_id = Timer::enqueue_function(func, ANIMATION_STEP_TIME, true);
Animation animation = {
id,
callback_id,
this->getPosition(),
position,
duration,
duration,
type,
callback
};
this->animations[id] = animation;
return id;
}
bool Character::cancelAnimation(animation_id id) {
if (this->animations.find(id) != this->animations.end()) {
Timer::dequeue_function(this->animations[id].callback_id);
if (this->animations[id].callback != NULL)
this->animations[id].callback(false);
this->animations.erase(id);
return true;
}
return false;
}
Point Character::getPosition() {
Point p {this->x, this->y};
return p;
}
void Character::update() {}
void Character::render() {
this->render(FixedReal(8), FixedReal(8));
}
void Character::render(FixedReal camera_x, FixedReal camera_y) {
FixedReal x, y;
x = (this->x - FixedReal(this->width, 8)) - camera_x;
y = (this->y - FixedReal(this->height, 8)) - camera_y;
if (x <= FixedReal(-16, 8) || y <= FixedReal(-16, 8) ||
x >= FixedReal(256, 8) || y >= FixedReal(192, 8)) {
this->sprite.disable();
return;
}
this->sprite.setPosition(x, y);
}
void Character::print() {
FMAW::printf("Character using sprite:");
this->sprite.print();
}
Character::~Character() {
this->sprite.disable();
this->sprite.clear();
}
} // namespace FMAW