-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsteroids.cpp
More file actions
178 lines (141 loc) · 4.14 KB
/
Asteroids.cpp
File metadata and controls
178 lines (141 loc) · 4.14 KB
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
#include "Asteroids.h"
#include "Messages_defs.h"
#include "ServiceLocator.h"
#include "Logger.h"
#include <sstream>
Asteroids::Asteroids(SDLGame* game) : GameObjectPool(game), rotating_(5),
asteroidImage_(game->getServiceLocator()->getTextures()->getTexture(Resources::Asteroid)),
numAsteroids_(10)
{
srand(time(0));
initAst_ = numAsteroids_;
for (auto o : getAllObjects()) {
o->addC(&rotating_);
o->addC(&naturalMove_);
o->addC(&asteroidImage_);
o->addC(&showUpAtOppositeSide_);
}
}
Asteroids::~Asteroids() {}
void Asteroids::InitAsteroids() {
//Crea tantos asteroides como se pidan
numAsteroids_ = initAst_;
for (int i = 0; i < numAsteroids_; i++) {
if (getUnusedObject() != nullptr) {
Asteroid *a = getUnusedObject();
a->setActive(true);
a->setPosition(randPos());
Vector2D c = Vector2D(getGame()->getWindowWidth() / 2, getGame()->getWindowHeight() / 2);
Vector2D v = (c - a->getPosition()).normalize() * ((rand() % 10 + 1) / 20.0);
a->setVelocity(v);
a->setHeight(20);
a->setWidth(20);
a->setGenerations(3);
//Logger
callLogger(a->getPosition(), a->getVelocity());
}
}
setId(msg::Asteroids);
setActive(false);
}
Vector2D Asteroids::randPos() {
Vector2D p = Vector2D(0, 0);
int aux = rand() % 4;
switch (aux) {
//Arriba
case 0:
p = Vector2D(rand() % this->getGame()->getWindowWidth(), rand() % 50);
break;
//Abajo
case 1:
p = Vector2D(rand() % this->getGame()->getWindowWidth(), rand() % 50 + (this->getGame()->getWindowHeight() - 50));
break;
//Izq
case 2:
p = Vector2D(rand() % 50, rand() % this->getGame()->getWindowHeight());
break;
//Der
case 3:
p = Vector2D(rand() % 50 + (this->getGame()->getWindowWidth() - 50), rand() % this->getGame()->getWindowHeight());
break;
default:
break;
}
return p;
}
void Asteroids::GenerateSonAsteroid(Asteroid* father) {
for (int i = 0; i < 2; i++) {
if (getUnusedObject() != nullptr) {
Asteroid *a = getUnusedObject();
a->setActive(true);
//Vel
Vector2D v = father->getVelocity();
v.setX(v.getX() * 1.1);
v.setY(v.getY() * 1.1);
a->setVelocity(v);
//Pos
a->setPosition(father->getPosition() + v + Vector2D(i * 10, 0));
a->setRotation(i * 30);
//Size
a->setHeight(father->getHeight() * 0.75);
a->setWidth(father->getWidth() * 0.75);
//Generations
a->setGenerations(father->getGenerations() - 1);
//Logger
callLogger(a->getPosition(), a->getVelocity());
}
}
}
void Asteroids::callLogger(Vector2D p, Vector2D v) {
Logger::instance()->log([p, v]() {
stringstream s;
s << "New asteroid: " << p << " " << v;
return s.str();
});
}
void Asteroids::receive(const void* senderObj, const msg::Message& m) {
Container::receive(senderObj, m);
switch (m.type_) {
case msg::GAME_START:
this->getGame()->send(this, msg::AsteroidsInfo(getId(), msg::Broadcast, &getAllObjects()));
break;
case msg::ROUND_START:
InitAsteroids();
setActive(true);
break;
case msg::ROUND_OVER:
this->deactiveAllObjects();
setActive(false);
break;
case msg::BULLET_ASTEROID_COLLISION: {
// Desactivamos el asteroide
const msg::BulletAsteroidCollision m_ = static_cast<const msg::BulletAsteroidCollision&>(m);
m_.asteroid_->setActive(false);
numAsteroids_--;
//sonido explosion por canal 2
this->getGame()->getServiceLocator()->getAudios()->playChannel(Resources::Explosion, 0, 2);
// Mandar mensaje
this->getGame()->send(this, msg::AsteroidDestroyed(getId(), msg::Broadcast, 4 - m_.asteroid_->getGenerations()));
// Generar 2 hijos
if (m_.asteroid_->getGenerations() > 1) {
GenerateSonAsteroid(m_.asteroid_);
numAsteroids_ += 2;
}
// Comprobamos el numero de asteroides
if (numAsteroids_ == 0) {
this->getGame()->send(this, msg::Message(msg::NO_MORE_ASTEROIDS, getId(), msg::Broadcast));
this->getGame()->getServiceLocator()->getAudios()->playMusic(Resources::Cheer, 3); //sonido de victoria
}
//numero asteroides
cout << numAsteroids_ << endl;
break;
}
case msg::ASTEROID_HOLE_COLLISION: {
const msg::AsteroidHoleCollision m_ = static_cast<const msg::AsteroidHoleCollision&>(m);
m_.asteroid_->setPosition(randPos());
break;
}
default:
break;
}
}