-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
461 lines (336 loc) · 14.8 KB
/
Copy pathmain.cpp
File metadata and controls
461 lines (336 loc) · 14.8 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "stdio.h"
#include <vector>
#include "GNAFramework/Game.h"
#include "GNAFramework/Keyboard.h"
#include "GNAFramework/Geom.h"
#include "GNAFramework/SpriteBatch.h"
#include "GNAFramework/BlendState.h"
#include "GNAFramework/MathHelper.h"
#include "GNAFramework/Mouse.h"
#include "GNAFramework/SpriteFont.h"
#include "GNAFramework/RenderTarget2D.h"
#include "tinyxml/tinyxml.h"
#include "Managers/InputManager.h"
#include "Managers/DataManager.h"
#include "Managers/DebugManager.h"
#include "GameActors/Scenario.h"
#include "GameActors/Enemy.h"
#include "GameActors/ExitPosition.h"
#include "GameActors/DirectorCamera.h"
#include "GameActors/PostProcess.h"
using namespace GNAFramework;
#define MAX_ENEMIES 16
class TronRacing : public Game {
public:
enum GameState {
Lobby,
Loading,
Playing,
WaitingNextLVL,
GameOver
} game_state;
InputManager im;
int actual_level;
bool paused;
ExitPosition *exitPosition;
int cameras;
Camera camera;
DirectorCamera *dirCamera;
//CameraState camState;
Scenario *scenario;
LightCycle *player;
int enemies_length;
Enemy **enemies;
Texture2D *loading_tex;
SpriteBatch *spriteBatch;
PostProcess *postProcces;
TronRacing() {
Content->RootDirectory = (char *) "Content/";
}
virtual void Initialize() {
DebugManager::debugInfo("Initialazing ImputManager.");
im.Initialize();
DebugManager::debugInfo("Loading GAME_CONF.xml.");
DataManager::loadGameDescription("GAME_CONF.xml");
actual_level = 0;
float ar = graphicDevice->getViewPort().aspectRatio();
camera = Camera(Vector3(-555, 430, 320) * DataManager::unit_size, Vector3(0, 0, 500.f) * DataManager::unit_size, Vector3(0, 0, 1), Frustum(45.0f, ar, 1.f, 10000.0f));
cameras = 1;
dirCamera = new DirectorCamera(graphicDevice, camera);
dirCamera->moveSpeed = 500.f;
dirCamera->PlanesChangeSpeed = 500.f;
DebugManager::debugInfo("Instantiating Scenario");
scenario = new Scenario(this, &camera);
DebugManager::debugInfo("Instantiating Player");
player = new LightCycle(this, &camera);
enemies_length = 0;
enemies = new Enemy*[MAX_ENEMIES];
for (int i = 0; i < MAX_ENEMIES; i++) enemies[i] = NULL;
exitPosition = new ExitPosition(this);
game_state = Lobby;
DebugManager::debugInfo("Initialazing GNAGame.");
Game::Initialize();
spriteBatch = new SpriteBatch(this->graphicDevice);
loading_tex = Content->Load<Texture2D > ("Loading.bmp");
DebugManager::debugInfo("Initialazing PostProcess Module.");
postProcces = new PostProcess(this);
DebugManager::debugInfo("Tron Race Initialized.");
}
virtual void Update(GameTime gameTime) {
im.Update();
if (im.KeyPressed(Keyboard::Escape)
|| (im.KeyPressed(Keyboard::F4) && im.KeyPressed(Keyboard::Left_Alt)))
Exit();
switch (game_state) {
case Lobby:
game_state = Loading;
/* On Loading "Pause = true" indicates that we have to wait that
* the game draw the load screen. */
paused = true;
break;
case Loading:
// <editor-fold defaultstate="collapsed" desc="Loading">
if (!paused) {
DebugManager::debugInfo("Loading Level.");
if (actual_level >= DataManager::levels_paths_lenght) {
throw new ArgumentOutOfRangeException("Trying to access a level out of range.");
}
char path[256];
sprintf(path, "Levels/%s", DataManager::levels_paths[actual_level]);
TiXmlDocument doc = TiXmlDocument(path);
if (doc.LoadFile()) {
TiXmlNode *base = doc.FirstChild("level");
if (base == NULL)
throw new ArgumentException("level root Tag coundn't be found. check the level's XML.");
// <editor-fold defaultstate="collapsed" desc="Loading Scenario">
DebugManager::debugInfo("Loading Scenario.");
scenario->Initialize(base->FirstChild("scenario"));
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Loading Player">
DebugManager::debugInfo("Loading Player.");
player->Initialize(base->FirstChild("player"));
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Loading Enemies">
TiXmlNode *nodeEnemys = base->FirstChild("enemys");
enemies_length = 0;
DebugManager::debugInfo("Loading Enemies.");
if (nodeEnemys != NULL) {
TiXmlElement *elemEnemys = nodeEnemys->ToElement();
for (TiXmlNode *o_node = elemEnemys->FirstChild(); o_node != 0; o_node = o_node->NextSibling()) {
if (o_node != NULL && o_node->ToElement() != NULL) {
DebugManager::debugInfo("Loading Enemy %d.", enemies_length);
if (enemies[enemies_length] == NULL) {
DebugManager::debugInfo("Instantiating Enemy %d.", enemies_length);
enemies[enemies_length] = new Enemy(this, &camera);
}
DebugManager::debugInfo("Instantializing Enemy %d.", enemies_length);
enemies[enemies_length++]->Initialize(o_node);
if (enemies_length >= MAX_ENEMIES)
throw new ArgumentException("the number of enemies exceeds the limit. check the level's XML.");
}
}
}
// </editor-fold>
exitPosition->Initialize(base->FirstChild("exit_position"));
} else {
char msg[128];
sprintf(msg, "Level XML on \"%s\" couldn't be loaded.", path);
throw new ContentLoadException(msg);
}
DebugManager::debugInfo("Level Loaded.");
game_state = Playing;
paused = true;
}
// </editor-fold>
break;
case Playing:
ImputHanlder(gameTime);
if (paused) {
} else {
player->Update(gameTime);
for (int i = 0; i < enemies_length; i++) {
enemies[i]->Update(gameTime);
}
}
break;
case WaitingNextLVL:
break;
case GameOver:
break;
}
Game::Update(gameTime);
}
virtual void Draw(GameTime gameTime) {
graphicDevice->Clear(Color::Grey);
if (Mouse::getMouseVisibility()) Mouse::setMouseVisibility(false);
switch (game_state) {
case Lobby:
break;
case Loading:
{
graphicDevice->DeepBufferEnabled(false);
spriteBatch->Begin(SpriteBatch::Immediate, BlendState::Opaque);
spriteBatch->DrawFullScreen(loading_tex, Color::White);
spriteBatch->End();
paused = false;
}
break;
case Playing:
{
if (!paused) {
Vector3 p;
Vector3 f;
Vector3 u;
switch (cameras) {
case 1:
player->getFirstPersonView(p, f, u);
setCamera(p, f, u);
miniDraw(gameTime);
break;
case 2:
player->getThirdPersonView(p, f, u);
setCamera(p, f, u);
miniDraw(gameTime);
break;
case 3:
p = exitPosition->position + Vector3(0.f, 0.f, 2000.f);
f = player->Position();
u = Vector3::UnitZ;
setCamera(p, f, u);
miniDraw(gameTime);
break;
case 4:
{
ViewPort new_vp, old_vp = graphicDevice->getViewPort();
new_vp = old_vp;
//First Person:
new_vp.Y = old_vp.height / 2;
new_vp.height = old_vp.height / 2;
graphicDevice->setViewPort(new_vp);
player->getFirstPersonView(p, f, u);
setCamera(p, f, u);
miniDraw(gameTime);
//Third Person:
new_vp.Y = 0;
new_vp.width = old_vp.width / 2;
graphicDevice->setViewPort(new_vp);
player->getThirdPersonView(p, f, u);
setCamera(p, f, u);
miniDraw(gameTime);
//Exit Positon:
new_vp.X = old_vp.width / 2;
graphicDevice->setViewPort(new_vp);
p = exitPosition->position + Vector3(0.f, 0.f, 2000.f);
f = player->Position();
u = Vector3::UnitZ;
setCamera(p, f, u);
setCamera(p, f, u);
miniDraw(gameTime);
graphicDevice->setViewPort(old_vp);
}
break;
default:
dirCamera->Update(gameTime);
camera = dirCamera->camera;
miniDraw(gameTime);
break;
}
} else {
miniDraw(gameTime);
}
}
break;
case WaitingNextLVL:
break;
case GameOver:
break;
}
DebugManager::debugInfo("End Drawing.");
Game::Draw(gameTime);
}
void setCamera(Vector3 p, Vector3 f, Vector3 u) {
Frustum frustum;
float ar = graphicDevice->getViewPort().aspectRatio();
if (cameras == 1) {
frustum = Frustum(76.0f, ar, 2.f, 10000.0f);
} else if(cameras == 2) {
frustum = Frustum(50.0f, ar, 10.f, 10000.0f);
} else if(cameras == 3) {
frustum = Frustum(50.0f, ar, 1000.f, 50000.0f);
}
camera = Camera(p, f, u, frustum);
}
void miniDraw(GameTime gameTime) {
// <editor-fold defaultstate="collapsed" desc="Draw Color:">
graphicDevice->DeepBufferEnabled(true);
graphicDevice->DeepBufferWritteEnabled(true);
graphicDevice->setRasterizerState(RasterizerState::CullCounterClockwise);
graphicDevice->setBlendState(BlendState::Opaque);
postProcces->Begin();
scenario->Draw(gameTime, DrawColor);
player->Draw(gameTime, DrawColor);
for (int i = 0; i < enemies_length; i++) {
enemies[i]->Draw(gameTime, DrawColor);
}
graphicDevice->DeepBufferWritteEnabled(false);
graphicDevice->setBlendState(BlendState(BlendState::One, BlendState::One, BlendState::Add));
graphicDevice->setRasterizerState(RasterizerState::CullNone);
player->DrawTrail(DrawColor);
for (int i = 0; i < enemies_length; i++) {
enemies[i]->DrawTrail(DrawColor);
}
postProcces->End();
// </editor-fold>
graphicDevice->DeepBufferWritteEnabled(true);
graphicDevice->setBlendState(BlendState::Opaque);
graphicDevice->setRasterizerState(RasterizerState::CullCounterClockwise);
}
virtual void Exit() {
DebugManager::debugInfo("Calling to Exit().");
delete scenario;
Game::Exit();
}
// <editor-fold defaultstate="collapsed" desc="Helpers">
void ImputHanlder(GameTime gameTime) {
if (im.KeyGetPressed(Keyboard::P)) paused = !paused;
float time = gameTime.elapsedTime.toSecondsF();
if (paused) {
} else {
if (im.KeyPressed(Keyboard::Right)) {
player->Turn(-DataManager::turnRate * time);
} else if (im.KeyPressed(Keyboard::Left)) {
player->Turn(+DataManager::turnRate * time);
}
if (im.KeyPressed(Keyboard::Space) && player->Fuel() > 0.f) {
player->Acelerate(true);
} else {
player->Acelerate(false);
}
if (im.KeyPressed(Keyboard::F1)) {
cameras = 1;
} else if (im.KeyPressed(Keyboard::F2)) {
cameras = 2;
} else if (im.KeyPressed(Keyboard::F3)) {
cameras = 3;
} else if (im.KeyPressed(Keyboard::F4)) {
cameras = 4;
} else if (im.KeyPressed(Keyboard::F5)) {
cameras = 5;
dirCamera->camera = camera;
} else if (im.KeyPressed(Keyboard::F6)) {
cameras = 6;
}
if (im.KeyGetPressed(Keyboard::B)) {
postProcces->active = !postProcces->active;
}
}
}
// </editor-fold>
};
int main(int argc, char** argv) {
DebugManager::debugInfo("Instantiating TronRacing game.");
TronRacing game;
DebugManager::debugInfo("Starting Game.");
game.Start();
return 0;
}