[4555] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
[2190] | 13 | co-programmer: Christian Meyer |
---|
[5390] | 14 | co-programmer: Benjamin Grauer |
---|
[1853] | 15 | */ |
---|
| 16 | |
---|
[3590] | 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
| 18 | |
---|
[2190] | 19 | #include "world.h" |
---|
[3608] | 20 | |
---|
[5205] | 21 | #include "shell_command.h" |
---|
[6150] | 22 | #include "resource_manager.h" |
---|
[4347] | 23 | #include "state.h" |
---|
| 24 | |
---|
[3608] | 25 | #include "p_node.h" |
---|
[2190] | 26 | #include "world_entity.h" |
---|
[2036] | 27 | #include "player.h" |
---|
[2190] | 28 | #include "camera.h" |
---|
[2816] | 29 | #include "environment.h" |
---|
[6151] | 30 | #include "terrain.h" |
---|
[6150] | 31 | |
---|
[4245] | 32 | #include "test_entity.h" |
---|
[3608] | 33 | #include "terrain.h" |
---|
[3436] | 34 | #include "light.h" |
---|
[4726] | 35 | #include "load_param.h" |
---|
[5174] | 36 | #include "shell.h" |
---|
[3620] | 37 | |
---|
[4940] | 38 | #include "fast_factory.h" |
---|
[3812] | 39 | #include "animation_player.h" |
---|
[4176] | 40 | #include "particle_engine.h" |
---|
[4245] | 41 | #include "graphics_engine.h" |
---|
[4338] | 42 | #include "physics_engine.h" |
---|
[4396] | 43 | #include "fields.h" |
---|
[3646] | 44 | |
---|
[4488] | 45 | #include "md2Model.h" |
---|
| 46 | |
---|
[3608] | 47 | #include "glmenu_imagescreen.h" |
---|
[4010] | 48 | #include "game_loader.h" |
---|
[2036] | 49 | |
---|
[3964] | 50 | #include "animation3d.h" |
---|
[3608] | 51 | |
---|
[4010] | 52 | #include "substring.h" |
---|
[3608] | 53 | |
---|
[4261] | 54 | #include "factory.h" |
---|
[4245] | 55 | |
---|
[5556] | 56 | #include "weapons/projectile.h" |
---|
[4405] | 57 | #include "event_handler.h" |
---|
[4504] | 58 | #include "sound_engine.h" |
---|
[4961] | 59 | #include "ogg_player.h" |
---|
[4504] | 60 | |
---|
[4747] | 61 | #include "class_list.h" |
---|
| 62 | |
---|
[4917] | 63 | #include "cd_engine.h" |
---|
[5750] | 64 | #include "npcs/npc_test1.h" |
---|
[5318] | 65 | #include "shader.h" |
---|
[4820] | 66 | |
---|
[5915] | 67 | #include "playable.h" |
---|
[5996] | 68 | #include "network_manager.h" |
---|
| 69 | #include "playable.h" |
---|
[5915] | 70 | |
---|
[5996] | 71 | |
---|
[5205] | 72 | SHELL_COMMAND(speed, World, setSpeed); |
---|
[5389] | 73 | SHELL_COMMAND(togglePNodeVisibility, World, togglePNodeVisibility); |
---|
[5429] | 74 | SHELL_COMMAND(toggleBVVisibility, World, toggleBVVisibility); |
---|
[5205] | 75 | |
---|
[1856] | 76 | using namespace std; |
---|
[1853] | 77 | |
---|
[4978] | 78 | //! This creates a Factory to fabricate a World |
---|
[5750] | 79 | CREATE_FACTORY(World, CL_WORLD); |
---|
[3620] | 80 | |
---|
[4261] | 81 | World::World(const TiXmlElement* root) |
---|
[4010] | 82 | { |
---|
| 83 | this->constuctorInit("", -1); |
---|
[4094] | 84 | this->path = NULL; |
---|
[4555] | 85 | |
---|
[4261] | 86 | this->loadParams(root); |
---|
[4010] | 87 | } |
---|
| 88 | |
---|
[4555] | 89 | /** |
---|
[4838] | 90 | * remove the World from memory |
---|
[6150] | 91 | * |
---|
| 92 | * delete everything explicitly, that isn't contained in the parenting tree! |
---|
| 93 | * things contained in the tree are deleted automaticaly |
---|
[4838] | 94 | */ |
---|
[2190] | 95 | World::~World () |
---|
[1872] | 96 | { |
---|
[5206] | 97 | delete this->shell; |
---|
[3546] | 98 | PRINTF(3)("World::~World() - deleting current world\n"); |
---|
[3677] | 99 | |
---|
[5915] | 100 | delete this->localPlayer; |
---|
[5115] | 101 | |
---|
[5296] | 102 | // delete all the initialized Engines. |
---|
[5447] | 103 | FastFactory::flushAll(true); |
---|
[4735] | 104 | delete LightManager::getInstance(); |
---|
[4822] | 105 | delete ParticleEngine::getInstance(); |
---|
[5296] | 106 | delete AnimationPlayer::getInstance(); |
---|
[4978] | 107 | delete PhysicsEngine::getInstance(); |
---|
[4822] | 108 | |
---|
[4978] | 109 | // external engines initialized by the orxonox-class get deleted |
---|
[4504] | 110 | SoundEngine::getInstance()->flushAllBuffers(); |
---|
[4830] | 111 | SoundEngine::getInstance()->flushAllSources(); |
---|
[4504] | 112 | |
---|
[6142] | 113 | if (State::getObjectManager() == &this->objectManager) |
---|
| 114 | State::setObjectManager(NULL); |
---|
[4978] | 115 | // erease everything that is left. |
---|
[6074] | 116 | delete PNode::getNullParent(); |
---|
[5915] | 117 | |
---|
[6054] | 118 | //secondary cleanup of PNodes; |
---|
| 119 | const std::list<BaseObject*>* nodeList = ClassList::getList(CL_PARENT_NODE); |
---|
| 120 | if (nodeList != NULL) |
---|
| 121 | while (!nodeList->empty()) |
---|
| 122 | delete nodeList->front(); |
---|
| 123 | |
---|
[5318] | 124 | Shader::suspendShader(); |
---|
[4872] | 125 | |
---|
[4978] | 126 | // unload the resources !! |
---|
[4136] | 127 | ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
[1872] | 128 | } |
---|
[1858] | 129 | |
---|
[3526] | 130 | /** |
---|
[4978] | 131 | * initializes the world. |
---|
| 132 | * @param name the name of the world |
---|
| 133 | * @param worldID the ID of this world |
---|
| 134 | * |
---|
| 135 | * set all stuff here that is world generic and does not use to much memory |
---|
| 136 | * because the real init() function StoryEntity::init() will be called |
---|
| 137 | * shortly before start of the game. |
---|
| 138 | * since all worlds are initiated/referenced before they will be started. |
---|
| 139 | * NO LEVEL LOADING HERE - NEVER! |
---|
[3526] | 140 | */ |
---|
[4978] | 141 | void World::constuctorInit(const char* name, int worldID) |
---|
[3526] | 142 | { |
---|
[4320] | 143 | this->setClassID(CL_WORLD, "World"); |
---|
[2636] | 144 | |
---|
[4978] | 145 | this->setName(name); |
---|
[5211] | 146 | this->gameTime = 0.0f; |
---|
[5205] | 147 | this->setSpeed(1.0); |
---|
[4961] | 148 | this->music = NULL; |
---|
[5211] | 149 | this->shell = NULL; |
---|
[5915] | 150 | this->localPlayer = NULL; |
---|
| 151 | this->localCamera = NULL; |
---|
[5389] | 152 | |
---|
| 153 | this->showPNodes = false; |
---|
[5429] | 154 | this->showBV = false; |
---|
[3629] | 155 | } |
---|
[3526] | 156 | |
---|
[4978] | 157 | /** |
---|
| 158 | * loads the parameters of a World from an XML-element |
---|
| 159 | * @param root the XML-element to load from |
---|
| 160 | */ |
---|
[4261] | 161 | void World::loadParams(const TiXmlElement* root) |
---|
| 162 | { |
---|
[4600] | 163 | PRINTF(4)("Creating a World\n"); |
---|
[4261] | 164 | |
---|
[5671] | 165 | LoadParam(root, "identifier", this, World, setStoryID) |
---|
[4261] | 166 | .describe("Sets the StoryID of this world"); |
---|
[4834] | 167 | |
---|
[5671] | 168 | LoadParam(root, "nextid", this, World, setNextStoryID) |
---|
[4261] | 169 | .describe("Sets the ID of the next world"); |
---|
[4834] | 170 | |
---|
[5671] | 171 | LoadParam(root, "path", this, World, setPath) |
---|
[4261] | 172 | .describe("The Filename of this World (relative from the data-dir)"); |
---|
| 173 | } |
---|
| 174 | |
---|
[3629] | 175 | /** |
---|
[4978] | 176 | * this is executed just before load |
---|
| 177 | * |
---|
| 178 | * since the load function sometimes needs data, that has been initialized |
---|
| 179 | * before the load and after the proceeding storyentity has finished |
---|
[3629] | 180 | */ |
---|
| 181 | ErrorMessage World::preLoad() |
---|
| 182 | { |
---|
[6142] | 183 | State::setObjectManager(&this->objectManager); |
---|
[4829] | 184 | this->cycle = 0; |
---|
| 185 | |
---|
[3620] | 186 | /* init the world interface */ |
---|
[5206] | 187 | this->shell = new Shell(); |
---|
[4010] | 188 | |
---|
[4735] | 189 | LightManager::getInstance(); |
---|
[6074] | 190 | PNode::getNullParent(); |
---|
[3993] | 191 | |
---|
[4010] | 192 | AnimationPlayer::getInstance(); // initializes the animationPlayer |
---|
[5915] | 193 | ParticleEngine::getInstance(); |
---|
[4338] | 194 | PhysicsEngine::getInstance(); |
---|
[4010] | 195 | |
---|
[4015] | 196 | this->localCamera = new Camera(); |
---|
[4978] | 197 | this->localCamera->setName ("World-Camera"); |
---|
[4555] | 198 | |
---|
[4827] | 199 | State::setCamera(this->localCamera, this->localCamera->getTarget()); |
---|
[4347] | 200 | |
---|
[4245] | 201 | GraphicsEngine::getInstance()->displayFPS(true); |
---|
[6152] | 202 | this->displayLoadScreen(); |
---|
[3526] | 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
[3449] | 206 | /** |
---|
[4836] | 207 | * loads the World by initializing all resources, and set their default values. |
---|
[6150] | 208 | */ |
---|
[3459] | 209 | ErrorMessage World::load() |
---|
[4555] | 210 | { |
---|
[4104] | 211 | PRINTF(3)("> Loading world: '%s'\n", getPath()); |
---|
| 212 | TiXmlElement* element; |
---|
[4010] | 213 | GameLoader* loader = GameLoader::getInstance(); |
---|
[4555] | 214 | |
---|
[4010] | 215 | if( getPath() == NULL) |
---|
[2636] | 216 | { |
---|
[4104] | 217 | PRINTF(1)("World has no path specified for loading"); |
---|
[4010] | 218 | return (ErrorMessage){213,"Path not specified","World::load()"}; |
---|
| 219 | } |
---|
[4555] | 220 | |
---|
[5915] | 221 | TiXmlDocument* XMLDoc = new TiXmlDocument( getPath()); |
---|
[4010] | 222 | // load the campaign document |
---|
[4555] | 223 | if( !XMLDoc->LoadFile()) |
---|
[4010] | 224 | { |
---|
| 225 | // report an error |
---|
[5915] | 226 | PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getPath(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
[4010] | 227 | delete XMLDoc; |
---|
| 228 | return (ErrorMessage){213,"XML File parsing error","World::load()"}; |
---|
| 229 | } |
---|
[4555] | 230 | |
---|
[4010] | 231 | // check basic validity |
---|
| 232 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
| 233 | assert( root != NULL); |
---|
[4555] | 234 | |
---|
[4010] | 235 | if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile")) |
---|
| 236 | { |
---|
| 237 | // report an error |
---|
[4104] | 238 | PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n"); |
---|
[4010] | 239 | delete XMLDoc; |
---|
| 240 | return (ErrorMessage){213,"Path not a WorldDataFile","World::load()"}; |
---|
| 241 | } |
---|
[4555] | 242 | |
---|
[4010] | 243 | // load the parameters |
---|
| 244 | // name |
---|
| 245 | const char* string = grabParameter( root, "name"); |
---|
| 246 | if( string == NULL) |
---|
| 247 | { |
---|
[4104] | 248 | PRINTF(2)("World is missing a proper 'name'\n"); |
---|
[5211] | 249 | this->setName("Unknown"); |
---|
[4010] | 250 | } |
---|
| 251 | else |
---|
| 252 | { |
---|
[5211] | 253 | this->setName(string); |
---|
[4010] | 254 | } |
---|
[4978] | 255 | |
---|
[4104] | 256 | //////////////// |
---|
| 257 | // LOADSCREEN // |
---|
| 258 | //////////////// |
---|
| 259 | element = root->FirstChildElement("LoadScreen"); |
---|
| 260 | if (element == NULL) |
---|
| 261 | { |
---|
| 262 | PRINTF(2)("no LoadScreen specified, loading default\n"); |
---|
| 263 | |
---|
| 264 | glmis->setBackgroundImage("pictures/load_screen.jpg"); |
---|
| 265 | this->glmis->setMaximum(8); |
---|
| 266 | this->glmis->draw(); |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | { |
---|
[4261] | 270 | this->glmis->loadParams(element); |
---|
[4104] | 271 | this->glmis->draw(); |
---|
| 272 | } |
---|
| 273 | this->glmis->draw(); |
---|
[4726] | 274 | |
---|
| 275 | //////////////////////// |
---|
| 276 | // find WorldEntities // |
---|
| 277 | //////////////////////// |
---|
| 278 | |
---|
[4104] | 279 | element = root->FirstChildElement("WorldEntities"); |
---|
[4555] | 280 | |
---|
[4010] | 281 | if( element == NULL) |
---|
| 282 | { |
---|
[4104] | 283 | PRINTF(1)("World is missing 'WorldEntities'\n"); |
---|
[4010] | 284 | } |
---|
| 285 | else |
---|
| 286 | { |
---|
| 287 | element = element->FirstChildElement(); |
---|
| 288 | // load Players/Objects/Whatever |
---|
[4104] | 289 | PRINTF(4)("Loading WorldEntities\n"); |
---|
[4010] | 290 | while( element != NULL) |
---|
[4555] | 291 | { |
---|
[5996] | 292 | BaseObject* created = Factory::fabricate(element); |
---|
[5915] | 293 | if( created != NULL ) |
---|
| 294 | { |
---|
| 295 | if(created->isA(CL_WORLD_ENTITY)) |
---|
| 296 | this->spawn(dynamic_cast<WorldEntity*>(created)); |
---|
[5996] | 297 | printf("Created a %s: %s\n", created->getClassName(), created->getName()); |
---|
[5915] | 298 | } |
---|
| 299 | |
---|
[4555] | 300 | // if we load a 'Player' we use it as localPlayer |
---|
[5915] | 301 | |
---|
| 302 | |
---|
[4555] | 303 | //todo do this more elegant |
---|
[5915] | 304 | if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) |
---|
[6150] | 305 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
[4918] | 306 | if( element->Value() != NULL && !strcmp( element->Value(), "Terrain")) |
---|
| 307 | { |
---|
[5915] | 308 | terrain = dynamic_cast<Terrain*>(created); |
---|
[4918] | 309 | CDEngine::getInstance()->setTerrain(terrain); |
---|
| 310 | } |
---|
[4555] | 311 | element = element->NextSiblingElement(); |
---|
[4836] | 312 | glmis->step(); //! @todo temporary |
---|
[4555] | 313 | } |
---|
[4104] | 314 | PRINTF(4)("Done loading WorldEntities\n"); |
---|
[4010] | 315 | } |
---|
[4555] | 316 | |
---|
[4726] | 317 | ////////////////////////////// |
---|
| 318 | // LOADING ADDITIONAL STUFF // |
---|
| 319 | ////////////////////////////// |
---|
| 320 | |
---|
[5652] | 321 | LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams); |
---|
[4735] | 322 | |
---|
[5915] | 323 | LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams); |
---|
| 324 | // LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams); |
---|
[4726] | 325 | |
---|
[4010] | 326 | // free the XML data |
---|
[4015] | 327 | |
---|
[4010] | 328 | delete XMLDoc; |
---|
[4015] | 329 | /* GENERIC LOADING PROCESS FINISHED */ |
---|
[4555] | 330 | |
---|
| 331 | |
---|
[5915] | 332 | // Create a Player |
---|
| 333 | this->localPlayer = new Player(); |
---|
[4245] | 334 | |
---|
[5915] | 335 | Playable* playable; |
---|
| 336 | const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE); |
---|
| 337 | if (playableList != NULL) |
---|
| 338 | { |
---|
| 339 | playable = dynamic_cast<Playable*>(playableList->front()); |
---|
| 340 | this->localPlayer->setControllable(playable); |
---|
| 341 | } |
---|
[4555] | 342 | |
---|
| 343 | |
---|
[5915] | 344 | // //localCamera->setParent(TrackNode::getInstance()); |
---|
| 345 | // tn->addChild(this->localCamera); |
---|
[4620] | 346 | localCamera->setClipRegion(1, 10000.0); |
---|
[6222] | 347 | // localCamera->lookAt(playable); |
---|
[5915] | 348 | // this->localPlayer->setParentMode(PNODE_ALL); |
---|
[6241] | 349 | if (this->sky != NULL) |
---|
[5355] | 350 | { |
---|
| 351 | this->sky->setParent(this->localCamera); |
---|
| 352 | this->sky->setParentMode(PNODE_MOVEMENT); |
---|
| 353 | } |
---|
[4504] | 354 | SoundEngine::getInstance()->setListener(this->localCamera); |
---|
[4176] | 355 | |
---|
[4347] | 356 | |
---|
[4709] | 357 | |
---|
[4715] | 358 | //////////// |
---|
| 359 | // STATIC // |
---|
| 360 | //////////// |
---|
| 361 | |
---|
| 362 | |
---|
[4721] | 363 | // TestEntity* testEntity = new TestEntity(); |
---|
| 364 | // testEntity->setRelCoor(Vector(570, 10, -15)); |
---|
| 365 | // testEntity->setRelDir(Quaternion(M_PI, Vector(0, 1, 0))); |
---|
| 366 | // this->spawn(testEntity); |
---|
[4397] | 367 | |
---|
[4976] | 368 | for(int i = 0; i < 100; i++) |
---|
| 369 | { |
---|
[5750] | 370 | WorldEntity* tmp = new NPCTest1(); |
---|
[5049] | 371 | char npcChar[10]; |
---|
| 372 | sprintf (npcChar, "NPC_%d", i); |
---|
[5256] | 373 | tmp->setName(npcChar); |
---|
[5336] | 374 | tmp->setAbsCoor(((float)rand()/RAND_MAX) * 5000, 50/*+ (float)rand()/RAND_MAX*20*/, ((float)rand()/RAND_MAX -.5) *30); |
---|
[4976] | 375 | this->spawn(tmp); |
---|
| 376 | } |
---|
| 377 | |
---|
[6034] | 378 | this->music = NULL; |
---|
| 379 | //(OggPlayer*)ResourceManager::getInstance()->load("sound/00-luke_grey_-_hypermode.ogg", OGG, RP_LEVEL); |
---|
[5211] | 380 | //music->playback(); |
---|
[4010] | 381 | } |
---|
[3365] | 382 | |
---|
[6152] | 383 | ErrorMessage World::postLoad() |
---|
| 384 | { |
---|
| 385 | this->releaseLoadScreen(); |
---|
| 386 | } |
---|
[4245] | 387 | |
---|
[6152] | 388 | |
---|
[4326] | 389 | /** |
---|
[4836] | 390 | * initializes a new World shortly before start |
---|
[4978] | 391 | * |
---|
| 392 | * this is the function, that will be loaded shortly before the world is |
---|
| 393 | * started |
---|
[3459] | 394 | */ |
---|
[6152] | 395 | ErrorMessage World::preStart() |
---|
[3459] | 396 | { |
---|
| 397 | this->bPause = false; |
---|
[5051] | 398 | |
---|
| 399 | /* update the object position before game start - so there are no wrong coordinates used in the first processing */ |
---|
[6074] | 400 | PNode::getNullParent()->updateNode (0.001f); |
---|
| 401 | PNode::getNullParent()->updateNode (0.001f); |
---|
[3459] | 402 | } |
---|
| 403 | |
---|
| 404 | |
---|
| 405 | /** |
---|
[4836] | 406 | * starts the World |
---|
[6150] | 407 | */ |
---|
[3459] | 408 | ErrorMessage World::start() |
---|
| 409 | { |
---|
[6151] | 410 | this->bQuitWorld = false; |
---|
[3459] | 411 | this->mainLoop(); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | /** |
---|
[4836] | 415 | * stops the world. |
---|
[3459] | 416 | |
---|
| 417 | This happens, when the player decides to end the Level. |
---|
| 418 | */ |
---|
| 419 | ErrorMessage World::stop() |
---|
| 420 | { |
---|
[3546] | 421 | PRINTF(3)("World::stop() - got stop signal\n"); |
---|
[6151] | 422 | this->bQuitWorld= true; |
---|
[3459] | 423 | } |
---|
| 424 | |
---|
| 425 | /** |
---|
[4836] | 426 | * pauses the Game |
---|
[3459] | 427 | */ |
---|
| 428 | ErrorMessage World::pause() |
---|
| 429 | { |
---|
| 430 | this->isPaused = true; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | /** |
---|
[4836] | 434 | * ends the pause Phase |
---|
[3459] | 435 | */ |
---|
| 436 | ErrorMessage World::resume() |
---|
| 437 | { |
---|
| 438 | this->isPaused = false; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | /** |
---|
[4836] | 442 | * destroys the World |
---|
[3459] | 443 | */ |
---|
| 444 | ErrorMessage World::destroy() |
---|
| 445 | { |
---|
[3566] | 446 | |
---|
[3459] | 447 | } |
---|
| 448 | |
---|
| 449 | /** |
---|
[4836] | 450 | * shows the loading screen |
---|
[3459] | 451 | */ |
---|
| 452 | void World::displayLoadScreen () |
---|
| 453 | { |
---|
[4555] | 454 | PRINTF(3)("World::displayLoadScreen - start\n"); |
---|
| 455 | |
---|
| 456 | //GLMenuImageScreen* |
---|
[4099] | 457 | this->glmis = new GLMenuImageScreen(); |
---|
[3675] | 458 | this->glmis->setMaximum(8); |
---|
[4555] | 459 | |
---|
| 460 | PRINTF(3)("World::displayLoadScreen - end\n"); |
---|
[3459] | 461 | } |
---|
| 462 | |
---|
| 463 | /** |
---|
[6150] | 464 | * @brief removes the loadscreen, and changes over to the game |
---|
| 465 | * |
---|
| 466 | * @todo take out the delay |
---|
[3459] | 467 | */ |
---|
| 468 | void World::releaseLoadScreen () |
---|
| 469 | { |
---|
[4555] | 470 | PRINTF(3)("World::releaseLoadScreen - start\n"); |
---|
[3459] | 471 | this->glmis->setValue(this->glmis->getMaximum()); |
---|
[4555] | 472 | PRINTF(3)("World::releaseLoadScreen - end\n"); |
---|
[4099] | 473 | delete this->glmis; |
---|
[3459] | 474 | } |
---|
| 475 | |
---|
| 476 | |
---|
[3620] | 477 | /** |
---|
[4836] | 478 | * this returns the current game time |
---|
| 479 | * @returns elapsed game time |
---|
[3646] | 480 | */ |
---|
| 481 | double World::getGameTime() |
---|
| 482 | { |
---|
| 483 | return this->gameTime; |
---|
| 484 | } |
---|
| 485 | |
---|
[3225] | 486 | |
---|
[5045] | 487 | |
---|
[2190] | 488 | /** |
---|
[4836] | 489 | * synchronize local data with remote data |
---|
[1855] | 490 | */ |
---|
[2636] | 491 | void World::synchronize () |
---|
[1855] | 492 | { |
---|
[2636] | 493 | // Get remote input |
---|
| 494 | // Update synchronizables |
---|
[5996] | 495 | /* NetworkManager::getInstance()->synchronize();*/ |
---|
[1855] | 496 | } |
---|
[2636] | 497 | |
---|
[3459] | 498 | |
---|
[2636] | 499 | /** |
---|
[4836] | 500 | * run all input processing |
---|
[3225] | 501 | |
---|
| 502 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
| 503 | sdl and has its own event-passing-queue. |
---|
[2636] | 504 | */ |
---|
[3225] | 505 | void World::handleInput () |
---|
[2636] | 506 | { |
---|
[4407] | 507 | EventHandler::getInstance()->process(); |
---|
| 508 | |
---|
[2636] | 509 | // remoteinput |
---|
| 510 | } |
---|
| 511 | |
---|
[6142] | 512 | void World::tick(std::list<WorldEntity*> entityList, float dt) |
---|
| 513 | { |
---|
| 514 | std::list<WorldEntity*>::iterator entity; |
---|
| 515 | for (entity = entityList.begin(); entity != entityList.end(); entity++) |
---|
| 516 | (*entity)->tick(dt); |
---|
[3459] | 517 | |
---|
[6142] | 518 | } |
---|
| 519 | |
---|
[2636] | 520 | /** |
---|
[4836] | 521 | * advance the timeline |
---|
[3225] | 522 | |
---|
| 523 | this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
| 524 | the time is mesured in ms and passed to all world-entities and other classes that need |
---|
| 525 | a heart-beat. |
---|
[2636] | 526 | */ |
---|
[3551] | 527 | void World::tick () |
---|
[2636] | 528 | { |
---|
| 529 | Uint32 currentFrame = SDL_GetTicks(); |
---|
| 530 | if(!this->bPause) |
---|
| 531 | { |
---|
[3644] | 532 | this->dt = currentFrame - this->lastFrame; |
---|
[4555] | 533 | |
---|
[4610] | 534 | if( this->dt > 10) |
---|
[4555] | 535 | { |
---|
| 536 | float fps = 1000/dt; |
---|
[3790] | 537 | |
---|
[4555] | 538 | // temporary, only for showing how fast the text-engine is |
---|
| 539 | char tmpChar[20]; |
---|
| 540 | sprintf(tmpChar, "fps: %4.0f", fps); |
---|
| 541 | } |
---|
[2636] | 542 | else |
---|
[4555] | 543 | { |
---|
| 544 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
| 545 | nothing. |
---|
| 546 | */ |
---|
[5048] | 547 | PRINTF(3)("fps = 1000 - frame rate is adjusted\n"); |
---|
[4610] | 548 | SDL_Delay(10-dt); |
---|
[4555] | 549 | this->dt = 10; |
---|
| 550 | } |
---|
| 551 | |
---|
[5205] | 552 | this->dtS = (float)this->dt / 1000.0 * this->speed; |
---|
[4145] | 553 | this->gameTime += this->dtS; |
---|
[4833] | 554 | |
---|
[6155] | 555 | this->tick(this->objectManager.getObjectList(OM_DEAD_TICK), this->dtS); |
---|
[6142] | 556 | this->tick(this->objectManager.getObjectList(OM_COMMON), this->dtS); |
---|
[6155] | 557 | this->tick(this->objectManager.getObjectList(OM_GROUP_00), this->dtS); |
---|
[6142] | 558 | this->tick(this->objectManager.getObjectList(OM_GROUP_01), this->dtS); |
---|
| 559 | this->tick(this->objectManager.getObjectList(OM_GROUP_01_PROJ), this->dtS); |
---|
[4010] | 560 | |
---|
[3459] | 561 | /* update tick the rest */ |
---|
[4832] | 562 | this->localCamera->tick(this->dtS); |
---|
[4558] | 563 | // tick the engines |
---|
[4245] | 564 | AnimationPlayer::getInstance()->tick(this->dtS); |
---|
[4979] | 565 | // if (this->cycle > 5) |
---|
[4558] | 566 | PhysicsEngine::getInstance()->tick(this->dtS); |
---|
[4396] | 567 | |
---|
[4558] | 568 | ParticleEngine::getInstance()->tick(this->dtS); |
---|
[4396] | 569 | |
---|
[4831] | 570 | |
---|
[4558] | 571 | /** actualy the Graphics Engine should tick the world not the other way around... |
---|
[4555] | 572 | but since we like the things not too complicated we got it this way around |
---|
| 573 | until there is need or time to do it the other way around. |
---|
[4836] | 574 | @todo: GraphicsEngine ticks world: separation of processes and data... |
---|
[4681] | 575 | |
---|
| 576 | bensch: in my opinion the GraphicsEngine could draw the world, but not tick it, |
---|
| 577 | beceause graphics have nothing(or at least not much) to do with Motion. |
---|
[4245] | 578 | */ |
---|
| 579 | GraphicsEngine::getInstance()->tick(this->dtS); |
---|
[2636] | 580 | } |
---|
| 581 | this->lastFrame = currentFrame; |
---|
| 582 | } |
---|
| 583 | |
---|
[3216] | 584 | |
---|
[2636] | 585 | /** |
---|
[4836] | 586 | * this function gives the world a consistant state |
---|
[3551] | 587 | |
---|
| 588 | after ticking (updating the world state) this will give a constistant |
---|
| 589 | state to the whole system. |
---|
| 590 | */ |
---|
| 591 | void World::update() |
---|
| 592 | { |
---|
[5406] | 593 | GraphicsEngine::getInstance()->update(this->dtS); |
---|
[6074] | 594 | PNode::getNullParent()->updateNode (this->dtS); |
---|
[4504] | 595 | SoundEngine::getInstance()->update(); |
---|
[4978] | 596 | //music->update(); |
---|
[3551] | 597 | } |
---|
| 598 | |
---|
| 599 | |
---|
[4917] | 600 | void World::collide() |
---|
| 601 | { |
---|
[6142] | 602 | CDEngine::getInstance()->checkCollisions(this->objectManager.getObjectList(OM_GROUP_00), |
---|
| 603 | this->objectManager.getObjectList(OM_GROUP_01_PROJ)); |
---|
| 604 | CDEngine::getInstance()->checkCollisions(this->objectManager.getObjectList(OM_GROUP_01), |
---|
| 605 | this->objectManager.getObjectList(OM_COMMON)); |
---|
[4917] | 606 | } |
---|
| 607 | |
---|
[3551] | 608 | /** |
---|
[4836] | 609 | * render the current frame |
---|
[4555] | 610 | |
---|
[3225] | 611 | clear all buffers and draw the world |
---|
[2636] | 612 | */ |
---|
| 613 | void World::display () |
---|
| 614 | { |
---|
| 615 | // clear buffer |
---|
| 616 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
| 617 | // set camera |
---|
| 618 | this->localCamera->apply (); |
---|
| 619 | // draw world |
---|
| 620 | this->draw(); |
---|
| 621 | // draw HUD |
---|
[4837] | 622 | /** @todo draw HUD */ |
---|
[2636] | 623 | // flip buffers |
---|
[4681] | 624 | GraphicsEngine::swapBuffers(); |
---|
[3365] | 625 | //SDL_Surface* screen = Orxonox::getInstance()->getScreen (); |
---|
| 626 | //SDL_Flip (screen); |
---|
[2636] | 627 | } |
---|
| 628 | |
---|
[2644] | 629 | |
---|
[3225] | 630 | /** |
---|
[4917] | 631 | * runs through all entities calling their draw() methods |
---|
| 632 | */ |
---|
| 633 | void World::draw () |
---|
| 634 | { |
---|
[6142] | 635 | GraphicsEngine* engine = GraphicsEngine::getInstance(); |
---|
| 636 | engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK)); |
---|
| 637 | engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON)); |
---|
| 638 | engine->draw(State::getObjectManager()->getObjectList(OM_COMMON)); |
---|
| 639 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00)); |
---|
| 640 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01)); |
---|
| 641 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ)); |
---|
[4917] | 642 | |
---|
[6142] | 643 | // { |
---|
| 644 | // if( entity->isVisible() ) entity->draw(); |
---|
| 645 | //FIXME |
---|
| 646 | // if( unlikely( this->showBV)) entity->drawBVTree(3, 226); // to draw the bounding boxes of the objects at level 2 for debug purp |
---|
| 647 | // entity = iterator->nextElement(); |
---|
| 648 | // } |
---|
| 649 | |
---|
[4917] | 650 | ParticleEngine::getInstance()->draw(); |
---|
| 651 | |
---|
[5389] | 652 | if (unlikely(this->showPNodes)) |
---|
[6074] | 653 | PNode::getNullParent()->debugDraw(0); |
---|
[5389] | 654 | |
---|
[6142] | 655 | engine->draw(); |
---|
[4917] | 656 | //TextEngine::getInstance()->draw(); |
---|
| 657 | } |
---|
| 658 | |
---|
[2816] | 659 | |
---|
[3225] | 660 | /** |
---|
[6150] | 661 | * \brief main loop of the world: executing all world relevant function |
---|
| 662 | * |
---|
| 663 | * in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
| 664 | * all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
| 665 | * collisions drawing everything to the screen. |
---|
| 666 | */ |
---|
| 667 | void World::mainLoop() |
---|
[2816] | 668 | { |
---|
[6150] | 669 | this->lastFrame = SDL_GetTicks (); |
---|
| 670 | PRINTF(3)("World::mainLoop() - Entering main loop\n"); |
---|
[3529] | 671 | |
---|
[6151] | 672 | while(!this->bQuitWorld) /* @todo implement pause */ |
---|
[6150] | 673 | { |
---|
| 674 | ++this->cycle; |
---|
| 675 | // Network |
---|
| 676 | this->synchronize (); |
---|
| 677 | // Process input |
---|
| 678 | this->handleInput (); |
---|
[6151] | 679 | if( this->bQuitWorld) |
---|
[6150] | 680 | break; |
---|
| 681 | // Process time |
---|
| 682 | this->tick (); |
---|
| 683 | // Process collision |
---|
| 684 | this->collide (); |
---|
| 685 | // Update the state |
---|
| 686 | this->update (); |
---|
| 687 | // Draw |
---|
| 688 | this->display (); |
---|
| 689 | } |
---|
[3365] | 690 | |
---|
[6150] | 691 | PRINTF(3)("World::mainLoop() - Exiting the main loop\n"); |
---|
[2644] | 692 | } |
---|
[2816] | 693 | |
---|
| 694 | |
---|
[6150] | 695 | |
---|
[3521] | 696 | /** |
---|
[4836] | 697 | * add and spawn a new entity to this world |
---|
| 698 | * @param entity to be added |
---|
[3521] | 699 | */ |
---|
[6150] | 700 | void World::spawn(WorldEntity* entity) |
---|
[3521] | 701 | { |
---|
[6150] | 702 | // this->entities->add (entity); |
---|
| 703 | entity->postSpawn (); |
---|
[3521] | 704 | } |
---|
| 705 | |
---|
[4010] | 706 | void World::setPath( const char* name) |
---|
| 707 | { |
---|
[4094] | 708 | if (this->path) |
---|
| 709 | delete this->path; |
---|
| 710 | if (ResourceManager::isFile(name)) |
---|
| 711 | { |
---|
| 712 | this->path = new char[strlen(name)+1]; |
---|
| 713 | strcpy(this->path, name); |
---|
| 714 | } |
---|
| 715 | else |
---|
| 716 | { |
---|
| 717 | this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1]; |
---|
| 718 | sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name); |
---|
| 719 | } |
---|
[4010] | 720 | } |
---|
| 721 | |
---|
| 722 | const char* World::getPath( void) |
---|
| 723 | { |
---|
| 724 | return path; |
---|
| 725 | } |
---|