[1853] | 1 | |
---|
| 2 | /* |
---|
| 3 | orxonox - the future of 3D-vertical-scrollers |
---|
| 4 | |
---|
| 5 | Copyright (C) 2004 orx |
---|
| 6 | |
---|
| 7 | This program is free software; you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU General Public License as published by |
---|
| 9 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 10 | any later version. |
---|
[1855] | 11 | |
---|
| 12 | ### File Specific: |
---|
| 13 | main-programmer: Patrick Boenzli |
---|
[2190] | 14 | co-programmer: Christian Meyer |
---|
[1853] | 15 | */ |
---|
| 16 | |
---|
[3590] | 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
| 18 | |
---|
[2190] | 19 | #include "world.h" |
---|
[3608] | 20 | |
---|
| 21 | #include "orxonox.h" |
---|
[3620] | 22 | |
---|
[3608] | 23 | #include "p_node.h" |
---|
| 24 | #include "null_parent.h" |
---|
| 25 | #include "helper_parent.h" |
---|
| 26 | #include "track_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" |
---|
[3608] | 32 | #include "terrain.h" |
---|
[3436] | 33 | #include "light.h" |
---|
[3620] | 34 | |
---|
[3646] | 35 | #include "track_manager.h" |
---|
| 36 | #include "garbage_collector.h" |
---|
| 37 | |
---|
[3608] | 38 | #include "command_node.h" |
---|
| 39 | #include "glmenu_imagescreen.h" |
---|
[3475] | 40 | #include "fontset.h" |
---|
[3608] | 41 | #include "list.h" |
---|
[2036] | 42 | |
---|
[3608] | 43 | |
---|
| 44 | |
---|
[1856] | 45 | using namespace std; |
---|
[1853] | 46 | |
---|
| 47 | |
---|
[3620] | 48 | WorldInterface* WorldInterface::singletonRef = 0; |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | \brief private constructor because of singleton |
---|
| 53 | */ |
---|
| 54 | WorldInterface::WorldInterface() |
---|
| 55 | { |
---|
| 56 | this->worldIsInitialized = false; |
---|
| 57 | this->worldReference = NULL; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | \brief public deconstructor |
---|
| 62 | */ |
---|
| 63 | WorldInterface::~WorldInterface() |
---|
| 64 | { |
---|
| 65 | this->singletonRef = NULL; |
---|
| 66 | this->worldIsInitialized = false; |
---|
| 67 | this->worldReference = NULL; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | \brief gets the singleton instance |
---|
| 72 | \returns singleton instance |
---|
| 73 | */ |
---|
| 74 | WorldInterface* WorldInterface::getInstance() |
---|
| 75 | { |
---|
| 76 | if( singletonRef == NULL) |
---|
| 77 | singletonRef = new WorldInterface(); |
---|
| 78 | return singletonRef; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | \brief initializes the interface |
---|
| 84 | \param reference to the world |
---|
| 85 | |
---|
| 86 | if the worldinterface is not initilizes, there wont be any |
---|
| 87 | useable interface |
---|
| 88 | */ |
---|
| 89 | void WorldInterface::init(World* world) |
---|
| 90 | { |
---|
| 91 | this->worldReference = world; |
---|
[3629] | 92 | if( world != NULL) |
---|
[3620] | 93 | { |
---|
| 94 | this->worldIsInitialized = true; |
---|
| 95 | PRINTF(3)("WorldInterface up and running\n"); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | \brief gets the entity list from the world |
---|
| 102 | \return entity list |
---|
| 103 | */ |
---|
| 104 | tList<WorldEntity>* WorldInterface::getEntityList() |
---|
| 105 | { |
---|
| 106 | if( this->worldIsInitialized) |
---|
| 107 | return this->worldReference->getEntities(); |
---|
| 108 | PRINT(1)("Someone tried to use the WorldInterface before it has been initizlized! this can result in SEGFAULTs!\n"); |
---|
| 109 | return NULL; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | |
---|
[1858] | 114 | /** |
---|
[2551] | 115 | \brief create a new World |
---|
| 116 | |
---|
| 117 | This creates a new empty world! |
---|
[1858] | 118 | */ |
---|
[2636] | 119 | World::World (char* name) |
---|
[1855] | 120 | { |
---|
[3526] | 121 | this->init(name, -1); |
---|
[3573] | 122 | //NullParent* np = NullParent::getInstance(); |
---|
[1855] | 123 | } |
---|
| 124 | |
---|
[3449] | 125 | /** |
---|
| 126 | \brief creates a new World... |
---|
| 127 | \param worldID with this ID |
---|
| 128 | */ |
---|
[2636] | 129 | World::World (int worldID) |
---|
| 130 | { |
---|
[3526] | 131 | this->init(NULL, worldID); |
---|
[2636] | 132 | } |
---|
| 133 | |
---|
[1858] | 134 | /** |
---|
[2551] | 135 | \brief remove the World from memory |
---|
[3365] | 136 | |
---|
| 137 | delete everything explicitly, that isn't contained in the parenting tree! |
---|
| 138 | things contained in the tree are deleted automaticaly |
---|
[1858] | 139 | */ |
---|
[2190] | 140 | World::~World () |
---|
[1872] | 141 | { |
---|
[3546] | 142 | PRINTF(3)("World::~World() - deleting current world\n"); |
---|
[3226] | 143 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
[3220] | 144 | cn->unbind(this->localPlayer); |
---|
| 145 | cn->reset(); |
---|
[3677] | 146 | |
---|
[3676] | 147 | ResourceManager::getInstance()->debug(); |
---|
[3672] | 148 | ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
[3677] | 149 | ResourceManager::getInstance()->debug(); |
---|
[3660] | 150 | |
---|
[3620] | 151 | delete WorldInterface::getInstance(); |
---|
| 152 | |
---|
[3529] | 153 | delete this->nullParent; |
---|
[3553] | 154 | delete this->entities; |
---|
[3597] | 155 | delete this->lightMan; |
---|
[3566] | 156 | delete this->trackManager; |
---|
[1872] | 157 | } |
---|
[1858] | 158 | |
---|
[3526] | 159 | /** |
---|
[3629] | 160 | \brief initializes the world. |
---|
| 161 | |
---|
| 162 | set all stuff here that is world generic and does not use to much memory |
---|
| 163 | because the real init() function StoryEntity::init() will be called |
---|
| 164 | shortly before start of the game. |
---|
| 165 | since all worlds are initiated/referenced before they will be started. |
---|
| 166 | NO LEVEL LOADING HERE - NEVER! |
---|
[3526] | 167 | */ |
---|
| 168 | void World::init(char* name, int worldID) |
---|
| 169 | { |
---|
| 170 | this->setClassName ("World"); |
---|
[2636] | 171 | |
---|
[3631] | 172 | this->worldName = name; |
---|
[3526] | 173 | this->debugWorldNr = worldID; |
---|
| 174 | this->entities = new tList<WorldEntity>(); |
---|
[3629] | 175 | } |
---|
[3526] | 176 | |
---|
[3629] | 177 | |
---|
| 178 | /** |
---|
| 179 | \brief this is executed before load |
---|
| 180 | |
---|
| 181 | since the load function sometimes needs data, that has been init before |
---|
| 182 | the load and after the proceeding storyentity has finished |
---|
| 183 | */ |
---|
| 184 | ErrorMessage World::preLoad() |
---|
| 185 | { |
---|
[3620] | 186 | /* init the world interface */ |
---|
| 187 | WorldInterface* wi = WorldInterface::getInstance(); |
---|
| 188 | wi->init(this); |
---|
[3646] | 189 | this->garbageCollector = GarbageCollector::getInstance(); |
---|
[3526] | 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
[3449] | 193 | /** |
---|
| 194 | \brief loads the World by initializing all resources, and set their default values. |
---|
| 195 | */ |
---|
[3459] | 196 | ErrorMessage World::load() |
---|
[2636] | 197 | { |
---|
[3365] | 198 | // BezierCurve* tmpCurve = new BezierCurve(); |
---|
[2636] | 199 | if(this->debugWorldNr != -1) |
---|
| 200 | { |
---|
[3455] | 201 | // initializing Font |
---|
[3456] | 202 | testFont = new FontSet(); |
---|
| 203 | testFont->buildFont("../data/pictures/font.tga"); |
---|
[3675] | 204 | this->glmis->step(); |
---|
[3455] | 205 | |
---|
| 206 | // initializing the TrackManager |
---|
[3365] | 207 | trackManager = TrackManager::getInstance(); |
---|
[3567] | 208 | trackManager->addPoint(Vector(0,0,0)); |
---|
| 209 | trackManager->addPoint(Vector(100, -40, 5)); |
---|
| 210 | trackManager->addPoint(Vector(200,-40,-8)); |
---|
| 211 | trackManager->addPoint(Vector(250, -35, -2)); |
---|
| 212 | trackManager->addPoint(Vector(320,-33,-.55)); |
---|
[3634] | 213 | trackManager->setDuration(3); |
---|
[3567] | 214 | trackManager->setSavePoint(); |
---|
| 215 | trackManager->addPoint(Vector(410, 0, 0)); |
---|
| 216 | trackManager->addPoint(Vector(510, 20, -10)); |
---|
| 217 | trackManager->addPoint(Vector(550, 20, -10)); |
---|
| 218 | trackManager->addPoint(Vector(570, 20, -10)); |
---|
[3634] | 219 | trackManager->setDuration(5); |
---|
[3567] | 220 | |
---|
| 221 | int fork11, fork12; |
---|
| 222 | trackManager->fork(2, &fork11, &fork12); |
---|
| 223 | trackManager->workOn(fork11); |
---|
| 224 | trackManager->addPoint(Vector(640, 25, -30)); |
---|
| 225 | trackManager->addPoint(Vector(700, 40, -120)); |
---|
| 226 | trackManager->addPoint(Vector(800, 50, -150)); |
---|
| 227 | trackManager->addPoint(Vector(900, 60, -100)); |
---|
| 228 | trackManager->addPoint(Vector(900, 60, -70)); |
---|
| 229 | trackManager->addPoint(Vector(990, 65, -15)); |
---|
| 230 | trackManager->addPoint(Vector(1050, 65, -10)); |
---|
| 231 | trackManager->addPoint(Vector(1100, 65, -20)); |
---|
[3634] | 232 | trackManager->setDuration(10); |
---|
[3567] | 233 | |
---|
[3365] | 234 | trackManager->workOn(fork12); |
---|
[3567] | 235 | trackManager->addPoint(Vector(640, 25, 20)); |
---|
| 236 | trackManager->addPoint(Vector(670, 50, 120)); |
---|
| 237 | trackManager->addPoint(Vector(700, 70, 80)); |
---|
| 238 | trackManager->addPoint(Vector(800, 70, 65)); |
---|
| 239 | trackManager->addPoint(Vector(850, 65, 65)); |
---|
| 240 | trackManager->addPoint(Vector(920, 35, 40)); |
---|
| 241 | trackManager->addPoint(Vector(945, 40, 40)); |
---|
| 242 | trackManager->addPoint(Vector(970, 24, 40)); |
---|
| 243 | trackManager->addPoint(Vector(1000, 40, -7)); |
---|
[3634] | 244 | trackManager->setDuration(10); |
---|
[3567] | 245 | |
---|
[3522] | 246 | |
---|
[3567] | 247 | trackManager->join(2, fork11, fork12); |
---|
| 248 | |
---|
[3522] | 249 | trackManager->workOn(5); |
---|
[3567] | 250 | trackManager->addPoint(Vector(1200, 60, -50)); |
---|
| 251 | trackManager->addPoint(Vector(1300, 50, -50)); |
---|
| 252 | trackManager->addPoint(Vector(1400, 40, -50)); |
---|
| 253 | trackManager->addPoint(Vector(1500, 40, -60)); |
---|
| 254 | trackManager->addPoint(Vector(1600, 35, -55)); |
---|
| 255 | trackManager->addPoint(Vector(1700, 45, -40)); |
---|
| 256 | trackManager->addPoint(Vector(1750, 60, -40)); |
---|
| 257 | trackManager->addPoint(Vector(1770, 80, -40)); |
---|
| 258 | trackManager->addPoint(Vector(1800, 100, -40)); |
---|
[3634] | 259 | trackManager->setDuration(10); |
---|
[3522] | 260 | |
---|
[3433] | 261 | trackManager->finalize(); |
---|
[3522] | 262 | |
---|
[3433] | 263 | |
---|
[3368] | 264 | /*monitor progress*/ |
---|
| 265 | this->glmis->step(); |
---|
| 266 | |
---|
[3602] | 267 | // LIGHT initialisation |
---|
| 268 | lightMan = LightManager::getInstance(); |
---|
| 269 | lightMan->setAmbientColor(.1,.1,.1); |
---|
| 270 | lightMan->addLight(); |
---|
[3603] | 271 | // lightMan->setAttenuation(1.0, .01, 0.0); |
---|
[3602] | 272 | // lightMan->setDiffuseColor(1,1,1); |
---|
| 273 | // lightMan->addLight(1); |
---|
| 274 | // lightMan->setPosition(20, 10, -20); |
---|
| 275 | // lightMan->setDiffuseColor(0,0,0); |
---|
| 276 | lightMan->debug(); |
---|
| 277 | |
---|
[2636] | 278 | switch(this->debugWorldNr) |
---|
| 279 | { |
---|
[3225] | 280 | /* |
---|
| 281 | this loads the hard-coded debug world. this only for simplicity and will be |
---|
| 282 | removed by a reald world-loader, which interprets a world-file. |
---|
| 283 | if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and |
---|
| 284 | make whatever you want... |
---|
| 285 | */ |
---|
[2636] | 286 | case DEBUG_WORLD_0: |
---|
| 287 | { |
---|
[3602] | 288 | lightMan->setPosition(-5.0, 10.0, -40.0); |
---|
[3365] | 289 | this->nullParent = NullParent::getInstance (); |
---|
| 290 | this->nullParent->setName ("NullParent"); |
---|
| 291 | |
---|
[3194] | 292 | // !\todo old track-system has to be removed |
---|
| 293 | |
---|
[3365] | 294 | //create helper for player |
---|
[3586] | 295 | //HelperParent* hp = new HelperParent (); |
---|
[3365] | 296 | /* the player has to be added to this helper */ |
---|
| 297 | |
---|
[2636] | 298 | // create a player |
---|
[3586] | 299 | this->localPlayer = new Player (); |
---|
| 300 | this->localPlayer->setName ("player"); |
---|
| 301 | this->spawn (this->localPlayer); |
---|
[3368] | 302 | /*monitor progress*/ |
---|
[3675] | 303 | //this->glmis->step(); |
---|
| 304 | this->glmis->step(); |
---|
[3368] | 305 | |
---|
[2636] | 306 | // bind input |
---|
[3365] | 307 | Orxonox *orx = Orxonox::getInstance (); |
---|
[3586] | 308 | orx->getLocalInput()->bind (this->localPlayer); |
---|
[2636] | 309 | |
---|
| 310 | // bind camera |
---|
[3635] | 311 | this->localCamera = new Camera(); |
---|
[3365] | 312 | this->localCamera->setName ("camera"); |
---|
[3635] | 313 | this->localCamera->lookAt(this->localPlayer); |
---|
[3643] | 314 | this->localCamera->setParent(this->localPlayer); |
---|
[3637] | 315 | |
---|
[3507] | 316 | /*monitor progress*/ |
---|
[3675] | 317 | this->glmis->step(); |
---|
[2816] | 318 | |
---|
[3419] | 319 | // Create SkySphere |
---|
[3545] | 320 | this->skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
| 321 | this->skySphere->setName("SkySphere"); |
---|
[3527] | 322 | this->localCamera->addChild(this->skySphere); |
---|
[3565] | 323 | this->skySphere->setMode(PNODE_MOVEMENT); |
---|
[3419] | 324 | |
---|
[3368] | 325 | /*monitor progress*/ |
---|
| 326 | this->glmis->step(); |
---|
| 327 | |
---|
[3521] | 328 | |
---|
| 329 | WorldEntity* env = new Environment(); |
---|
| 330 | env->setName ("env"); |
---|
| 331 | this->spawn(env); |
---|
| 332 | |
---|
[3586] | 333 | |
---|
[3670] | 334 | /* |
---|
[3587] | 335 | Vector* es = new Vector (10, 5, 0); |
---|
| 336 | Quaternion* qs = new Quaternion (); |
---|
[3614] | 337 | WorldEntity* pr = new Primitive(P_CYLINDER); |
---|
[3587] | 338 | pr->setName("primitive"); |
---|
| 339 | this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT); |
---|
[3670] | 340 | */ |
---|
[3521] | 341 | |
---|
[3368] | 342 | /*monitor progress*/ |
---|
| 343 | this->glmis->step(); |
---|
[2816] | 344 | |
---|
[3535] | 345 | // trackManager->setBindSlave(env); |
---|
[3556] | 346 | PNode* tn = trackManager->getTrackNode(); |
---|
[3586] | 347 | tn->addChild(this->localPlayer); |
---|
[3539] | 348 | |
---|
[3537] | 349 | //localCamera->setParent(TrackNode::getInstance()); |
---|
[3643] | 350 | tn->addChild(this->localCamera); |
---|
| 351 | // localCamera->lookAt(tn); |
---|
[3634] | 352 | this->localPlayer->setMode(PNODE_ALL); |
---|
[3538] | 353 | //Vector* cameraOffset = new Vector (0, 5, -10); |
---|
[3596] | 354 | trackManager->condition(2, LEFTRIGHT, this->localPlayer); |
---|
[3675] | 355 | this->glmis->step(); |
---|
[3522] | 356 | |
---|
[2636] | 357 | break; |
---|
| 358 | } |
---|
| 359 | case DEBUG_WORLD_1: |
---|
| 360 | { |
---|
[3602] | 361 | lightMan->setPosition(.0, .0, .0); |
---|
[3603] | 362 | lightMan->setAttenuation(1.0, .01, 0.0); |
---|
| 363 | lightMan->setSpecularColor(1,0,0); |
---|
[3365] | 364 | this->nullParent = NullParent::getInstance (); |
---|
| 365 | this->nullParent->setName ("NullParent"); |
---|
| 366 | |
---|
[2636] | 367 | // create a player |
---|
[2644] | 368 | WorldEntity* myPlayer = new Player(); |
---|
[3365] | 369 | myPlayer->setName ("player"); |
---|
[2644] | 370 | this->spawn(myPlayer); |
---|
[3194] | 371 | this->localPlayer = myPlayer; |
---|
[2636] | 372 | |
---|
| 373 | // bind input |
---|
| 374 | Orxonox *orx = Orxonox::getInstance(); |
---|
[3226] | 375 | orx->getLocalInput()->bind (myPlayer); |
---|
[2636] | 376 | |
---|
| 377 | // bind camera |
---|
[3635] | 378 | this->localCamera = new Camera (); |
---|
[3365] | 379 | this->localCamera->setName ("camera"); |
---|
[3637] | 380 | this->localCamera->lookAt(LightManager::getInstance()->getLight(0)); |
---|
| 381 | this->localCamera->setParent(this->localPlayer); |
---|
[3429] | 382 | |
---|
| 383 | // Create SkySphere |
---|
| 384 | skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
[3502] | 385 | this->localPlayer->addChild(this->skySphere); |
---|
[3429] | 386 | |
---|
[3586] | 387 | Vector* es = new Vector (20, 0, 0); |
---|
| 388 | Quaternion* qs = new Quaternion (); |
---|
| 389 | |
---|
[3602] | 390 | lightMan->getLight(0)->setParent(trackManager->getTrackNode()); |
---|
[2636] | 391 | break; |
---|
| 392 | } |
---|
| 393 | default: |
---|
| 394 | printf("World::load() - no world with ID %i found", this->debugWorldNr ); |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | else if(this->worldName != NULL) |
---|
| 398 | { |
---|
| 399 | |
---|
| 400 | } |
---|
[2731] | 401 | |
---|
| 402 | // initialize debug coord system |
---|
| 403 | objectList = glGenLists(1); |
---|
| 404 | glNewList (objectList, GL_COMPILE); |
---|
[3559] | 405 | |
---|
[3365] | 406 | trackManager->drawGraph(.01); |
---|
| 407 | trackManager->debug(2); |
---|
[2731] | 408 | glEndList(); |
---|
[3526] | 409 | |
---|
[3567] | 410 | terrain = new Terrain("../data/worlds/newGround.obj"); |
---|
[3568] | 411 | terrain->setRelCoor(new Vector(0,-10,0)); |
---|
[3559] | 412 | this->spawn(terrain); |
---|
[3455] | 413 | |
---|
[2636] | 414 | } |
---|
| 415 | |
---|
[3629] | 416 | |
---|
[3459] | 417 | /** |
---|
[3629] | 418 | \brief initializes a new World shortly before start |
---|
| 419 | |
---|
| 420 | this is the function, that will be loaded shortly before the world is |
---|
| 421 | started |
---|
[3459] | 422 | */ |
---|
| 423 | ErrorMessage World::init() |
---|
| 424 | { |
---|
| 425 | this->bPause = false; |
---|
| 426 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
| 427 | cn->addToWorld(this); |
---|
| 428 | cn->enable(true); |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | |
---|
| 432 | /** |
---|
| 433 | \brief starts the World |
---|
| 434 | */ |
---|
| 435 | ErrorMessage World::start() |
---|
| 436 | { |
---|
[3546] | 437 | PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr); |
---|
[3459] | 438 | this->bQuitOrxonox = false; |
---|
| 439 | this->bQuitCurrentGame = false; |
---|
| 440 | this->mainLoop(); |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | /** |
---|
| 444 | \brief stops the world. |
---|
| 445 | |
---|
| 446 | This happens, when the player decides to end the Level. |
---|
| 447 | */ |
---|
| 448 | ErrorMessage World::stop() |
---|
| 449 | { |
---|
[3546] | 450 | PRINTF(3)("World::stop() - got stop signal\n"); |
---|
[3459] | 451 | this->bQuitCurrentGame = true; |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | /** |
---|
| 455 | \brief pauses the Game |
---|
| 456 | */ |
---|
| 457 | ErrorMessage World::pause() |
---|
| 458 | { |
---|
| 459 | this->isPaused = true; |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | /** |
---|
| 463 | \brief ends the pause Phase |
---|
| 464 | */ |
---|
| 465 | ErrorMessage World::resume() |
---|
| 466 | { |
---|
| 467 | this->isPaused = false; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | /** |
---|
| 471 | \brief destroys the World |
---|
| 472 | */ |
---|
| 473 | ErrorMessage World::destroy() |
---|
| 474 | { |
---|
[3566] | 475 | |
---|
[3459] | 476 | } |
---|
| 477 | |
---|
| 478 | /** |
---|
| 479 | \brief shows the loading screen |
---|
| 480 | */ |
---|
| 481 | void World::displayLoadScreen () |
---|
| 482 | { |
---|
[3546] | 483 | PRINTF(3)("World::displayLoadScreen - start\n"); |
---|
[3459] | 484 | |
---|
| 485 | //GLMenuImageScreen* |
---|
| 486 | this->glmis = GLMenuImageScreen::getInstance(); |
---|
| 487 | this->glmis->init(); |
---|
[3675] | 488 | this->glmis->setMaximum(8); |
---|
[3459] | 489 | this->glmis->draw(); |
---|
| 490 | |
---|
[3546] | 491 | PRINTF(3)("World::displayLoadScreen - end\n"); |
---|
[3459] | 492 | } |
---|
| 493 | |
---|
| 494 | /** |
---|
| 495 | \brief removes the loadscreen, and changes over to the game |
---|
| 496 | |
---|
| 497 | \todo take out the delay |
---|
| 498 | */ |
---|
| 499 | void World::releaseLoadScreen () |
---|
| 500 | { |
---|
[3546] | 501 | PRINTF(3)("World::releaseLoadScreen - start\n"); |
---|
[3459] | 502 | this->glmis->setValue(this->glmis->getMaximum()); |
---|
[3675] | 503 | //SDL_Delay(500); |
---|
[3546] | 504 | PRINTF(3)("World::releaseLoadScreen - end\n"); |
---|
[3459] | 505 | } |
---|
| 506 | |
---|
| 507 | |
---|
[3620] | 508 | /** |
---|
| 509 | \brief gets the list of entities from the world |
---|
| 510 | \returns entity list |
---|
| 511 | */ |
---|
| 512 | tList<WorldEntity>* World::getEntities() |
---|
| 513 | { |
---|
| 514 | return this->entities; |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | |
---|
[3646] | 518 | /** |
---|
| 519 | \brief this returns the current game time |
---|
| 520 | \returns elapsed game time |
---|
| 521 | */ |
---|
| 522 | double World::getGameTime() |
---|
| 523 | { |
---|
| 524 | return this->gameTime; |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | |
---|
[2636] | 528 | /** |
---|
[2551] | 529 | \brief checks for collisions |
---|
| 530 | |
---|
| 531 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
| 532 | between them. In case of collisions the collide() method of the corresponding entities |
---|
| 533 | is called. |
---|
[1858] | 534 | */ |
---|
[2190] | 535 | void World::collide () |
---|
[1858] | 536 | { |
---|
[2816] | 537 | /* |
---|
| 538 | List *a, *b; |
---|
[2551] | 539 | WorldEntity *aobj, *bobj; |
---|
[2816] | 540 | |
---|
| 541 | a = entities; |
---|
[2551] | 542 | |
---|
| 543 | while( a != NULL) |
---|
| 544 | { |
---|
[2816] | 545 | aobj = a->nextElement(); |
---|
[2551] | 546 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
[2190] | 547 | { |
---|
[2816] | 548 | b = a->nextElement(); |
---|
[2551] | 549 | while( b != NULL ) |
---|
| 550 | { |
---|
[2816] | 551 | bobj = b->nextElement(); |
---|
[2551] | 552 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
[2190] | 553 | { |
---|
[2551] | 554 | unsigned long ahitflg, bhitflg; |
---|
| 555 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
| 556 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
| 557 | &bhitflg) ); |
---|
| 558 | { |
---|
| 559 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
| 560 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
| 561 | } |
---|
[2190] | 562 | } |
---|
[2816] | 563 | b = b->nextElement(); |
---|
[2551] | 564 | } |
---|
[2190] | 565 | } |
---|
[2816] | 566 | a = a->enumerate(); |
---|
[2551] | 567 | } |
---|
[2816] | 568 | */ |
---|
[1858] | 569 | } |
---|
| 570 | |
---|
| 571 | /** |
---|
[2551] | 572 | \brief runs through all entities calling their draw() methods |
---|
[1931] | 573 | */ |
---|
[2190] | 574 | void World::draw () |
---|
[2077] | 575 | { |
---|
[3462] | 576 | /* draw entities */ |
---|
[2551] | 577 | WorldEntity* entity; |
---|
[3526] | 578 | glLoadIdentity(); |
---|
| 579 | |
---|
[3653] | 580 | //entity = this->entities->enumerate(); |
---|
| 581 | tIterator<WorldEntity>* iterator = this->entities->getIterator(); |
---|
| 582 | entity = iterator->nextElement(); |
---|
[2816] | 583 | while( entity != NULL ) |
---|
[2551] | 584 | { |
---|
[2822] | 585 | if( entity->bDraw ) entity->draw(); |
---|
[3653] | 586 | //entity = this->entities->nextElement(); |
---|
| 587 | entity = iterator->nextElement(); |
---|
| 588 | } |
---|
| 589 | delete iterator; |
---|
[2551] | 590 | |
---|
[2731] | 591 | glCallList (objectList); |
---|
[3419] | 592 | //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list. |
---|
| 593 | skySphere->draw(); |
---|
| 594 | |
---|
[3456] | 595 | testFont->printText(0, 0, 1, "orxonox_" PACKAGE_VERSION); |
---|
[3455] | 596 | |
---|
[3602] | 597 | lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes // |
---|
[1931] | 598 | } |
---|
| 599 | |
---|
[2816] | 600 | |
---|
[2190] | 601 | /** |
---|
[3225] | 602 | \brief function to put your own debug stuff into it. it can display informations about |
---|
| 603 | the current class/procedure |
---|
| 604 | */ |
---|
[2640] | 605 | void World::debug() |
---|
| 606 | { |
---|
[3546] | 607 | PRINTF(2)("debug() - starting debug\n"); |
---|
[3365] | 608 | PNode* p1 = NullParent::getInstance (); |
---|
| 609 | PNode* p2 = new PNode (new Vector(2, 2, 2), p1); |
---|
| 610 | PNode* p3 = new PNode (new Vector(4, 4, 4), p1); |
---|
| 611 | PNode* p4 = new PNode (new Vector(6, 6, 6), p2); |
---|
| 612 | |
---|
| 613 | p1->debug (); |
---|
| 614 | p2->debug (); |
---|
| 615 | p3->debug (); |
---|
| 616 | p4->debug (); |
---|
| 617 | |
---|
| 618 | p1->shiftCoor (new Vector(-1, -1, -1)); |
---|
| 619 | |
---|
| 620 | printf("World::debug() - shift\n"); |
---|
| 621 | p1->debug (); |
---|
| 622 | p2->debug (); |
---|
| 623 | p3->debug (); |
---|
| 624 | p4->debug (); |
---|
| 625 | |
---|
[3644] | 626 | p1->update (0); |
---|
[3365] | 627 | |
---|
| 628 | printf ("World::debug() - update\n"); |
---|
| 629 | p1->debug (); |
---|
| 630 | p2->debug (); |
---|
| 631 | p3->debug (); |
---|
| 632 | p4->debug (); |
---|
| 633 | |
---|
| 634 | p2->shiftCoor (new Vector(-1, -1, -1)); |
---|
[3644] | 635 | p1->update (0); |
---|
[3365] | 636 | |
---|
| 637 | p1->debug (); |
---|
| 638 | p2->debug (); |
---|
| 639 | p3->debug (); |
---|
| 640 | p4->debug (); |
---|
| 641 | |
---|
| 642 | p2->setAbsCoor (new Vector(1,2,3)); |
---|
| 643 | |
---|
| 644 | |
---|
[3644] | 645 | p1->update (0); |
---|
[3365] | 646 | |
---|
| 647 | p1->debug (); |
---|
| 648 | p2->debug (); |
---|
| 649 | p3->debug (); |
---|
| 650 | p4->debug (); |
---|
| 651 | |
---|
[3544] | 652 | delete p1; |
---|
[3365] | 653 | |
---|
| 654 | |
---|
| 655 | /* |
---|
[2640] | 656 | WorldEntity* entity; |
---|
| 657 | printf("counting all entities\n"); |
---|
[2816] | 658 | printf("World::debug() - enumerate()\n"); |
---|
| 659 | entity = entities->enumerate(); |
---|
| 660 | while( entity != NULL ) |
---|
[2640] | 661 | { |
---|
| 662 | if( entity->bDraw ) printf("got an entity\n"); |
---|
[2816] | 663 | entity = entities->nextElement(); |
---|
[2640] | 664 | } |
---|
[3365] | 665 | */ |
---|
[2640] | 666 | } |
---|
[2636] | 667 | |
---|
[2640] | 668 | |
---|
[3449] | 669 | /** |
---|
[3225] | 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 | */ |
---|
[2636] | 676 | void World::mainLoop() |
---|
| 677 | { |
---|
[3365] | 678 | this->lastFrame = SDL_GetTicks (); |
---|
[3546] | 679 | PRINTF(3)("World::mainLoop() - Entering main loop\n"); |
---|
[3215] | 680 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
[2551] | 681 | { |
---|
[3546] | 682 | PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize()); |
---|
[2636] | 683 | // Network |
---|
[3365] | 684 | this->synchronize (); |
---|
[2636] | 685 | // Process input |
---|
[3365] | 686 | this->handleInput (); |
---|
[3215] | 687 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
| 688 | break; |
---|
[2636] | 689 | // Process time |
---|
[3551] | 690 | this->tick (); |
---|
| 691 | // Update the state |
---|
| 692 | this->update (); |
---|
[2636] | 693 | // Process collision |
---|
[3459] | 694 | this->collide (); |
---|
[2636] | 695 | // Draw |
---|
[3365] | 696 | this->display (); |
---|
[3548] | 697 | |
---|
[3565] | 698 | // for( int i = 0; i < 5000000; i++) {} |
---|
[3365] | 699 | /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/ |
---|
[2551] | 700 | } |
---|
[3546] | 701 | PRINTF(3)("World::mainLoop() - Exiting the main loop\n"); |
---|
[1899] | 702 | } |
---|
| 703 | |
---|
[3459] | 704 | |
---|
[2190] | 705 | /** |
---|
[2636] | 706 | \brief synchronize local data with remote data |
---|
[1855] | 707 | */ |
---|
[2636] | 708 | void World::synchronize () |
---|
[1855] | 709 | { |
---|
[2636] | 710 | // Get remote input |
---|
| 711 | // Update synchronizables |
---|
[1855] | 712 | } |
---|
[2636] | 713 | |
---|
[3459] | 714 | |
---|
[2636] | 715 | /** |
---|
| 716 | \brief run all input processing |
---|
[3225] | 717 | |
---|
| 718 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
| 719 | sdl and has its own event-passing-queue. |
---|
[2636] | 720 | */ |
---|
[3225] | 721 | void World::handleInput () |
---|
[2636] | 722 | { |
---|
| 723 | // localinput |
---|
[3225] | 724 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
[3216] | 725 | cn->process(); |
---|
[2636] | 726 | // remoteinput |
---|
| 727 | } |
---|
| 728 | |
---|
[3459] | 729 | |
---|
[2636] | 730 | /** |
---|
| 731 | \brief advance the timeline |
---|
[3225] | 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. |
---|
[2636] | 736 | */ |
---|
[3551] | 737 | void World::tick () |
---|
[2636] | 738 | { |
---|
| 739 | Uint32 currentFrame = SDL_GetTicks(); |
---|
| 740 | if(!this->bPause) |
---|
| 741 | { |
---|
[3644] | 742 | this->dt = currentFrame - this->lastFrame; |
---|
[2816] | 743 | |
---|
[3646] | 744 | if( this->dt > 0) |
---|
[2636] | 745 | { |
---|
| 746 | float fps = 1000/dt; |
---|
[3546] | 747 | PRINTF(3)("fps = %f\n", fps); |
---|
[2636] | 748 | } |
---|
| 749 | else |
---|
| 750 | { |
---|
[3225] | 751 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
| 752 | nothing. |
---|
| 753 | */ |
---|
[3546] | 754 | PRINTF(2)("fps = 1000 - frame rate is adjusted\n"); |
---|
[3194] | 755 | SDL_Delay(10); |
---|
[3646] | 756 | this->dt = 10; |
---|
[2636] | 757 | } |
---|
[3459] | 758 | //this->timeSlice (dt); |
---|
| 759 | |
---|
| 760 | /* function to let all entities tick (iterate through list) */ |
---|
[3646] | 761 | float seconds = this->dt / 1000.0; |
---|
| 762 | this->gameTime += seconds; |
---|
[3654] | 763 | //entity = entities->enumerate(); |
---|
| 764 | tIterator<WorldEntity>* iterator = this->entities->getIterator(); |
---|
| 765 | WorldEntity* entity = iterator->nextElement(); |
---|
[3459] | 766 | while( entity != NULL) |
---|
| 767 | { |
---|
| 768 | entity->tick (seconds); |
---|
[3654] | 769 | entity = iterator->nextElement(); |
---|
[3459] | 770 | } |
---|
[3654] | 771 | delete iterator; |
---|
[3502] | 772 | //skySphere->updatePosition(localCamera->absCoordinate); |
---|
[3459] | 773 | |
---|
| 774 | /* update tick the rest */ |
---|
[3646] | 775 | this->trackManager->tick(this->dt); |
---|
| 776 | this->localCamera->tick(this->dt); |
---|
| 777 | this->garbageCollector->tick(seconds); |
---|
[2636] | 778 | } |
---|
| 779 | this->lastFrame = currentFrame; |
---|
| 780 | } |
---|
| 781 | |
---|
[3216] | 782 | |
---|
[2636] | 783 | /** |
---|
[3551] | 784 | \brief this function gives the world a consistant state |
---|
| 785 | |
---|
| 786 | after ticking (updating the world state) this will give a constistant |
---|
| 787 | state to the whole system. |
---|
| 788 | */ |
---|
| 789 | void World::update() |
---|
| 790 | { |
---|
[3646] | 791 | this->garbageCollector->update(); |
---|
[3644] | 792 | this->nullParent->update (dt); |
---|
[3551] | 793 | } |
---|
| 794 | |
---|
| 795 | |
---|
| 796 | /** |
---|
[3225] | 797 | \brief render the current frame |
---|
| 798 | |
---|
| 799 | clear all buffers and draw the world |
---|
[2636] | 800 | */ |
---|
| 801 | void World::display () |
---|
| 802 | { |
---|
| 803 | // clear buffer |
---|
| 804 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
| 805 | // set camera |
---|
| 806 | this->localCamera->apply (); |
---|
| 807 | // draw world |
---|
| 808 | this->draw(); |
---|
| 809 | // draw HUD |
---|
[3365] | 810 | /* \todo draw HUD */ |
---|
[2636] | 811 | // flip buffers |
---|
| 812 | SDL_GL_SwapBuffers(); |
---|
[3365] | 813 | //SDL_Surface* screen = Orxonox::getInstance()->getScreen (); |
---|
| 814 | //SDL_Flip (screen); |
---|
[2636] | 815 | } |
---|
| 816 | |
---|
[2644] | 817 | |
---|
[3225] | 818 | /** |
---|
| 819 | \brief add and spawn a new entity to this world |
---|
| 820 | \param entity to be added |
---|
| 821 | */ |
---|
[2644] | 822 | void World::spawn(WorldEntity* entity) |
---|
| 823 | { |
---|
[3365] | 824 | this->entities->add (entity); |
---|
[3233] | 825 | entity->postSpawn (); |
---|
[2816] | 826 | } |
---|
| 827 | |
---|
| 828 | |
---|
[3225] | 829 | /** |
---|
| 830 | \brief add and spawn a new entity to this world |
---|
| 831 | \param entity to be added |
---|
[3449] | 832 | \param absCoor At what coordinates to add this entity. |
---|
| 833 | \param absDir In which direction should it look. |
---|
[3225] | 834 | */ |
---|
[3365] | 835 | void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir) |
---|
[2816] | 836 | { |
---|
[3529] | 837 | this->entities->add (entity); |
---|
| 838 | |
---|
[3365] | 839 | entity->setAbsCoor (absCoor); |
---|
| 840 | entity->setAbsDir (absDir); |
---|
| 841 | |
---|
[3233] | 842 | entity->postSpawn (); |
---|
[2644] | 843 | } |
---|
[2816] | 844 | |
---|
| 845 | |
---|
[3521] | 846 | /** |
---|
| 847 | \brief add and spawn a new entity to this world |
---|
| 848 | \param entity to be added |
---|
| 849 | \param entity to be added to (PNode) |
---|
| 850 | \param At what relative coordinates to add this entity. |
---|
| 851 | \param In which relative direction should it look. |
---|
| 852 | */ |
---|
| 853 | void World::spawn(WorldEntity* entity, PNode* parentNode, |
---|
| 854 | Vector* relCoor, Quaternion* relDir, |
---|
[3565] | 855 | int parentingMode) |
---|
[3521] | 856 | { |
---|
[3551] | 857 | this->nullParent = NullParent::getInstance(); |
---|
[3529] | 858 | if( parentNode != NULL) |
---|
[3521] | 859 | { |
---|
| 860 | parentNode->addChild (entity); |
---|
| 861 | |
---|
| 862 | entity->setRelCoor (relCoor); |
---|
| 863 | entity->setRelDir (relDir); |
---|
[3586] | 864 | entity->setMode(parentingMode); |
---|
[3521] | 865 | |
---|
| 866 | this->entities->add (entity); |
---|
| 867 | |
---|
| 868 | entity->postSpawn (); |
---|
| 869 | } |
---|
| 870 | } |
---|
| 871 | |
---|
| 872 | |
---|
| 873 | |
---|
[3449] | 874 | /** |
---|
[3225] | 875 | \brief commands that the world must catch |
---|
| 876 | \returns false if not used by the world |
---|
| 877 | */ |
---|
[3216] | 878 | bool World::command(Command* cmd) |
---|
| 879 | { |
---|
[3640] | 880 | if( !strcmp( cmd->cmd, "view0")) this->localCamera->setViewMode(VIEW_NORMAL); |
---|
| 881 | else if( !strcmp( cmd->cmd, "view1")) this->localCamera->setViewMode(VIEW_BEHIND); |
---|
| 882 | else if( !strcmp( cmd->cmd, "view2")) this->localCamera->setViewMode(VIEW_FRONT); |
---|
| 883 | else if( !strcmp( cmd->cmd, "view3")) this->localCamera->setViewMode(VIEW_LEFT); |
---|
| 884 | else if( !strcmp( cmd->cmd, "view4")) this->localCamera->setViewMode(VIEW_RIGHT); |
---|
[3643] | 885 | else if( !strcmp( cmd->cmd, "view5")) this->localCamera->setViewMode(VIEW_TOP); |
---|
[3640] | 886 | |
---|
[3216] | 887 | return false; |
---|
| 888 | } |
---|
[3365] | 889 | |
---|