-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteor.cpp
114 lines (103 loc) · 3.49 KB
/
meteor.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
#include <cmath>
#include "meteor.h"
#include "gamedata.h"
#include "frameFactory.h"
#include "explodingPlayer.h"
#include "SDL/SDL_rotozoom.h"
float getVariation(const std::string& name, const char *s, int min, int max)
{
float variation = Gamedata::getInstance().getRandInRange(min,max);
variation *= (rand()%2 == 0? -1: 1);
return Gamedata::getInstance().getXmlInt(name+s)+variation;
}
Meteor::Meteor(const std::string& name) :
Drawable(name,
Vector2f(getVariation(name,"/startLoc/x",
Gamedata::getInstance().getXmlInt(name+"/minDist"),
Gamedata::getInstance().getXmlInt(name+"/maxDist")
),
Gamedata::getInstance().getXmlInt(name+"/startLoc/y")
),
Vector2f(Gamedata::getInstance().getXmlInt(name+"/speedX"),
Gamedata::getInstance().getXmlInt(name+"/speedY")
)
),
explosion(NULL),
zoom(Gamedata::getInstance().getRandFloat( 0.2, 1.0 )),
frame( FrameFactory::getInstance().getFrame(name) ),
frameWidth(frame->getWidth()),
frameHeight(frame->getHeight()),
worldWidth(Gamedata::getInstance().getXmlInt("world/width")),
worldHeight(Gamedata::getInstance().getXmlInt("world/height")),
hasExploded( false )
{
Vector2f change = getVelocity()*zoom; //This step was done in the constructor as an assignment statement because the zoom factor which is a part of this class can
setVelocity( change ); //only be initialized after the base class members are initialized thus preventing us from directly initializing the velocity
//according to the size of the meteor to produce the depth effect.
}
Meteor::Meteor(const Meteor& s) :
Drawable(s),
explosion( s.explosion ),
zoom( s.zoom ),
frame(s.frame),
frameWidth(s.getFrame()->getWidth()),
frameHeight(s.getFrame()->getHeight()),
worldWidth(Gamedata::getInstance().getXmlInt("world/width")),
worldHeight(Gamedata::getInstance().getXmlInt("world/height")),
hasExploded( s.hasExploded )
{ }
void Meteor::draw() const {
if (explosion)
{
explosion->draw();
return;
}
if( !hasExploded )
{
Uint32 x = static_cast<Uint32>(X());
Uint32 y = static_cast<Uint32>(Y());
SDL_Surface* surf = frame->getSurface();
surf = zoomSurface(surf, zoom, zoom, SMOOTHING_ON);
std::string name = "meteor";
Frame* immediate = new Frame(name, surf);
immediate->draw(x, y);
delete immediate;
SDL_FreeSurface(surf);
}
}
void Meteor::explode() {
if ( explosion ) return;
Sprite sprite(getName(), getPosition(), getVelocity(), getFrame());
explosion = new ExplodingPlayer(sprite);
}
void Meteor::reset(){
if( hasExploded )
{
hasExploded = false;
}
}
int Meteor::getDistance(const Meteor *obj) const {
return hypot(X()-obj->X(), Y()-obj->Y());
}
void Meteor::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 ( X() < 0) {
velocityX( abs( velocityY() ) );
}
if ( X() > worldWidth-frameWidth) {
velocityX( -abs( velocityX() ) );
}
if ( Y() > worldHeight-frameHeight) {
Y(0); //wraparound on X-axis
X( Gamedata::getInstance().getRandInRange(0, worldWidth-frameWidth) );
}
}