Skip to content

Commit c133e8c

Browse files
committed
updated rush00
1 parent 8700677 commit c133e8c

16 files changed

+235
-43
lines changed

rush00/Collection/ACollection.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class ACollection
1414
virtual ~ACollection(void);
1515
int getSize(void) const;
1616
AObject *get(int index) const;
17-
virtual void updateObjects(void) = 0;
1817
bool checkElapsedTime(double waitTimeMs);
1918

2019
protected:
@@ -23,4 +22,4 @@ class ACollection
2322
clock_t lastUpdate;
2423
};
2524

26-
#endif
25+
#endif

rush00/Collection/AsteroidsCollection.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
AsteroidsCollection::AsteroidsCollection(void)
55
{
6-
this->size = 20;
6+
this->size = 40;
77
this->data = new Asteroid[this->size];
88
};
99

@@ -14,7 +14,7 @@ AsteroidsCollection::AsteroidsCollection (const AsteroidsCollection &copy){
1414
void AsteroidsCollection::updateObjects() {
1515
Asteroid *obj;
1616

17-
if (!this->checkElapsedTime(100))
17+
if (!this->checkElapsedTime(600))
1818
return;
1919

2020
// parcours de tous les object pour les desactiver si ils sont sortis de l'ecran
@@ -35,6 +35,37 @@ void AsteroidsCollection::updateObjects() {
3535
}
3636
}
3737

38+
bool AsteroidsCollection::checkCollision(Pilot *pilot)
39+
{
40+
AObject *asteroid;
41+
AObject *rocket;
42+
int rx, ry, ax, ay;
43+
44+
for (int i = 0; i < this->getSize(); i++) {
45+
asteroid = this->get(i);
46+
if (!asteroid->getEnabled())
47+
continue;
48+
ax = asteroid->getPosition().x;
49+
ay = asteroid->getPosition().y;
50+
if (ay == pilot->getPosition().y && (ax == pilot->getPosition().x || ax+1 == pilot->getPosition().x || ax+2 == pilot->getPosition().x || ax+3 == pilot->getPosition().x))
51+
return true;
52+
for (int i = 0; i < pilot->getRockets().getSize(); i++) {
53+
rocket = pilot->getRockets().get(i);
54+
if (!rocket->getEnabled())
55+
continue;
56+
rx = rocket->getPosition().x;
57+
ry = rocket->getPosition().y;
58+
if (ry == ay && (rx == ax || rx == ax+1 || rx == ax+2 || rx == ax+3) ) {
59+
rocket->setPosition(-1, -1);
60+
rocket->setEnabled(false);
61+
Engine::ammo++;
62+
break;
63+
}
64+
}
65+
}
66+
return false;
67+
}
68+
3869
AsteroidsCollection & AsteroidsCollection::operator=(const AsteroidsCollection &copy) {
3970
if (this != &copy){
4071
this->data = copy.data;

rush00/Collection/AsteroidsCollection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# include "ACollection.hpp"
55
# include "../Object/Asteroid.hpp"
6+
# include "../Object/Pilot.hpp"
67

78
class AsteroidsCollection: public ACollection
89
{
@@ -12,6 +13,7 @@ class AsteroidsCollection: public ACollection
1213
AsteroidsCollection &operator=(const AsteroidsCollection &copy);
1314
~AsteroidsCollection(void);
1415
void updateObjects(void);
16+
bool checkCollision(Pilot *pilot);
1517

1618
};
1719

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "EnRockCollection.hpp"
2+
#include "../Engine.hpp"
3+
4+
EnRockCollection::EnRockCollection(void) {
5+
6+
this->size = 10;
7+
this->data = new Rocket[this->size];
8+
};
9+
10+
EnRockCollection::EnRockCollection(const EnRockCollection &copy) {
11+
*this = copy;
12+
};
13+
14+
void EnRockCollection::fire(int y, int x) {
15+
Rocket *rkt;
16+
17+
for (int i = 0; i < this->size; i++) {
18+
rkt = (Rocket*)&this->data[i];
19+
if (!rkt->getEnabled()) {
20+
rkt->setPosition(y, x);
21+
rkt->setEnabled(true);
22+
return;
23+
}
24+
}
25+
}
26+
27+
void EnRockCollection::updateObjects(int maxHeight) {
28+
Rocket *obj;
29+
30+
if (!this->checkElapsedTime(100))
31+
return;
32+
33+
// parcours de tous les object pour les desactiver si ils sont sortis de l'ecran
34+
// et les faire bouger le cas echeant
35+
for (int i = 0; i < this->size; i++) {
36+
obj = (Rocket*)&this->data[i];
37+
if (obj->getEnabled()) {
38+
if (obj->getPosition().y > maxHeight) {
39+
obj->setEnabled(false);
40+
}
41+
}
42+
obj->demove();
43+
}
44+
}
45+
46+
47+
bool EnRockCollection::checkCollision(Pilot *pilot) {
48+
AObject *e_rocket;
49+
AObject *rocket;
50+
51+
for (int i = 0; i < this->getSize(); i++) {
52+
e_rocket = (Rocket*)&this->data[i];
53+
if (!e_rocket->getEnabled())
54+
continue;
55+
for (int i = 0; i < pilot->getRockets().getSize(); i++) {
56+
rocket = pilot->getRockets().get(i);
57+
if (!rocket->getEnabled())
58+
continue;
59+
if (rocket->getPosition().y == e_rocket->getPosition().y && rocket->getPosition().x == e_rocket->getPosition().x) {
60+
e_rocket->setPosition(-1, -1);
61+
e_rocket->setEnabled(false);
62+
rocket->setPosition(-1, -1);
63+
rocket->setEnabled(false);
64+
Engine::ammo++;
65+
break;
66+
}
67+
}
68+
if (e_rocket->getPosition().y == pilot->getPosition().y && e_rocket->getPosition().x == pilot->getPosition().x) {
69+
return true;
70+
}
71+
}
72+
return false;
73+
}
74+
75+
EnRockCollection & EnRockCollection::operator=(const EnRockCollection &copy) {
76+
this->data = copy.data;
77+
this->size = copy.size;
78+
this->lastUpdate = copy.lastUpdate;
79+
return *this;
80+
};
81+
82+
EnRockCollection::~EnRockCollection(void) {
83+
delete [] this->data;
84+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef ENROCKCOLLECTION_HPP
2+
# define ENROCKCOLLECTION_HPP
3+
4+
# include "ACollection.hpp"
5+
# include "../Object/Rocket.hpp"
6+
# include "../Object/Pilot.hpp"
7+
8+
class EnRockCollection: public ACollection
9+
{
10+
public:
11+
EnRockCollection(void);
12+
EnRockCollection(const EnRockCollection &copy);
13+
EnRockCollection &operator=(const EnRockCollection &copy);
14+
~EnRockCollection(void);
15+
void updateObjects(int maxHeight);
16+
void fire(int y, int x);
17+
bool checkCollision(Pilot *pilot);
18+
};
19+
20+
#endif

rush00/Collection/EnemiesCollection.cpp

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ void EnemiesCollection::updateObjects() {
1616

1717
if (!this->checkElapsedTime(300))
1818
return;
19-
2019
// parcours de tous les object pour les desactiver si ils sont sortis de l'ecran
2120
// et les faire bouger le cas echeant
2221
for (int i = 0; i < this->size; i++) {
@@ -25,16 +24,61 @@ void EnemiesCollection::updateObjects() {
2524
if (obj->getPosition().y > Engine::maxHeight) {
2625
obj->setEnabled(false);
2726
}
27+
obj->move();
2828
} else {
29-
if (arc4random() % 100 < 1) { // le faire que dans 1% des cas
29+
// on active un enemy
30+
if (arc4random() % 100 < 20) { // le faire que dans 1% des cas
3031
obj->setPosition(arc4random() % Engine::maxHeight - Engine::maxHeight, ((arc4random() % Engine::maxWidth) / 2) * 2);
3132
obj->setEnabled(true);
3233
}
3334
}
34-
obj->move();
3535
}
3636
}
3737

38+
void EnemiesCollection::maybeShoot(EnRockCollection &e_rockets) {
39+
Enemy *obj;
40+
41+
for (int i = 0; i < this->getSize(); i++) {
42+
obj = (Enemy*)&this->data[i];
43+
if (obj->getEnabled()) {
44+
if (arc4random() % 10000 < 1) {
45+
e_rockets.fire(obj->getPosition().y+1, obj->getPosition().x);
46+
}
47+
}
48+
}
49+
50+
}
51+
52+
bool EnemiesCollection::checkCollision(Pilot *pilot, int *score) {
53+
AObject *enemy;
54+
AObject *rocket;
55+
56+
for (int i = 0; i < this->getSize(); i++) {
57+
enemy = (Enemy*)&this->data[i];
58+
if (!enemy->getEnabled())
59+
continue;
60+
for (int i = 0; i < pilot->getRockets().getSize(); i++) {
61+
rocket = pilot->getRockets().get(i);
62+
if (!rocket->getEnabled())
63+
continue;
64+
// si la rocket est sur un ennemi
65+
if (rocket->getPosition().y == enemy->getPosition().y && rocket->getPosition().x == enemy->getPosition().x) {
66+
(*score)++;
67+
enemy->setPosition(-1, -1);
68+
enemy->setEnabled(false);
69+
rocket->setPosition(-1, -1);
70+
rocket->setEnabled(false);
71+
Engine::ammo++;
72+
break;
73+
}
74+
}
75+
if (enemy->getPosition().y == pilot->getPosition().y && enemy->getPosition().x == pilot->getPosition().x) {
76+
return true;
77+
}
78+
}
79+
return false;
80+
}
81+
3882
EnemiesCollection & EnemiesCollection::operator=(const EnemiesCollection &copy) {
3983
if (this != &copy){
4084
this->data = copy.data;
@@ -46,4 +90,4 @@ EnemiesCollection & EnemiesCollection::operator=(const EnemiesCollection &copy)
4690

4791
EnemiesCollection::~EnemiesCollection(void) {
4892
delete [] this->data;
49-
}
93+
}

rush00/Collection/EnemiesCollection.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# include "ACollection.hpp"
55
# include "../Object/Enemy.hpp"
6+
# include "EnRockCollection.hpp"
7+
# include "../Object/Pilot.hpp"
68

79
class EnemiesCollection: public ACollection
810
{
@@ -12,7 +14,8 @@ class EnemiesCollection: public ACollection
1214
EnemiesCollection &operator=(const EnemiesCollection &copy);
1315
~EnemiesCollection(void);
1416
void updateObjects(void);
15-
17+
void maybeShoot(EnRockCollection &e_rockets);
18+
bool checkCollision(Pilot *pilot, int *score);
1619
};
1720

1821
#endif

rush00/Collection/RocketsCollection.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "RocketsCollection.hpp"
2+
#include "../Engine.hpp"
23

34
RocketsCollection::RocketsCollection(void) {
45

5-
this->size = 40;
6+
this->size = 20;
67
this->data = new Rocket[this->size];
78
};
89

@@ -36,6 +37,7 @@ void RocketsCollection::updateObjects() {
3637
if (obj->getEnabled()) {
3738
if (obj->getPosition().y < 0) {
3839
obj->setEnabled(false);
40+
Engine::ammo++;
3941
}
4042
}
4143
obj->move();
@@ -51,4 +53,4 @@ RocketsCollection & RocketsCollection::operator=(const RocketsCollection &copy)
5153

5254
RocketsCollection::~RocketsCollection(void) {
5355
delete [] this->data;
54-
};
56+
};

0 commit comments

Comments
 (0)