-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
498 lines (443 loc) · 14.8 KB
/
player.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "player.h"
using namespace std;
using namespace sf;
Player::Player(short id, int life, Vector2f position):
Unit(life, 10, 10, position),
max_lives(3),
m_frameWidth(118),
m_frameHeight(93),
fireRate(0.1),
lastShot(0),
m_score(0),
lostLife(false),
missiles(false),
third(false),
secondary(false),
m_coefSpeed(40),
m_damages(5),
m_lives(3),
m_armor(3),
killThemAll(true),
exploding(false)
{
timer.start();
timerArmor.start();
timerKillThemAll.start();
image = new Image;
int start = 0;
*image = image_manager::getInstance()->getImage("images/player.png");
if(id == 1)
{
start = m_frameHeight;
m_position.x += -100;
}
m_anim.PushFrame(Frame(image, sf::Rect<int>(0, start,m_frameWidth,m_frameHeight+start)));
goRight.PushFrame(Frame(image, sf::Rect<int>(m_frameWidth,start,m_frameWidth*2,m_frameHeight+start)));
goRight.PushFrame(Frame(image, sf::Rect<int>(m_frameWidth*3,start,m_frameWidth*4,m_frameHeight+start)));
goLeft.PushFrame(Frame(image, sf::Rect<int>(m_frameWidth*2,start,m_frameWidth*3,m_frameHeight+start)));
goLeft.PushFrame(Frame(image, sf::Rect<int>(m_frameWidth*4,start,m_frameWidth*5,m_frameHeight+start)));
m_animated = new Animated(&m_anim, false, false, 0.4);
m_animated->SetPosition(m_position.x, m_position.y);
imageReactor = new Image;
*imageReactor = image_manager::getInstance()->getImage("images/reacteur.png");
m_animReactor.PushFrame(Frame(imageReactor, sf::Rect<int>(0, 0, 40, 100)));
m_animReactor.PushFrame(Frame(imageReactor, sf::Rect<int>(40, 0, 80, 100)));
m_animReactor.PushFrame(Frame(imageReactor, sf::Rect<int>(80, 0, 120, 100)));
m_animReactor.PushFrame(Frame(imageReactor, sf::Rect<int>(120, 0, 160, 100)));
m_animReactor.PushFrame(Frame(imageReactor, sf::Rect<int>(160, 0, 200, 100)));
m_animatedReactor = new Animated(&m_animReactor, true, true, 0.1);
m_animatedReactor->SetPosition(m_position.x, m_position.y);
*imageExplosion = image_manager::getInstance()->getImage("images/prendretarif.png");
int explo_width = imageExplosion->GetWidth()/5;
int explo_height= imageExplosion->GetHeight();
m_animExplosion.PushFrame(Frame(imageExplosion, sf::Rect<int>(0, 0, explo_width, explo_height) ));
m_animExplosion.PushFrame(Frame(imageExplosion, sf::Rect<int>(explo_width, 0, explo_width*2, explo_height) ));
m_animExplosion.PushFrame(Frame(imageExplosion, sf::Rect<int>(explo_width*2, 0, explo_width*3, explo_height) ));
m_animExplosion.PushFrame(Frame(imageExplosion, sf::Rect<int>(explo_width*3, 0, explo_width*4, explo_height) ));
m_animExplosion.PushFrame(Frame(imageExplosion, sf::Rect<int>(explo_width*4, 0, explo_width*5, explo_height) ));
m_animatedExplosion->SetAnim(&m_animExplosion);
m_animatedExplosion->Pause();
m_animatedExplosion->SetLoop(true);
m_animatedExplosion->SetFrameTime(0.5f);
m_animatedExplosion->SetPosition(position.x, position.y);
}
Player::~Player()
{
delete image;
delete imageReactor;
delete m_animatedReactor;
delete m_animated;
}
Sprite* Player::getSprite()
{
//Retourne la référence de sprite
return m_animated;
}
short Player::getLives()
{
//Retourne le nombre de vies restantes
return m_lives;
}
IntRect Player::GetBoundingBox()
{
//Récupère le rectangle de collision pour le joueur
IntRect boundingBox;
boundingBox.Left = (int)m_position.x;
boundingBox.Right = boundingBox.Left + m_frameWidth;
boundingBox.Top = (int)m_position.y;
boundingBox.Bottom = boundingBox.Top + m_frameHeight;
return boundingBox;
}
void Player::fire0()
{
if(canFire(lastShot, timer, fireRate))//Création des projectiles
{
//*******************************
//On créé le projectile du centre
//*******************************
Vector2f positionProjectile = m_position;
positionProjectile.x += 39;
positionProjectile.y -= 30;
string filepath;
Vector2f vitesse(0, -30);
if(Cheat_manager::getInstance()->getSopalintMode())
{
filepath = "images/projectile5.png";
vitesse.x = -15;
}
else
filepath = "images/projectile.png";
std::tr1::shared_ptr<Projectile> projectile(new Projectile(filepath, positionProjectile, Vector2f(0, -30), m_coefSpeed));
projectile->setPosition(positionProjectile);
Projectile_manager::getInstance()->addPlayerProjectile(projectile);
if(secondary)//Si les armes secondaires sont activées
{
//*******************************
//On créé le projectile de droite
//*******************************
positionProjectile.x += 19;
positionProjectile.y += 25;
std::tr1::shared_ptr<Projectile> projectileDroite(new Projectile(filepath, positionProjectile, Vector2f(0, -30), m_coefSpeed));
projectileDroite->setPosition(positionProjectile);
Projectile_manager::getInstance()->addPlayerProjectile(projectileDroite);
//*******************************
//On créé le projectile de gauche
//*******************************
positionProjectile.x -= 38;
std::tr1::shared_ptr<Projectile> projectileGauche(new Projectile(filepath, positionProjectile, Vector2f(0, -30), m_coefSpeed));
projectileGauche->setPosition(positionProjectile);
Projectile_manager::getInstance()->addPlayerProjectile(projectileGauche);
}
if(third)
{
//***************************************
//On créé le projectile de extreme droite
//***************************************
positionProjectile.x += 68;
positionProjectile.y += 5;
std::tr1::shared_ptr<Projectile> projectileExtremeGauche(new Projectile(filepath, positionProjectile, Vector2f(0, -30), m_coefSpeed));
projectileExtremeGauche->setPosition(positionProjectile);
Projectile_manager::getInstance()->addPlayerProjectile(projectileExtremeGauche);
//***************************************
//On créé le projectile de extreme gauche
//***************************************
positionProjectile.x -= 90;
std::tr1::shared_ptr<Projectile> projectileExtremeDroite(new Projectile(filepath, positionProjectile, Vector2f(0, -30), m_coefSpeed));
projectileExtremeDroite->setPosition(positionProjectile);
Projectile_manager::getInstance()->addPlayerProjectile(projectileExtremeDroite);
}
lastShot = timer.getTime();
}
}
void Player::fire1()
{
if(canFire(lastShot, timer, fireRate))
{
Vector2f positionProjectile = m_position;
positionProjectile.x += 39;
positionProjectile.y -= 30;
string filepath;
Vector2f vitesse(0, -30), vitesse21(10, -30), vitesse22(-10, -30), vitesse31(-30, -30), vitesse32(30, -30);
if(Cheat_manager::getInstance()->getSopalintMode())
{
filepath = "images/projectile5.png";
vitesse.y = -15;
vitesse21.x = -5;
vitesse21.y = -15;
vitesse31.x = 10;
vitesse31.y = -15;
vitesse22.x = 5;
vitesse22.y = -15;
vitesse32.x = -10;
vitesse32.y = -15;
}
else
{
filepath = "images/projectile.png";
}
std::tr1::shared_ptr<Projectile> projectile(new Projectile(filepath, positionProjectile, vitesse, m_coefSpeed));
projectile->setPosition(positionProjectile);
Projectile_manager::getInstance()->addPlayerProjectile(projectile);
if(secondary)
{
//*******************************
//On créé le projectile de droite
//*******************************
positionProjectile.x += 19;
positionProjectile.y += 25;
std::tr1::shared_ptr<Projectile> projectileDroite(new Projectile(filepath, positionProjectile, vitesse21, m_coefSpeed));
projectileDroite->rotate(-15);
projectileDroite->setPosition(positionProjectile);
Projectile_manager::getInstance()->addPlayerProjectile(projectileDroite);
//*******************************
//On créé le projectile de gauche
//*******************************
positionProjectile.x -= 38;
std::tr1::shared_ptr<Projectile> projectileGauche(new Projectile(filepath, positionProjectile, vitesse22, m_coefSpeed));
projectileGauche->setPosition(positionProjectile);
projectileGauche->rotate(15);
Projectile_manager::getInstance()->addPlayerProjectile(projectileGauche);
}
if(third)
{
//***************************************
//On créé le projectile de extreme droite
//***************************************
positionProjectile.x += 80;
positionProjectile.y += 5;
std::tr1::shared_ptr<Projectile> projectileExtremeGauche(new Projectile(filepath, positionProjectile, vitesse32, m_coefSpeed));
projectileExtremeGauche->setPosition(positionProjectile);
projectileExtremeGauche->rotate(-45);
Projectile_manager::getInstance()->addPlayerProjectile(projectileExtremeGauche);
//***************************************
//On créé le projectile de extreme gauche
//***************************************
positionProjectile.y += 25;
positionProjectile.x -= 115;
std::tr1::shared_ptr<Projectile> projectileExtremeDroite(new Projectile(filepath, positionProjectile, vitesse31, m_coefSpeed));
projectileExtremeDroite->setPosition(positionProjectile);
projectileExtremeDroite->rotate(45);
Projectile_manager::getInstance()->addPlayerProjectile(projectileExtremeDroite);
}
lastShot = timer.getTime();
}
}
bool canFire(float lastShot, Timer &timer, float const fireRate)
{
//Créé un interval de temps entre chaque tir
if(timer.getTime() - lastShot > fireRate)
{
timer.reinitialize();
return true;
}
else
return false;
}
void Player::addScore(int score)
{
m_score += score;
}
int Player::getScore()
{
return m_score;
}
void Player::loseLive()
{
if(m_armor > 0)
{
if(timerArmor.getTime() > 2)
{
m_armor--;
timerArmor.reinitialize();
}
}
else
{
Vector2f position(500, 1000);
m_position = position;
m_lives -= 1;
lostLife = true;
m_armor = 3;
exploding = true;
}
}
bool Player::getLostlife()
{
return lostLife;
}
void Player::resetLostLife()
{
lostLife = false;
}
void Player::moveUp()
{
if (m_animated->GetAnim() != &m_anim)
{
m_animated->SetAnim(&m_anim);
m_animated->SetLoop(false);
}
m_position.y -= m_ySpeed * app.GetFrameTime() * coefSpeed;
m_animated->SetPosition(m_position.x, m_position.y);
if(m_animated->IsPaused())
m_animated->Play();
}
void Player::moveDown()
{
if (m_animated->GetAnim() != &m_anim)
{
m_animated->SetAnim(&m_anim);
m_animated->SetLoop(true);
}
m_position.y += m_ySpeed * app.GetFrameTime() * coefSpeed;
m_animated->SetPosition(m_position.x, m_position.y);
if(m_animated->IsPaused())
m_animated->Play();
}
void Player::moveLeft()
{
if (m_animated->GetAnim() != &goLeft)
{
m_animated->SetAnim(&goLeft);
m_animated->SetLoop(false);
if(m_animated->IsPaused())
m_animated->Play();
}
else if(m_animated->GetAnim() == &goLeft && m_animated->GetCurrentFrame() == 1)
{
m_animated->Pause();
}
m_position.x -= m_xSpeed * app.GetFrameTime() * coefSpeed;
m_animated->SetPosition(m_position.x, m_position.y);
}
void Player::moveRight()
{
if (m_animated->GetAnim() != &goRight)
{
m_animated->SetLoop(false);
m_animated->SetAnim(&goRight);
if(m_animated->IsPaused())
m_animated->Play();
}
else if(m_animated->GetAnim() == &goRight && m_animated->GetCurrentFrame() == 1)
{
m_animated->Pause();
}
m_position.x += m_xSpeed * app.GetFrameTime() * coefSpeed;
m_animated->SetPosition(m_position.x, m_position.y);
}
void Player::dontMove()
{
if (m_animated->GetAnim() != &m_anim)
{
m_animated->SetAnim(&m_anim);
m_animated->SetLoop(false);
}
if(m_animated->IsPaused())
m_animated->Play();
}
void Player::draw()
{
if(Cheat_manager::getInstance()->getSopalintMode())
{
m_lives = 3;
m_armor = 3;
}
this->checkKTA();
if(!this->isDead())
{
m_animatedReactor->SetPosition(m_animated->GetPosition().x + m_frameWidth/2.8, m_animated->GetPosition().y + m_frameHeight/1.35);
m_animatedReactor->anim(app.GetFrameTime());
app.Draw(*m_animatedReactor);
m_animated->anim(app.GetFrameTime());
app.Draw(*m_animated);
}
if(Cheat_manager::getInstance()->getSopalintMode() && m_score < 10000)
{
m_score = 100000;
}
}
int Player::getPosition(int axis)
{
if(axis == 0)
return m_position.x;
else
return m_position.y;
}
Vector2f Player::getPosition()
{
return m_position;
}
void Player::setPosition(int axis, int value)
{
if(axis == 1)
m_position.x = value;
else
m_position.y = value;
}
short Player::getDamages() const
{
return m_damages;
}
bool Player::getMissile()
{
return missiles;
}
bool Player::getSecondary()
{
return secondary;
}
bool Player::getThird()
{
return third;
}
void Player::setMissile(bool mode)
{
missiles = mode;
}
void Player::setSecondary(bool mode)
{
secondary = mode;
}
void Player::setThird(bool mode)
{
third = mode;
}
short Player::getArmor()
{
return m_armor;
}
bool Player::getPlayerKTA()
{
return killThemAll;
}
void Player::setPlayerKTA(bool state)
{
killThemAll = state;
}
void Player::checkKTA()
{
if(!killThemAll && timerKillThemAll.getTime() > 120)
{
killThemAll = true;
timerKillThemAll.reinitialize();
}
}
bool Player::isDead()
{
if(m_lives == 0)
return true;
return false;
}
void Player::explosion()
{
if(exploding)
{
m_animatedExplosion->SetPosition(m_position);
m_animatedExplosion->anim(app.GetFrameTime());
m_animatedExplosion->Play();
int currentFrame = m_animatedExplosion->GetCurrentFrame();
app.Draw(*m_animatedExplosion);
if(currentFrame > 4)
exploding = false;
}
}