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