-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroidManager.cpp
119 lines (93 loc) · 3.33 KB
/
AsteroidManager.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
/*
Author: JoseGuilhermeCR
Copyright 2017
*/
#include "include/AsteroidManager.h"
#include "include/Utils.h"
AsteroidManager::AsteroidManager(unsigned int maxAsteroidNumber)
:
m_asteroids(),
m_currentAsteroidNumber(0),
m_maxAsteroidNumber(maxAsteroidNumber)
{
m_asteroids.reserve(maxAsteroidNumber);
}
void AsteroidManager::update(float deltaTime) {
if (m_currentAsteroidNumber < m_maxAsteroidNumber)
{
unsigned int asteroidToAdd = m_maxAsteroidNumber - m_currentAsteroidNumber;
for (unsigned int i = 0; i < asteroidToAdd; ++i)
{
float x = Utils::randomRange(0, Utils::WIDTH);
float y = Utils::randomRange(0, Utils::HEIGHT);
float r = Utils::randomRange(35, 60);
float total = Utils::randomRange(5, 15);
std::vector<int> offsets;
offsets.reserve(total);
for (unsigned int i = 0; i < total; ++i)
{
int offset = Utils::randomRange(-10, 10);
offsets.push_back(offset);
}
// Random velocity to the asteroid
float velX = Utils::randomRange(-25, 25);
float velY = Utils::randomRange(-25, 25);
// A random rotation value to the asteroid
float rotation = Utils::randomRange(0, 180);
m_asteroids.push_back(Asteroid(x, y, velX, velY, r, total, rotation, offsets));
++m_currentAsteroidNumber;
}
}
for (unsigned int i = 0; i < m_asteroids.size(); ++i)
{
m_asteroids.at(i).update(deltaTime);
if (m_asteroids.at(i).shouldDelete())
{
m_asteroids.erase(m_asteroids.begin() + i);
--i;
--m_currentAsteroidNumber;
}
}
}
void AsteroidManager::draw(sf::RenderWindow& window)
{
for (unsigned int i = 0; i < m_asteroids.size(); ++i)
{
m_asteroids.at(i).draw(window);
}
}
void AsteroidManager::spawnChildAsteroid(unsigned int index)
{
m_asteroids.at(index).setToDelete(true);
float radius = m_asteroids.at(index).getR();
if (radius / 2 < 10)
return;
float x = m_asteroids.at(index).getX() + Utils::randomRange(-radius, radius);
float y = m_asteroids.at(index).getY() + Utils::randomRange(-radius, radius);
float r = radius * 0.5f;
float total = Utils::randomRange(5, 15);
std::vector<int> offsets;
offsets.reserve(total);
for (unsigned int i = 0; i < total; ++i)
{
int offset = Utils::randomRange(-5, 10);
offsets.push_back(offset);
}
// Random velocity to the asteroid
float velX = Utils::randomRange(-25, 25);
float velY = Utils::randomRange(-25, 25);
// A random rotation value to the asteroid
float rotation = Utils::randomRange(0, 270);
m_asteroids.push_back(Asteroid(x, y, velX, velY, r, total, rotation, offsets));
m_asteroids.push_back(Asteroid(x + Utils::randomRange(-r, r), y + Utils::randomRange(-r, r), -velX, -velY, r, total, -rotation, offsets));
m_currentAsteroidNumber += 2;
}
std::vector<Asteroid>& AsteroidManager::getAsteroids()
{
return m_asteroids;
}
void AsteroidManager::reset()
{
m_currentAsteroidNumber = 0;
m_asteroids.clear();
}