-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMario.cpp
240 lines (224 loc) · 4.8 KB
/
Mario.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
/*!
* \file Mario.cpp
* \author Ekinox <[email protected]>
*/
#include "Mario.h"
#include <fstream>
#include <SFML/Graphics.hpp>
#include "config.h"
#include "Collision.h"
#include "Map.h"
#include "Tile.h"
#include "TileAttributes.h"
/*!
* \param PosX The position of Mario on the X axis
* \param PosY The position of Mario on the Y axis
* \param map The map on which Mario is evolving
*/
Mario::Mario(const size_t PosX, const size_t PosY, const Map &map,
const sf::Uint16 Lifes) :
AnimatedSprite(),
myIsGoingLeft(false), myIsGoingRight(false), myIsJumping(false),
myIsRunning(false), mySpeedY(0), myMap(&map), myReturnPosX(PosX
* TILES_WIDTH), myReturnPosY((PosY - 1) * TILES_HEIGHT),
mySize("small"), myLifes(Lifes), myLost(false), myState("stand")
{
std::ifstream properties("images/mario.properties");
Load(properties);
SetPosition(PosX * TILES_WIDTH, (PosY - 1) * TILES_HEIGHT);
}
/*!
* \brief Sets the return position of Mario (if it dies).
*/
void Mario::SetReturnPos(float X, float Y)
{
myReturnPosX = X;
myReturnPosY = Y;
}
/*!
* \brief Mario dies
*/
void Mario::Die()
{
if (myLifes <= 1)
myLost = true;
else
{
--myLifes;
SetPosition(myReturnPosX, myReturnPosY);
}
}
/*!
* \brief Has Mario lost ?
*/
bool Mario::Lost() const
{
return myLost;
}
/*!
* \brief Mario goes to the right
*/
void Mario::GoRight()
{
if (!myIsGoingRight)
{
myIsGoingRight = true;
FlipX(false);
myState = "walk";
}
}
/*!
* \brief Mario goes to the left
*/
void Mario::GoLeft()
{
if (!myIsGoingLeft)
{
myIsGoingLeft = true;
FlipX(true);
myState = "walk";
}
}
/*!
* \brief Mario stops going to the left
*/
void Mario::StopGoingLeft()
{
myIsGoingLeft = false;
myState = "stand";
}
/*!
* \brief Mario stops going to the right
*/
void Mario::StopGoingRight()
{
myIsGoingRight = false;
myState = "stand";
}
/*!
* \brief Makes Mario jumping
*/
void Mario::Jump()
{
if (myMap->BottomMax(*this, 1) == 0)
{
mySpeedY = -6.;
myIsJumping = true;
myState = "jump";
}
}
/*!
* \brief Mario starts running
*/
void Mario::Run()
{
myIsRunning = true;
}
/*!
* \brief Mario stops running
*/
void Mario::StopRunning()
{
myIsRunning = false;
}
/*!
* \brief It updates the Mario
*
* \author Ekinox <[email protected]>, iSma
*/
void Mario::Update()
{
AnimatedSprite::Update();
// Test if Mario is died
if (GetPosition().y > (myMap->GetMaxY() + 1) * TILES_HEIGHT)
Die();
// Compute Y move
mySpeedY += GRAVITATION;
if (mySpeedY < 0)
{
float i = myMap->TopMax(*this, -mySpeedY);
if (i == 0)
mySpeedY = 0;
else
Move(0, -i);
}
else if (mySpeedY > 0)
{
float i = myMap->BottomMax(*this, mySpeedY);
if (i == 0)
{
mySpeedY = 0;
myIsJumping = false;
}
else
Move(0, i);
}
// Compute X move
if (myIsRunning)
{
if (myIsGoingLeft && !myIsGoingRight)
Move(-myMap->LeftMax(*this, 4), 0);
else if (myIsGoingRight && !myIsGoingLeft)
Move(+myMap->RightMax(*this, 4), 0);
}
else
{
if (myIsGoingLeft && !myIsGoingRight)
Move(-myMap->LeftMax(*this, 1.7), 0);
else if (myIsGoingRight && !myIsGoingLeft)
Move(+myMap->RightMax(*this, 1.7), 0);
}
// Compute state
if (myIsJumping)
{
myState = "jump";
}
else if (myIsGoingLeft || myIsGoingRight)
{
myState = "walk";
if (myIsRunning)
framesPerSecond = 12;
else
framesPerSecond = 6;
}
else
{
myState = "stand";
}
std::string newAnim = mySize + "." + myState;
if (newAnim != currentAnimation->name)
SetAnimation(newAnim);
}
/*!
* \return The view centered on Mario if possible
*/
sf::View Mario::GetView() const
{
// Compute the view centered on Mario
float l = GetPosition().x + (GetSize().x / 2) - (WINDOW_WIDTH / 2), r = l
+ WINDOW_WIDTH, t = GetPosition().y + (GetSize().y / 2)
- (WINDOW_HEIGHT / 2), b = t + WINDOW_HEIGHT;
// Re-center (if out of the limits of the map)
if (l < 0)
{
l = 0;
r = WINDOW_WIDTH;
}
if (t < 0)
{
t = 0;
b = WINDOW_HEIGHT;
}
if (r > (myMap->GetMaxX() + 1) * TILES_WIDTH)
{
r = (myMap->GetMaxX() + 1) * TILES_HEIGHT;
l = r - WINDOW_WIDTH;
}
if (b > (myMap->GetMaxY() + 1) * TILES_HEIGHT)
{
b = (myMap->GetMaxY() + 1) * TILES_HEIGHT;
t = b - WINDOW_HEIGHT;
}
// Return the view
return sf::View(sf::FloatRect(l, t, r, b));
}