-
Notifications
You must be signed in to change notification settings - Fork 1
/
twowayMultisprite.cpp
89 lines (79 loc) · 2.8 KB
/
twowayMultisprite.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
#include <iostream>
#include <cmath>
#include "twowayMultisprite.h"
#include "gamedata.h"
unsigned TwowayMultiframeSprite::getPixel(Uint32 i, Uint32 j) const {
Uint32 x = static_cast<Uint32>(X());
Uint32 y = static_cast<Uint32>(Y());
x = i - x;
y = j - y;
Uint32 *pixels = static_cast<Uint32 *>(frames[currentFrame]->getSurface()->pixels);
return pixels[ ( y * frames[currentFrame]->getWidth() ) + x ];
}
void TwowayMultiframeSprite::advanceFrame(Uint32 ticks) {
float ms = 1000.0/frameInterval;
dt += ticks;
int df = dt / ms;
dt -= df * ms;
currentFrame = (currentFrame + df) % numberOfFrames;
}
TwowayMultiframeSprite::TwowayMultiframeSprite( const std::string& name,
std::vector<Frame*> & fmsLeft,
std::vector<Frame*> & fmsRight, const float sc) :
Drawable(name,
Vector2f(Gamedata::getInstance()->getXmlInt(name+"X"),
Gamedata::getInstance()->getXmlInt(name+"Y")),
Vector2f(Gamedata::getInstance()->getXmlInt(name+"SpeedX"),
Gamedata::getInstance()->getXmlInt(name+"SpeedY")),
sc
),
framesLeft(fmsLeft),
framesRight(fmsRight),
frames(fmsRight),
frameWidth(framesLeft[0]->getWidth()),
frameHeight(framesLeft[0]->getHeight()),
worldWidth(Gamedata::getInstance()->getXmlInt("worldWidth")),
worldHeight(Gamedata::getInstance()->getXmlInt("worldHeight")),
dt(0),
currentFrame(0),
numberOfFrames( Gamedata::getInstance()->getXmlInt(name+"Frames") ),
frameInterval( Gamedata::getInstance()->getXmlInt(name+"FrameInterval") )
{ }
TwowayMultiframeSprite::
TwowayMultiframeSprite(const TwowayMultiframeSprite& s) :
Drawable(s.getName(), s.getPosition(), s.getVelocity(), s.getScale()),
framesLeft(s.framesLeft),
framesRight(s.framesRight),
frames(s.framesRight),
frameWidth(s.getFrame()->getWidth()),
frameHeight(s.getFrame()->getHeight()),
worldWidth(Gamedata::getInstance()->getXmlInt("worldWidth")),
worldHeight(Gamedata::getInstance()->getXmlInt("worldHeight")),
dt(s.dt),
currentFrame(s.currentFrame),
numberOfFrames( s.numberOfFrames ),
frameInterval( s.frameInterval )
{ }
void TwowayMultiframeSprite::draw() const {
Uint32 x = static_cast<Uint32>(X());
Uint32 y = static_cast<Uint32>(Y());
frames[currentFrame]->draw(x, y);
}
void TwowayMultiframeSprite::update(Uint32 ticks) {
if ( getVelocity().magnitude() > 1.0 ) {
advanceFrame(ticks);
}
Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001;
setPosition(getPosition() + incr);
if ( Y() < 0) {
velocityY( abs( velocityY() ) );
}
if ( X() < 0) {
velocityX( abs( velocityX() ) );
}
if ( X() > worldWidth-frameWidth) {
velocityX( -abs( velocityX() ) );
}
if ( velocityX() < 0 ) frames = framesLeft;
if ( velocityX() > 0 ) frames = framesRight;
}