-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanim.cpp
353 lines (294 loc) · 10.2 KB
/
anim.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "anim.h"
#include "tinyxml.h"
#include "SDL_image.h"
#include "game_map.h"
#include <math.h>
void AnimObj::
set_direction(int sx, int sy, int next_x, int next_y, int tw, int th)
{
/* if (UserState *next = PeekSolutionNext()) {
}
else*/
{
next_x *= tw; next_x += tw/2;
next_y *= th; next_y += th/2;
}
distance_ = sqrt((sx - next_x) * (sx - next_x) + (sy - next_y) * (sy - next_y));
// std::cerr << "Distance between (" << sx << ',' << sy << ") and (" << next_x << ',' << next_y << ") is " << distance_ << ".\n";
double delta_step = distance_ / speed_;
microstep_x_ = ((double)(next_x - sx)) / delta_step;
microstep_y_ = ((double)(next_y - sy)) / delta_step;
if (microstep_x_ > 0.0 && microstep_y_ > 0.0) {
if (microstep_x_ > microstep_y_)
Set("walk_right");
else
Set("walk_down");
}
else if (microstep_x_ < 0.0 && microstep_y_ < 0.0) {
if (microstep_y_ > microstep_x_)
Set("walk_left");
else
Set("walk_up");
}
else if (microstep_x_ < 0.0 && microstep_y_ > 0.0) {
if (microstep_x_ < -microstep_y_)
Set("walk_left");
else
Set("walk_down");
}
else {
if (microstep_x_ > -microstep_y_)
Set("walk_right");
else
Set("walk_up");
}
}
void AnimObj::
do_step(uint32_t now)
{
if (!step_)
return;
int tw = MapSearchNode::RefMap->TileWidth(),
th = MapSearchNode::RefMap->TileHeight();
double delta = (now - last_) / 1000.0;
last_ = now;
int actual_x = base_x(),
actual_y = base_y();
if (distance_ <= 0.0) {
step_ = astar_.GetSolutionNext();
// std::cerr << "Next step:";
if (!step_) {
// std::cerr << "GOAL REACHED\n";
// arrived!
if (microstep_x_ > 0 && microstep_y_ > 0) {
if (microstep_y_ > microstep_x_)
Set("idle_down");
else
Set("idle_right");
}
else if (microstep_x_ < 0 && microstep_y_ < 0) {
if (microstep_y_ > microstep_x_)
Set("idle_left");
else
Set("idle_up");
}
else if (microstep_x_ < 0 && microstep_y_ > 0) {
if (microstep_x_ < -microstep_y_)
Set("idle_left");
else
Set("idle_down");
}
else if (microstep_x_ > -microstep_y_)
Set("idle_right");
else
Set("idle_up");
return;
}
// step_->PrintNodeInfo();
set_direction(actual_x, actual_y, step_->x, step_->y, tw, th);
}
/*
std::cerr << "Dir: " << direction_ << " X:" << actual_x << " Y:" << actual_y << " d:" << delta
<< " distance:" << distance_
<< " dx:" << microstep_x_ << " dy:" << microstep_y_ <<'\n';
*/
distance_ -= delta * speed_;
/*
if we are arrived to the next step I force it to be in the EXACT position <- not needed
if (distance_ <= 0.0) {
x_ = step_->x * tw + tw/2 + actual_x - x_;
y_ = step_->y * th + th/2 + actual_y - y_;
}
*/
x_ += delta * microstep_x_;
y_ += delta * microstep_y_;
}
bool AnimObj::
go_to(int x, int y, uint32_t msec)
{
int tw = MapSearchNode::RefMap->TileWidth(),
th = MapSearchNode::RefMap->TileHeight();
distance_ = 0.0;
astar_.FreeSolutionNodes();
astar_.EnsureMemoryFreed();
MapSearchNode ns(base_x() /tw , base_y() /th);
MapSearchNode ne(x / tw, y / th);
astar_.SetStartAndGoalStates(ns, ne);
/*
std::cerr << "Start: ";
ns.PrintNodeInfo();
std::cerr << "End: ";
ne.PrintNodeInfo();
*/
unsigned int SearchState;
unsigned int SearchSteps = 0;
do {
if (SearchSteps > 600) {
// std::cerr << "Search interrupted. Did not find goal state\n";
astar_.CancelSearch();
}
SearchState = astar_.SearchStep();
SearchSteps++;
} while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING );
if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED ) {
step_ = astar_.GetSolutionStart();
// always skip first step, it's the position we are in
step_ = astar_.GetSolutionNext();
if (step_)
set_direction(base_x(), base_y(), step_->x, step_->y, tw, th);
}
else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED )
{
// std::cerr << "Search terminated. Did not find goal state\n";
step_ = NULL;
}
// std::cerr << "SearchSteps : " << SearchSteps << "\n";
last_ = msec;
return step_ != NULL;
}
bool AnimObj::
Set(const std::string &anim)
{
AnimCIt it = animations_.find(anim);
if (it == animations_.end())
return false;
if (actual_ == anim)
return true;
actual_ = anim;
frame_ = 0;
next_frame_ = it->second.frames[0].persistence + SDL_GetTicks();
return true;
}
void AnimObj::
parse_animations(const TiXmlNode *baseNode)
{
for (const TiXmlNode *animNode = baseNode->FirstChild("anim"); animNode;
animNode = baseNode->IterateChildren("anim", animNode)) {
int s;
const TiXmlElement* e = animNode->ToElement();
Anim anim;
anim.name = e->Attribute("id");
e->Attribute("sheet", &s);
SheetCIt it = sheets_.find(s);
if (it == sheets_.end())
throw std::string("Unavailable sheet for animation " + anim.name);
anim.sheet = it->second;
const TiXmlNode *frameNode = animNode->FirstChild("frame");
while (frameNode) {
Frame frame;
const TiXmlElement *e = frameNode->ToElement();
int x, y, w, h;
e->Attribute("x", &x);
e->Attribute("y", &y);
e->Attribute("w", &w);
e->Attribute("h", &h);
frame.box.x = x;
frame.box.y = y;
frame.box.w = w;
frame.box.h = h;
e->Attribute("time", &frame.persistence);
anim.frames.push_back(frame);
frameNode = animNode->IterateChildren("frame", frameNode);
}
animations_[anim.name] = anim;
if (actual_.empty())
actual_ = anim.name;
}
}
AnimObj::AnimObj(const std::string &xmlname) : frame_(0), step_(NULL)
{
TiXmlDocument doc(xmlname);
if (!doc.LoadFile())
throw std::string("Unable to load animobj from " + xmlname + ": " + doc.ErrorDesc());
if (doc.Error())
throw std::string("Error parsing " + xmlname + ": " + doc.ErrorDesc());
TiXmlNode *baseNode = doc.FirstChild("character");
if (!baseNode)
throw std::string("Invalid XML " + xmlname);
if (const TiXmlNode *n = baseNode->FirstChild("name"))
name_ = n->ToElement()->GetText();
if (const TiXmlNode *n = baseNode->FirstChild("speed"))
speed_ = atof(n->ToElement()->GetText());
else
speed_ = 100.0f; // speed of 100 to objects with undefined speed
for (const TiXmlNode *sheetNode = baseNode->FirstChild("sheet"); sheetNode;
sheetNode = baseNode->IterateChildren("sheet", sheetNode)) {
int id;
const TiXmlElement* e = sheetNode->ToElement();
e->Attribute("id", &id);
std::map<std::string, std::string> layers;
for (const TiXmlNode *layer = sheetNode->FirstChild("layer"); layer;
layer = sheetNode->IterateChildren("layer", layer)) {
const TiXmlElement* e = layer->ToElement();
layers[e->Attribute("id")] = e->GetText();
}
if (SDL_Surface *s = load_sheets(layers))
sheets_[id] = s;
else
throw std::string("Unable to create animation sheet for " + xmlname);
std::cerr << "Loaded sheet " << id << " for " << xmlname << " with " << layers.size() << " layers\n";
}
// for each "animation" instance execute the parse of the file contained in the tag src
for (const TiXmlNode *n = baseNode->FirstChild("animation"); n; n = baseNode->IterateChildren("animation", n)) {
const TiXmlElement* e = n->ToElement();
std::string name = e->Attribute("src");
TiXmlDocument anim_doc(name);
if (anim_doc.LoadFile()) {
if (TiXmlNode *an = anim_doc.FirstChild("animation"))
parse_animations(an);
}
}
// executing the parse of the local animations
parse_animations(baseNode);
std::cerr << "Loaded " << animations_.size() << " animations for " << xmlname << '\n';
next_frame_ = SDL_GetTicks();
}
void AnimObj::
update_animation(uint32_t now)
{
Anim &a = animations_[actual_];
if (now >= next_frame_) {
frame_++;
if (frame_ >= a.frames.size())
frame_ = 0;
next_frame_ += a.frames[frame_].persistence;
}
}
bool AnimObj::
on_screen(int mapx, int mapy) const
{
AnimCIt it = animations_.find(actual_);
if (it != animations_.end())
return Object::on_screen(it->second.frames[frame_].box, mapx, mapy);
return false;
}
void AnimObj::
blit(int x, int y) const
{
AnimCIt it = animations_.find(actual_);
if (it != animations_.end())
lowblit(it->second.sheet, it->second.frames[frame_].box, x, y);
}
SDL_Surface *AnimObj::
load_sheets(const std::map<std::string, std::string> &sheets)
{
SDL_Surface *temp = NULL;
for (std::map<std::string, std::string>::const_iterator it = sheets.begin();
it != sheets.end(); ++it) {
SDL_Surface *b = IMG_Load(it->second.c_str());
if (!b)
throw std::string("Unable to load character sheet layer id " + it->first + ", filename " + it->second);
if (!temp)
temp = b;
else {
if (b->w != temp->w ||
b->h != temp->h)
throw std::string("Invalid layer " + it->first + ", filename " + it->second);
SDL_BlitSurface(b, NULL, temp, NULL);
SDL_FreeSurface(b);
}
}
SDL_Surface * bitmap = SDL_DisplayFormatAlpha(temp);
SDL_FreeSurface(temp);
return bitmap;
}