-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatchraw.patch
More file actions
76 lines (68 loc) · 2.67 KB
/
patchraw.patch
File metadata and controls
76 lines (68 loc) · 2.67 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
diff --git a/Asteroids/gameState.cpp b/Asteroids/gameState.cpp
index 9fc6e90..f5788d6 100644
--- a/Asteroids/gameState.cpp
+++ b/Asteroids/gameState.cpp
@@ -21,12 +21,13 @@ void gameState::setup()
void gameState::addAsteroid()
{
- asteroidVector.push_back(new gameObject(gameObjectType::asteroid, 0.09f));
- asteroidVector.back()->getBody()->setAngularVelocity(angularVelocity / 30);
+ auto asteroid = std::make_unique<gameObject>(gameObjectType::asteroid, 0.09f);
+ asteroid->getBody()->setAngularVelocity(angularVelocity / 30);
float travelHeading = rand() % 360 / 180.0f * pi; //0-359 degrees in radian fly direction
vec2 asteroidForwardDirection = { cosf(travelHeading), sinf(travelHeading) };
- asteroidVector.back()->getBody()->setLinearVelocity(asteroidForwardDirection * (maxLinearVelocity / 3));
- asteroidVector.back()->getBody()->setPos(asteroidSpawnCoords[spawnCoordCounter % 4]);
+ asteroid->getBody()->setLinearVelocity(asteroidForwardDirection * (maxLinearVelocity / 3));
+ asteroid->getBody()->setPos(asteroidSpawnCoords[spawnCoordCounter % 4]);
+ asteroidVector.push_back(std::move(asteroid));
++spawnCoordCounter; //next asteroid spawns at next position defined in vector
}
@@ -58,9 +59,10 @@ void gameState::update(float dt)
}
ship.getBody()->confineTo(area);
- for (auto it = asteroidVector.begin(); it != asteroidVector.end(); it++) {
- (*it)->getBody()->update(dt);
- (*it)->getBody()->confineTo(area);
+ for (auto& asteroid : asteroidVector)
+ {
+ asteroid->getBody()->update(dt);
+ asteroid->getBody()->confineTo(area);
}
ufo.getBody()->update(dt);
@@ -90,13 +92,15 @@ void gameState::update(float dt)
shipStateTimeStamp = 0;
ship.setInvincible(false);
}
- }else
+ }
+ else
{
this->collision = checkCollision(ufo, ship);
if (!this->collision)
{
- for (auto it = asteroidVector.begin(); it != asteroidVector.end(); it++) {
- if (checkCollision((**it), ship))
+ for (auto& asteroid : asteroidVector)
+ {
+ if (checkCollision(*asteroid, ship))
{
this->collision = true;
break;
diff --git a/Asteroids/gameState.h b/Asteroids/gameState.h
index 16126e4..c0a79ac 100644
--- a/Asteroids/gameState.h
+++ b/Asteroids/gameState.h
@@ -4,6 +4,7 @@
#include "gameObject.h"
#include <math.h>
#include <vector>
+#include <memory>
static const float pi = acosf(-1.0f);
@@ -14,7 +15,7 @@ struct gameState
gameObject ship = { gameObjectType::ship, 0.04f };
gameObject ufo = { gameObjectType::ufo, 0.04f };
- std::vector<gameObject*> asteroidVector;
+ std::vector<std::unique_ptr<gameObject>> asteroidVector;
//asteroid spawn and last spawn coord counter
std::vector<vec2> asteroidSpawnCoords = { {0,-1}, {-1,0}, {0,1}, {1,0} };