-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite.cpp
109 lines (98 loc) · 2.98 KB
/
sprite.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
#include <cmath>
#include "sprite.h"
#include "gamedata.h"
#include "frameFactory.h"
#include "explodingSprite.h"
Sprite::Sprite(const std::string& name) :
Drawable(name,
Vector2f(Gamedata::getInstance().getXmlInt(name+"/startLoc/x"),
Gamedata::getInstance().getXmlInt(name+"/startLoc/y")),
Vector2f(Gamedata::getInstance().getXmlInt(name+"/speedX"),
Gamedata::getInstance().getXmlInt(name+"/speedY"))
),
explosion(NULL),
frame( FrameFactory::getInstance().getFrame(name) ),
frameWidth(frame->getWidth()),
frameHeight(frame->getHeight()),
worldWidth( Gamedata::getInstance().getXmlInt("world/width")),
worldHeight( Gamedata::getInstance().getXmlInt("world/height"))
{ }
Sprite::Sprite(const string& n, const Vector2f& pos, const Vector2f& vel):
Drawable(n, pos, vel),
explosion(NULL),
frame( FrameFactory::getInstance().getFrame(n) ),
frameWidth(frame->getWidth()),
frameHeight(frame->getHeight()),
worldWidth( Gamedata::getInstance().getXmlInt("world/width") ),
worldHeight( Gamedata::getInstance().getXmlInt("world/height") )
{ }
Sprite::Sprite(const string& n, const Vector2f& pos, const Vector2f& vel,
const Frame* frm):
Drawable(n, pos, vel),
explosion(NULL),
frame( frm ),
frameWidth(frame->getWidth()),
frameHeight(frame->getHeight()),
worldWidth( Gamedata::getInstance().getXmlInt("world/width") ),
worldHeight( Gamedata::getInstance().getXmlInt("world/height") )
{ }
Sprite::Sprite(const Sprite& s) :
Drawable(s),
explosion(NULL),
frame(s.frame),
frameWidth(s.getFrame()->getWidth()),
frameHeight(s.getFrame()->getHeight()),
worldWidth( Gamedata::getInstance().getXmlInt("world/width") ),
worldHeight( Gamedata::getInstance().getXmlInt("world/height") )
{ }
void Sprite::draw() const {
if(explosion){
explosion->draw();
return;
}
Uint32 x = static_cast<Uint32>(X());
Uint32 y = static_cast<Uint32>(Y());
frame->draw(x, y);
}
int Sprite::getDistance(const Sprite *obj) const {
return hypot(X()-obj->X(), Y()-obj->Y());
}
void Sprite::update(Uint32 ticks) {
if(explosion){
explosion->update(ticks);
if(explosion->chunkCount() == 0){
delete explosion;
explosion = NULL;
}
return;
}
Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001;
setPosition(getPosition() + incr);
if ( Y() < 0) {
velocityY( abs( velocityY() ) );
}
if ( Y() > worldHeight-frameHeight) {
velocityY( -abs( velocityY() ) );
}
if ( X() < 0) {
velocityX( abs( velocityX() ) );
}
if ( X() > worldWidth-frameWidth) {
velocityX( -abs( velocityX() ) );
}
}
Sprite& Sprite::operator=(const Sprite& rhs) {
if(this == &rhs){
return *this;
}
Drawable::operator=(rhs);
frameWidth = rhs.frameWidth;
frameHeight = rhs.frameHeight;
worldHeight = rhs.worldHeight;
worldWidth = rhs.worldWidth;
return *this;
}
void Sprite::explode(){
if(explosion) return;
explosion = new ExplodingSprite(*this);
}