1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: Christian Meyer |
---|
14 | co-programmer: Benjamin Grauer |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
18 | |
---|
19 | #include "game_world.h" |
---|
20 | #include "game_world_data.h" |
---|
21 | |
---|
22 | #include "resource_manager.h" |
---|
23 | #include "state.h" |
---|
24 | #include "class_list.h" |
---|
25 | #include "substring.h" |
---|
26 | |
---|
27 | #include "game_loader.h" |
---|
28 | |
---|
29 | #include "p_node.h" |
---|
30 | #include "world_entity.h" |
---|
31 | #include "player.h" |
---|
32 | #include "camera.h" |
---|
33 | #include "environment.h" |
---|
34 | #include "terrain.h" |
---|
35 | #include "test_entity.h" |
---|
36 | #include "terrain.h" |
---|
37 | #include "md2Model.h" |
---|
38 | #include "world_entities/projectiles/projectile.h" |
---|
39 | #include "npcs/npc_test1.h" |
---|
40 | #include "playable.h" |
---|
41 | |
---|
42 | #include "light.h" |
---|
43 | |
---|
44 | #include "factory.h" |
---|
45 | #include "fast_factory.h" |
---|
46 | #include "load_param.h" |
---|
47 | #include "shell_command.h" |
---|
48 | |
---|
49 | #include "graphics_engine.h" |
---|
50 | #include "event_handler.h" |
---|
51 | #include "sound_engine.h" |
---|
52 | #include "cd_engine.h" |
---|
53 | #include "network_manager.h" |
---|
54 | #include "physics_engine.h" |
---|
55 | #include "fields.h" |
---|
56 | |
---|
57 | #include "glmenu_imagescreen.h" |
---|
58 | #include "shell.h" |
---|
59 | |
---|
60 | #include "animation_player.h" |
---|
61 | #include "animation3d.h" |
---|
62 | |
---|
63 | #include "ogg_player.h" |
---|
64 | #include "shader.h" |
---|
65 | |
---|
66 | |
---|
67 | using namespace std; |
---|
68 | |
---|
69 | |
---|
70 | SHELL_COMMAND(speed, GameWorld, setSpeed); |
---|
71 | SHELL_COMMAND_STATIC(togglePNodeVisibility, GameWorld, GameWorld::togglePNodeVisibility); |
---|
72 | SHELL_COMMAND_STATIC(toggleBVVisibility, GameWorld, GameWorld::toggleBVVisibility); |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | GameWorld::GameWorld() |
---|
77 | : StoryEntity() |
---|
78 | { |
---|
79 | this->setClassID(CL_GAME_WORLD, "GameWorld"); |
---|
80 | this->setName("Preloaded World - no name yet"); |
---|
81 | |
---|
82 | this->gameTime = 0.0f; |
---|
83 | this->setSpeed(1.0f); |
---|
84 | this->shell = NULL; |
---|
85 | |
---|
86 | this->showPNodes = false; |
---|
87 | this->showBV = false; |
---|
88 | |
---|
89 | this->dataXML = NULL; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * remove the GameWorld from memory |
---|
94 | * |
---|
95 | * delete everything explicitly, that isn't contained in the parenting tree! |
---|
96 | * things contained in the tree are deleted automaticaly |
---|
97 | */ |
---|
98 | GameWorld::~GameWorld () |
---|
99 | { |
---|
100 | PRINTF(4)("Deleted GameWorld\n"); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | * loads the parameters of a GameWorld from an XML-element |
---|
107 | * @param root the XML-element to load from |
---|
108 | */ |
---|
109 | void GameWorld::loadParams(const TiXmlElement* root) |
---|
110 | { |
---|
111 | StoryEntity::loadParams(root); |
---|
112 | |
---|
113 | PRINTF(4)("Loaded GameWorld specific stuff\n"); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /** |
---|
118 | * this is executed just before load |
---|
119 | * |
---|
120 | * since the load function sometimes needs data, that has been initialized |
---|
121 | * before the load and after the proceeding storyentity has finished |
---|
122 | */ |
---|
123 | ErrorMessage GameWorld::init() |
---|
124 | { |
---|
125 | this->cycle = 0; |
---|
126 | /* init the world interface */ |
---|
127 | this->shell = new Shell(); |
---|
128 | |
---|
129 | State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this)); |
---|
130 | this->dataTank->init(); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | * loads the GameWorld by initializing all resources, and set their default values. |
---|
136 | */ |
---|
137 | ErrorMessage GameWorld::loadData() |
---|
138 | { |
---|
139 | this->displayLoadScreen(); |
---|
140 | |
---|
141 | PRINTF(0)("Loading the GameWorld\n"); |
---|
142 | |
---|
143 | PRINTF(3)("> Loading world: '%s'\n", getLoadFile()); |
---|
144 | TiXmlElement* element; |
---|
145 | GameLoader* loader = GameLoader::getInstance(); |
---|
146 | |
---|
147 | if( getLoadFile() == NULL) |
---|
148 | { |
---|
149 | PRINTF(1)("GameWorld has no path specified for loading\n"); |
---|
150 | return (ErrorMessage){213,"Path not specified","GameWorld::load()"}; |
---|
151 | } |
---|
152 | |
---|
153 | TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile()); |
---|
154 | // load the xml world file for further loading |
---|
155 | if( !XMLDoc->LoadFile()) |
---|
156 | { |
---|
157 | PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
158 | delete XMLDoc; |
---|
159 | return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"}; |
---|
160 | } |
---|
161 | // check basic validity |
---|
162 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
163 | assert( root != NULL); |
---|
164 | if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile")) |
---|
165 | { |
---|
166 | // report an error |
---|
167 | PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n"); |
---|
168 | delete XMLDoc; |
---|
169 | return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"}; |
---|
170 | } |
---|
171 | /* the whole loading process for the GameWorld */ |
---|
172 | this->dataTank->loadData(root); |
---|
173 | this->dataXML = (TiXmlElement*)root->Clone(); |
---|
174 | |
---|
175 | delete XMLDoc; |
---|
176 | this->releaseLoadScreen(); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | /** |
---|
181 | * unload the data of this GameWorld |
---|
182 | */ |
---|
183 | ErrorMessage GameWorld::unloadData() |
---|
184 | { |
---|
185 | delete this->shell; |
---|
186 | PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n"); |
---|
187 | |
---|
188 | this->dataTank->unloadData(); |
---|
189 | |
---|
190 | State::setCurrentStoryEntity(NULL); |
---|
191 | if (this->dataXML) |
---|
192 | delete this->dataXML; |
---|
193 | |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | /** |
---|
198 | * starts the GameWorld |
---|
199 | */ |
---|
200 | bool GameWorld::start() |
---|
201 | { |
---|
202 | this->isPaused = false; |
---|
203 | this->isRunning = true; |
---|
204 | |
---|
205 | this->run(); |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | /** |
---|
210 | * stops the world. |
---|
211 | */ |
---|
212 | bool GameWorld::stop() |
---|
213 | { |
---|
214 | PRINTF(3)("GameWorld::stop() - got stop signal\n"); |
---|
215 | this->isRunning = false; |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | /** |
---|
220 | * pauses the game |
---|
221 | */ |
---|
222 | bool GameWorld::pause() |
---|
223 | { |
---|
224 | this->isPaused = true; |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | /** |
---|
229 | * ends the pause Phase |
---|
230 | */ |
---|
231 | bool GameWorld::resume() |
---|
232 | { |
---|
233 | this->isPaused = false; |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | /** |
---|
238 | * main loop of the world: executing all world relevant function |
---|
239 | * |
---|
240 | * in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
241 | * all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
242 | * collisions drawing everything to the screen. |
---|
243 | */ |
---|
244 | void GameWorld::run() |
---|
245 | { |
---|
246 | /* start the music */ |
---|
247 | if(this->dataTank->music != NULL) |
---|
248 | this->dataTank->music->playback(); |
---|
249 | |
---|
250 | this->lastFrame = SDL_GetTicks (); |
---|
251 | PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n"); |
---|
252 | |
---|
253 | while( this->isRunning) /* @todo implement pause */ |
---|
254 | { |
---|
255 | ++this->cycle; |
---|
256 | /* process intput */ |
---|
257 | this->handleInput (); |
---|
258 | if( !this->isRunning) |
---|
259 | break; |
---|
260 | |
---|
261 | /* network synchronisation */ |
---|
262 | this->synchronize (); |
---|
263 | /* process time */ |
---|
264 | this->tick (); |
---|
265 | /* process collision */ |
---|
266 | this->collide (); |
---|
267 | /* update the state */ |
---|
268 | this->update (); |
---|
269 | /* draw everything */ |
---|
270 | this->display (); |
---|
271 | } |
---|
272 | |
---|
273 | PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n"); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | /** |
---|
278 | * synchronize local data with remote data |
---|
279 | */ |
---|
280 | void GameWorld::synchronize () |
---|
281 | {} |
---|
282 | |
---|
283 | |
---|
284 | /** |
---|
285 | * run all input processing |
---|
286 | |
---|
287 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
288 | sdl and has its own event-passing-queue. |
---|
289 | */ |
---|
290 | void GameWorld::handleInput () |
---|
291 | { |
---|
292 | EventHandler::getInstance()->process(); |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | /** |
---|
297 | * ticks a WorldEntity list |
---|
298 | * @param entityList list of the WorldEntities |
---|
299 | * @param dt time passed since last frame |
---|
300 | */ |
---|
301 | void GameWorld::tick(std::list<WorldEntity*> entityList, float dt) |
---|
302 | { |
---|
303 | std::list<WorldEntity*>::iterator entity, next; |
---|
304 | next = entityList.begin(); |
---|
305 | while (next != entityList.end()) |
---|
306 | { |
---|
307 | entity = next++; |
---|
308 | (*entity)->tick(dt); |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | * advance the timeline |
---|
314 | * |
---|
315 | * this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
316 | * the time is mesured in ms and passed to all world-entities and other classes that need |
---|
317 | * a heart-beat. |
---|
318 | */ |
---|
319 | void GameWorld::tick () |
---|
320 | { |
---|
321 | Uint32 currentFrame = SDL_GetTicks(); |
---|
322 | |
---|
323 | if( !this->isPaused) |
---|
324 | { |
---|
325 | this->dt = currentFrame - this->lastFrame; |
---|
326 | |
---|
327 | /* limit the the frame rate to 100 frames per second (fps) */ |
---|
328 | if( this->dt < 10) |
---|
329 | { |
---|
330 | /* the frame-rate is limited to 100 frames per second, all other things are for nothing. */ |
---|
331 | //PRINTF(0)("fps = 1000 - frame rate is adjusted\n"); |
---|
332 | SDL_Delay(10 - dt); |
---|
333 | this->dt = 10; |
---|
334 | } |
---|
335 | |
---|
336 | this->dtS = (float)this->dt / 1000.0f * this->speed; |
---|
337 | this->gameTime += this->dtS; |
---|
338 | |
---|
339 | this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS); |
---|
340 | this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS); |
---|
341 | this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS); |
---|
342 | this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS); |
---|
343 | this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS); |
---|
344 | this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS); |
---|
345 | this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS); |
---|
346 | |
---|
347 | /* update tick the rest */ |
---|
348 | this->dataTank->localCamera->tick(this->dtS); |
---|
349 | AnimationPlayer::getInstance()->tick(this->dtS); |
---|
350 | PhysicsEngine::getInstance()->tick(this->dtS); |
---|
351 | |
---|
352 | GraphicsEngine::getInstance()->tick(this->dtS); |
---|
353 | } |
---|
354 | this->lastFrame = currentFrame; |
---|
355 | } |
---|
356 | |
---|
357 | |
---|
358 | /** |
---|
359 | * this function gives the world a consistant state |
---|
360 | * |
---|
361 | * after ticking (updating the world state) this will give a constistant |
---|
362 | * state to the whole system. |
---|
363 | */ |
---|
364 | void GameWorld::update() |
---|
365 | { |
---|
366 | GraphicsEngine::getInstance()->update(this->dtS); |
---|
367 | PNode::getNullParent()->updateNode (this->dtS); |
---|
368 | SoundEngine::getInstance()->update(); |
---|
369 | |
---|
370 | if (this->dataTank->music != NULL) |
---|
371 | this->dataTank->music->update(); |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | /** |
---|
376 | * kicks the CDEngine to detect the collisions between the object groups in the world |
---|
377 | */ |
---|
378 | void GameWorld::collide() |
---|
379 | { |
---|
380 | CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00), |
---|
381 | this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ)); |
---|
382 | CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01), |
---|
383 | this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ)); |
---|
384 | |
---|
385 | CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01), |
---|
386 | this->dataTank->objectManager->getObjectList(OM_COMMON)); |
---|
387 | } |
---|
388 | |
---|
389 | /** |
---|
390 | * render the current frame |
---|
391 | * |
---|
392 | * clear all buffers and draw the world |
---|
393 | */ |
---|
394 | void GameWorld::display () |
---|
395 | { |
---|
396 | // clear buffer |
---|
397 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
398 | // draw world |
---|
399 | this->draw(); |
---|
400 | // flip buffers |
---|
401 | GraphicsEngine::swapBuffers(); |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | /** |
---|
406 | * runs through all entities calling their draw() methods |
---|
407 | */ |
---|
408 | void GameWorld::draw () |
---|
409 | { |
---|
410 | GraphicsEngine* engine = GraphicsEngine::getInstance(); |
---|
411 | |
---|
412 | // set camera |
---|
413 | this->dataTank->localCamera->apply (); |
---|
414 | |
---|
415 | /* draw all WorldEntiy groups */ |
---|
416 | engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK)); |
---|
417 | engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON)); |
---|
418 | engine->draw(State::getObjectManager()->getObjectList(OM_COMMON)); |
---|
419 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00)); |
---|
420 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ)); |
---|
421 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01)); |
---|
422 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ)); |
---|
423 | |
---|
424 | |
---|
425 | if( unlikely( this->showBV)) |
---|
426 | { |
---|
427 | CDEngine* engine = CDEngine::getInstance(); |
---|
428 | engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK)); |
---|
429 | engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON)); |
---|
430 | engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON)); |
---|
431 | engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00)); |
---|
432 | engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01)); |
---|
433 | engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ)); |
---|
434 | } |
---|
435 | |
---|
436 | if( unlikely(this->showPNodes)) |
---|
437 | PNode::getNullParent()->debugDraw(0); |
---|
438 | |
---|
439 | engine->draw(); |
---|
440 | } |
---|
441 | |
---|
442 | /** |
---|
443 | * shows the loading screen |
---|
444 | */ |
---|
445 | void GameWorld::displayLoadScreen () |
---|
446 | { |
---|
447 | PRINTF(3)("GameWorld::displayLoadScreen - start\n"); |
---|
448 | this->dataTank->glmis = new GLMenuImageScreen(); |
---|
449 | this->dataTank->glmis->setMaximum(8); |
---|
450 | PRINTF(3)("GameWorld::displayLoadScreen - end\n"); |
---|
451 | } |
---|
452 | |
---|
453 | |
---|
454 | /** |
---|
455 | * removes the loadscreen, and changes over to the game |
---|
456 | */ |
---|
457 | void GameWorld::releaseLoadScreen () |
---|
458 | { |
---|
459 | PRINTF(3)("GameWorld::releaseLoadScreen - start\n"); |
---|
460 | this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum()); |
---|
461 | PRINTF(3)("GameWorld::releaseLoadScreen - end\n"); |
---|
462 | } |
---|
463 | |
---|
464 | |
---|
465 | |
---|
466 | /** |
---|
467 | * @brief toggles the PNode visibility in the world (drawn as boxes) |
---|
468 | */ |
---|
469 | void GameWorld::togglePNodeVisibility() |
---|
470 | { |
---|
471 | if (State::getCurrentStoryEntity() != NULL && |
---|
472 | State::getCurrentStoryEntity()->isA(CL_GAME_WORLD)) |
---|
473 | { |
---|
474 | dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes; |
---|
475 | } |
---|
476 | else |
---|
477 | PRINTF(2)("The Current Story entity is not a GameWorld\n"); |
---|
478 | }; |
---|
479 | |
---|
480 | |
---|
481 | /** |
---|
482 | * @brief toggles the bounding volume (BV) visibility |
---|
483 | */ |
---|
484 | void GameWorld::toggleBVVisibility() |
---|
485 | { |
---|
486 | if (State::getCurrentStoryEntity() != NULL && |
---|
487 | State::getCurrentStoryEntity()->isA(CL_GAME_WORLD)) |
---|
488 | { |
---|
489 | dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV; |
---|
490 | } |
---|
491 | else |
---|
492 | PRINTF(2)("The Current Story entity is not a GameWorld\n"); |
---|
493 | }; |
---|
494 | |
---|