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
|
/*********************************************************************
* File: game.cpp
* Description: Contains the implementaiton of the game class
* methods.
*
*********************************************************************/
#include "game.h"
#include "uiDraw.h"
#include "bullet.h"
// These are needed for the getClosestDistance function...
#include <limits>
#include <iostream>
#include <algorithm>
#define CLOSE_ENOUGH 15
using namespace std;
// Game non-default constructor
Game :: Game ( const Point &tl , const Point &br ) : topLeft ( tl ) , bottomRight ( br )
{
rocks = createRocks();
ship = new Ship ( Point() );
}
// Create rocks for the game
vector<Rock *> Game :: createRocks()
{
vector<Rock *> initRocks;
for ( int i = 0; i < random ( 5 , 8 ); i++ )
{
Point rockPoint = Point ( random( -190 , 190 ) , random ( -190 , 190 ) );
Velocity velocity;
velocity.setDx ( random ( -3.0 , 3.0 ) );
velocity.setDy ( random ( -2.0 , 2.0 ) );
// Make sure spawn is safe on start by calculating if
// The rock is on a trajectory to the ship
float m = velocity.getDy() / velocity.getDx();
if ( fabs ( rockPoint.getY() - m * rockPoint.getX() ) >= 50 )
{
switch ( random( 1 , 5 ) ) {
case 1:
case 2:
case 3:
initRocks.push_back ( new BigRock ( rockPoint , velocity ) );
break;
case 4:
initRocks.push_back ( new MediumRock ( rockPoint , velocity ) );
break;
case 5:
initRocks.push_back ( new SmallRock ( rockPoint , velocity ) );
break;
default:
break;
}
}
}
return initRocks;
}
// Get closest distance between two flying objects
float Game :: getClosestDistance(const FlyingObject &obj1, const FlyingObject &obj2) const
{
// find the maximum distance traveled
float dMax = max(abs(obj1.getVelocity().getDx()), abs(obj1.getVelocity().getDy()));
dMax = max(dMax, abs(obj2.getVelocity().getDx()));
dMax = max(dMax, abs(obj2.getVelocity().getDy()));
dMax = max(dMax, 0.1f); // when dx and dy are 0.0. Go through the loop once.
float distMin = std::numeric_limits<float>::max();
for (float i = 0.0; i <= dMax; i++)
{
Point point1(obj1.getPoint().getX() + (obj1.getVelocity().getDx() * i / dMax),
obj1.getPoint().getY() + (obj1.getVelocity().getDy() * i / dMax));
Point point2(obj2.getPoint().getX() + (obj2.getVelocity().getDx() * i / dMax),
obj2.getPoint().getY() + (obj2.getVelocity().getDy() * i / dMax));
float xDiff = point1.getX() - point2.getX();
float yDiff = point1.getY() - point2.getY();
float distSquared = (xDiff * xDiff) +(yDiff * yDiff);
distMin = min(distMin, distSquared);
}
return sqrt(distMin);
}
// Advance everything in the game
void Game :: advance()
{
cleanUpZombies();
advanceShip();
advanceBullets();
advanceAsteroids();
handleCollisions();
}
// Advance all asteroids in game
void Game :: advanceAsteroids()
{
for ( int i = 0; i < rocks.size(); i++ )
{
rocks[i]->advance();
}
}
// Advance all the bullets
void Game :: advanceBullets()
{
for ( int i = 0; i < bullets.size(); i++ )
{
bullets[i]->advance();
}
}
// Advance the ship
void Game :: advanceShip()
{
if ( ship->isAlive() && ship->getFuel() >= 0 )
{
ship->advance();
}
}
// Clean up memory
void Game :: cleanUpZombies()
{
for ( int i = 0; i < bullets.size(); i++ )
{
if ( !bullets[i]->isAlive() )
{
delete bullets[i];
bullets[i] = NULL;
bullets.erase( bullets.begin() + i );
}
}
for ( int i = 0; i < rocks.size(); i++ )
{
if ( !rocks[i]->isAlive() )
{
delete rocks[i];
rocks[i] = NULL;
rocks.erase( rocks.begin() + i );
}
}
}
// Handle all collisions
void Game :: handleCollisions()
{
// Handle bullet collisions
for ( int i = 0; i < rocks.size(); i++ )
{
Rock * rock = rocks[i];
if ( rock->isAlive() )
{
for ( int j = 0; j < bullets.size(); j++ )
{
Bullet * bullet = bullets[j];
if ( bullet->isAlive() )
{
float allowedRange = rock->getSize() + CLOSE_ENOUGH;
if ( fabs( getClosestDistance( *bullet , *rock ) ) < allowedRange
|| bullet->getPoint().inRange( rock->getPoint() , allowedRange ) )
{
vector<Rock *> newRocks = rock->destroy();
for ( int i = 0; i < newRocks.size(); i++ )
{
rocks.push_back ( newRocks[i] );
}
rock->kill();
bullet->kill();
}
}
}
}
}
// Handle ship collisions
for ( int i = 0; i < rocks.size(); i++ )
{
Rock * rock = rocks[i];
if ( rock->isAlive() && ship->isAlive() )
{
float allowedRange = rock->getSize() + CLOSE_ENOUGH;
if ( fabs( getClosestDistance( *ship , *rock ) ) < allowedRange
|| ship->getPoint().inRange( rock->getPoint() , allowedRange ) )
{
vector<Rock *> newRocks = rock->destroy();
for ( int i = 0; i < newRocks.size(); i++ )
{
rocks.push_back ( newRocks[i] );
}
rock->kill();
ship->kill();
}
}
}
cleanUpZombies();
}
// Draw everything
void Game :: draw ( const Interface &ui )
{
drawRocks();
drawShip();
drawBullets();
drawNumber ( Point ( -170 , 170 ) , (int) ship->getFuel() );
drawText ( Point ( -170 , 180 ) , "FUEL:" );
}
// Draw rocks
void Game :: drawRocks()
{
for ( int i = 0; i < rocks.size(); i++ )
{
rocks[i]->draw();
}
}
// Draw ship
void Game :: drawShip()
{
if ( ship->isAlive() )
{
ship->draw();
}
}
// Draw bullets
void Game :: drawBullets()
{
for ( int i = 0; i < bullets.size(); i++ )
{
bullets[i]->draw();
}
}
// Handle in-game input
void Game::handleInput(const Interface & ui)
{
if ( ship->isAlive() )
{
if ( ui.isUp() )
{
ship->setThrusting ( true );
ship->thrust( true );
}
if ( ui.isDown() )
{
ship->setThrusting ( true );
ship->thrust( false );
}
if ( ui.isLeft() )
{
ship->setThrusting ( true );
ship->rotate( true );
}
if ( ui.isRight() )
{
ship->setThrusting ( true );
ship->rotate( false );
}
if ( ui.isSpace() )
{
Bullet * newBullet = new Bullet( ship->getPoint() );
newBullet->fire( ship->getVelocity(), ship->getAngle() );
bullets.push_back( newBullet );
}
if ( !ui.isRight() && !ui.isUp() && !ui.isDown() && !ui.isLeft() )
{
ship->setThrusting ( false );
}
}
}
|