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