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