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