-
Notifications
You must be signed in to change notification settings - Fork 0
/
explodingSprite.cpp
60 lines (54 loc) · 2 KB
/
explodingSprite.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
#include <iostream>
#include <cmath>
#include "explodingSprite.h"
#include "framefactory.h"
ExplodingSprite::ExplodingSprite(const Sprite& s) :
SingleFrameSprite(s.getName(),s.getNumber(),s.getPosition(),s.getVelocity()),
chunks(),
freeList(),
frames() {
setFrame(s.getFrame());
makeChunks();
}
ExplodingSprite::~ExplodingSprite() {
chunks.clear();
freeList.clear();
}
void ExplodingSprite::draw() const {
std::list<Chunk>::const_iterator ptr = chunks.begin();
while (ptr != chunks.end()) {
ptr->draw();
++ptr;
}
}
void ExplodingSprite::update(Uint32 ticks) {
std::list<Chunk>::iterator ptr = chunks.begin();
while (ptr != chunks.end()) {
ptr->update(ticks);
if (ptr->goneTooFar()) {
freeList.push_back(*ptr);
ptr = chunks.erase(ptr);
}
else ++ptr;
}
}
void ExplodingSprite::makeChunks() {
unsigned int chunk_width = getFrame()->getWidth()/Gamedata::getInstance()->getXmlInt(getName()+"ChunkNumberOfFrames");
unsigned int chunk_height = getFrame()->getHeight()/Gamedata::getInstance()->getXmlInt(getName()+"ChunkNumberOfFramesY");
int speedx = static_cast<int>(velocityX())+1; // Make sure it's not 0;
int speedy = static_cast<int>(velocityY())+1; // Make sure it's an int.
frames = FrameFactory::getInstance().getFrame(getName(),"Chunk");
for (int j = 0; j < Gamedata::getInstance()->getXmlInt(getName()+"ChunkNumberOfFramesY"); ++j) {
for (int i = 0; i < Gamedata::getInstance()->getXmlInt(getName()+"ChunkNumberOfFrames"); ++i) {
float sx = (rand() % speedx + 40) * (rand()%2?-1:1); // 'cause %0 is
float sy = (rand() % speedy + 40) * (rand()%2?-1:1); // float except
Chunk chunk(
Vector2f(X()+i*chunk_width, // x coord of destination
Y()+j*chunk_height), // y coord of destination
Vector2f(sx, sy),
getName()+"Chunk",
frames[j*Gamedata::getInstance()->getXmlInt(getName()+"ChunkNumberOfFramesY")+i]);
chunks.push_back(chunk);
}
}
}