Repository navigation
Expand file tree
/
Copy pathLab6Main.cpp
More file actions
378 lines (308 loc) · 11.6 KB
/
Copy pathLab6Main.cpp
File metadata and controls
378 lines (308 loc) · 11.6 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**********************************************************************
* * 1. Name:
* Jonathan Gunderson, Dahal Sulav
* 2. Assignment Name:
* Lab 06: Apollo 11
*****************************************************************/
/**********************************************************************
* GL GameState
* Just a simple program to demonstrate how to create an Open GL window,
* draw something on the window, and accept simple user input
**********************************************************************/
#include "point.h"
#include "uiInteract.h"
#include "uiDraw.h"
#include "ground.h"
#include "constellations.h"
#include "star.h"
#include "crash.h"
#include "gameState.h"
#include "lander.h"
#include "environmentalForces.h"
#include "star.h"
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
/*************************************
* All the interesting work happens here, when
* I get called back from OpenGL to draw a frame.
* When I am finished drawing, then the graphics
* engine will wait until the proper amount of
* time has passed and put the drawing on the screen.
**************************************/
void callBack(const Interface* pUI, void* p)
{
ogstream gout;
playState currentstate = playState(2);
// the first step is to cast the void pointer into a game object. This
// is the first step of every single callback function in OpenGL.
GameState* GameStateInstance = (GameState*)p; // Create the Game state class to stay persistance throughout the game session
Lander& landerInstance = GameStateInstance->getLanderInstance(); // Get Lander instance created inside of the GameState class
Constellations& constellationsInstance = GameStateInstance->getConstellationsInstance(); // Get Constellations instance created inside of the GameState class
EnvironmentalForces& environmentalForcesInstance = GameStateInstance->getEnvironmentalForcesInstance(); // Get Environmental Forces instance created inside of the GameState class
constellationsInstance.show(); // Draw stars created from inside the constellations class
GameStateInstance->getGroundInstance().draw(gout); // Draw ground created from inside the GameState
// Get the current state of the lander
SessionState().getCurrentState(currentstate, GameStateInstance);
if ( currentstate == playState(win))
{
GameStateInstance->displayEndGameSession(true); // Display to user that they WON the game.
}
else if (currentstate == playState(lose))
{
GameStateInstance->displayEndGameSession(false); // Display to user that they LOST the game.
}
else
{
environmentalForcesInstance.applyGravity(); // Apply One Game Tick of Gravity
environmentalForcesInstance.applyIntertia(landerInstance.getptLMInstance()); // Apply One Game Tick of Intertia
landerInstance.applyThrustEffect(landerInstance, environmentalForcesInstance); // Apply One Game Tick ofthrust
GameStateInstance->getPlayerController(pUI, landerInstance); // move the ship around
// draw lander flames
gout.drawLanderFlames(landerInstance.getptLMInstance(), landerInstance.getAngle(), /*angle*/
pUI->isDown(), pUI->isLeft(), pUI->isRight());
GameStateInstance->onScreenStats(); // put some text on the screen
gout.drawLander(landerInstance.getptLMInstance() /*position*/, landerInstance.getAngle() /*angle*/);
}
}
/*********************************
* iterate through the vector
* of created star instances
*********************************/
void Constellations::show()
{
for (int i = 0; i < constellationsInstance.size(); i++) // Iterate through the stored stars vector.
{
constellationsInstance.at(i).show(); // Display the stars on screen.
}
}
/*********************************
* Display the star class instance
*********************************/
void Star::show()
{
ogstream gout;
gout.drawStar(location, phase);
phase++;
}
/*********************************
* Create The effect of Gravity.
* The lander will be constantly
* Pulled Down.
*********************************/
void EnvironmentalForces::applyGravity()
{
ddy += GRAVITY;
}
/*********************************
* Apply Intertia effect. The input will be a point in space,
* taken, altered, and returned to its appropriate new value.
* Time is based on the set dialation which can be calculated
* Based on the current game framerate.
*********************************/
void EnvironmentalForces::applyIntertia(Point& point)
{
dx = ddx * TIMEDILATION;
dy = ddy * TIMEDILATION;
/*********************************
* Apply the kinematic equation of
* --> s = s + vt + 1/2 at^2 <---
* The time is based of the the time dilation game modifier
*********************************/
point.setX((point.getX()) + (dx * TIMEDILATION) + (.5 * ddx * (TIMEDILATION * TIMEDILATION)));
point.setY((point.getY()) + (dy * TIMEDILATION) + (.5 * ddy * (TIMEDILATION * TIMEDILATION)));
}
/*********************************
* Decrement the amount of fuel that
* is currently in the lander, Only
* When the user applys thrust.
*********************************/
void Lander::decrementFuel()
{
fuel -= 1;
}
/*********************************
* Set if the player is requesting
* that thrust be applied to the moon
* lander, by the moon lander.
*********************************/
void Lander::setThrustActive(bool thrust)
{
applyThrust = thrust;
}
/*********************************
* Apply thrust to the lander. THis will be appied to the
* current acceleration value. That will need to be
* follow-up called on by the applyIntertia function.
*********************************/
void Lander::applyThrustEffect(Lander& landerInstance, EnvironmentalForces& environmentalForcesInstance)
{
if (landerInstance.isThrustActive() == true)
{
environmentalForcesInstance.setDDY(environmentalForcesInstance.getDDY() + (cos(landerInstance.getAngle()) * Lander().THRUST / Lander().WEIGHT)); //acceleration In vertical axis
environmentalForcesInstance.setDDX(environmentalForcesInstance.getDDX() + (-1 * (sin(landerInstance.getAngle()) * Lander().THRUST / Lander().WEIGHT))); //acceleration In horizontal axis
landerInstance.decrementFuel();
}
}
/*********************************
* Return Current velocity of the tracked
* entitiy that the enviromental forces classes
* was created for.
*********************************/
double EnvironmentalForces::getTotalVelocity()
{
return sqrt(dx * dx + dy * dy);
}
/*********************************
* retrieve the players controls,
* route data to the lander inputs.
*********************************/
void GameState::getPlayerController(const Interface* pUI, Lander& landerInstance)
{
if (pUI->isRight())
landerInstance.setAngle(landerInstance.getAngle() - .1);
if (pUI->isLeft())
landerInstance.setAngle(landerInstance.getAngle() + .1);
if (pUI->isDown())
landerInstance.setThrustActive(true);
else
landerInstance.setThrustActive(false);
}
/*********************************
* Return the currnet distance beween the
* GROUND instance and the LANDER instance
*********************************/
double GameState::altitudeToGround()
{
return this->getGroundInstance().getElevation(landerInstance.getptLMInstance());
}
/*********************************
* Display the end session flavor text.
* Text type is based on a WIN/LOSE condition.
*********************************/
void GameState::displayEndGameSession(bool endCondition)
{
ogstream endGameSessionInformationOut;
endGameSessionInformationOut.setPosition(Point(250, 400.0));
if (endCondition == true)
{
endGameSessionInformationOut << "YOU WIN :D";
}
else
{
endGameSessionInformationOut << "YOU LOSE :(";
}
}
/*********************************
* Plan on passing reading the values for the
* onscreen text directly from the lander state.
*********************************/
void GameState::onScreenStats()
{
ogstream gout;
gout.setPosition(Point(30.0, 550.0));
gout << "Fuel:\t" << this->getLanderInstance().getFuelStatus() << " lbs" << "\n"
<< "Altitude:\t" << this->altitudeToGround() << " meters" << "\n"
<< "Speed:\t" << this->getEnvironmentalForcesInstance().getTotalVelocity() << showpoint << fixed << setprecision(2) << " m/s";
}
/*********************************
* Return the current state of the game session, if the lander
* is still flying, attempted to land on platform,
* or crashed into the ground.
*********************************/
void SessionState::getCurrentState(playState ¤tstate, GameState* GameStateInstance)
{
if ((this->crashedIntoGroundCheck(GameStateInstance)) ||
this->isFuelEmptyCheck() == true ||
(this->landedOnPlatformCheck(GameStateInstance) == true // Check if they landed on the platform.
&&
this->crashedIntoPlatformCheck(GameStateInstance) == true // ensure player did not hit the plateform at a speed greater 5 m/s
))
{
currentstate = playState(1);
}
else if (this->landedOnPlatformCheck(GameStateInstance))
{
currentstate = playState(0);
}
else
{
currentstate = playState(2);
}
}
/*********************************
* return if the lander has impacted
* the ground at any point of the gameplay.
*********************************/
bool SessionState::crashedIntoGroundCheck(GameState* GameStateInstance)
{
return GameStateInstance->getGroundInstance().hitGround(GameStateInstance->getLanderInstance().getptLMInstance(), Lander().getMoonLanderWidth());
}
/*********************************
* Check if the lander is inside
* the landing zone.
*********************************/
bool SessionState::landedOnPlatformCheck(GameState* GameStateInstance)
{
return GameStateInstance->getGroundInstance().onPlatform(GameStateInstance->getLanderInstance().getptLMInstance(), Lander().getMoonLanderWidth());
}
/*********************************
* Check if the lander hit the platform at
* a velocity greater than 5 m / s.
* if so, report crash, else landed.
*********************************/
bool SessionState::crashedIntoPlatformCheck(GameState* GameStateInstance)
{
if (EnvironmentalForces().getTotalVelocity() > 5.0)
{
return true;
}
else
{
return false;
}
}
/*********************************
* Return current fuel-level of
* the moon lander.
*********************************/
bool SessionState::isFuelEmptyCheck()
{
if (Lander().getFuelStatus() <= 0.0)
{
return true;
}
else
{
return false;
}
}
/*********************************
* Main is pretty sparse. Just initialize
* my GameState type and call the display engine.
* That is all!
*********************************/
#ifdef _WIN32_X
#include <windows.h>
int WINAPI wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ PWSTR pCmdLine,
_In_ int nCmdShow)
#else // !_WIN32
int main(int argc, char** argv)
#endif // !_WIN32
{
// Initialize OpenGL
Point ptUpperRight(600.0, 600.0);
Interface ui(0, NULL,
"Open GL",
ptUpperRight);
// Initialize the game class
GameState demo(ptUpperRight);
// set everything into action
ui.run(callBack, &demo);
return 0;
}