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