[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" |
---|
[3693] | 40 | #include "glfont.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 |
---|
[3754] | 202 | tmpFont = new GLFont("../data/fonts/earth.ttf"); |
---|
[3675] | 203 | this->glmis->step(); |
---|
[3455] | 204 | // initializing the TrackManager |
---|
[3365] | 205 | trackManager = TrackManager::getInstance(); |
---|
[3567] | 206 | trackManager->addPoint(Vector(0,0,0)); |
---|
| 207 | trackManager->addPoint(Vector(100, -40, 5)); |
---|
| 208 | trackManager->addPoint(Vector(200,-40,-8)); |
---|
| 209 | trackManager->addPoint(Vector(250, -35, -2)); |
---|
| 210 | trackManager->addPoint(Vector(320,-33,-.55)); |
---|
[3634] | 211 | trackManager->setDuration(3); |
---|
[3567] | 212 | trackManager->setSavePoint(); |
---|
| 213 | trackManager->addPoint(Vector(410, 0, 0)); |
---|
| 214 | trackManager->addPoint(Vector(510, 20, -10)); |
---|
| 215 | trackManager->addPoint(Vector(550, 20, -10)); |
---|
| 216 | trackManager->addPoint(Vector(570, 20, -10)); |
---|
[3634] | 217 | trackManager->setDuration(5); |
---|
[3567] | 218 | |
---|
| 219 | int fork11, fork12; |
---|
| 220 | trackManager->fork(2, &fork11, &fork12); |
---|
| 221 | trackManager->workOn(fork11); |
---|
| 222 | trackManager->addPoint(Vector(640, 25, -30)); |
---|
| 223 | trackManager->addPoint(Vector(700, 40, -120)); |
---|
| 224 | trackManager->addPoint(Vector(800, 50, -150)); |
---|
| 225 | trackManager->addPoint(Vector(900, 60, -100)); |
---|
| 226 | trackManager->addPoint(Vector(900, 60, -70)); |
---|
| 227 | trackManager->addPoint(Vector(990, 65, -15)); |
---|
| 228 | trackManager->addPoint(Vector(1050, 65, -10)); |
---|
| 229 | trackManager->addPoint(Vector(1100, 65, -20)); |
---|
[3634] | 230 | trackManager->setDuration(10); |
---|
[3567] | 231 | |
---|
[3365] | 232 | trackManager->workOn(fork12); |
---|
[3567] | 233 | trackManager->addPoint(Vector(640, 25, 20)); |
---|
| 234 | trackManager->addPoint(Vector(670, 50, 120)); |
---|
| 235 | trackManager->addPoint(Vector(700, 70, 80)); |
---|
| 236 | trackManager->addPoint(Vector(800, 70, 65)); |
---|
| 237 | trackManager->addPoint(Vector(850, 65, 65)); |
---|
| 238 | trackManager->addPoint(Vector(920, 35, 40)); |
---|
| 239 | trackManager->addPoint(Vector(945, 40, 40)); |
---|
| 240 | trackManager->addPoint(Vector(970, 24, 40)); |
---|
| 241 | trackManager->addPoint(Vector(1000, 40, -7)); |
---|
[3634] | 242 | trackManager->setDuration(10); |
---|
[3567] | 243 | |
---|
[3522] | 244 | |
---|
[3567] | 245 | trackManager->join(2, fork11, fork12); |
---|
| 246 | |
---|
[3522] | 247 | trackManager->workOn(5); |
---|
[3567] | 248 | trackManager->addPoint(Vector(1200, 60, -50)); |
---|
| 249 | trackManager->addPoint(Vector(1300, 50, -50)); |
---|
| 250 | trackManager->addPoint(Vector(1400, 40, -50)); |
---|
| 251 | trackManager->addPoint(Vector(1500, 40, -60)); |
---|
| 252 | trackManager->addPoint(Vector(1600, 35, -55)); |
---|
| 253 | trackManager->addPoint(Vector(1700, 45, -40)); |
---|
| 254 | trackManager->addPoint(Vector(1750, 60, -40)); |
---|
| 255 | trackManager->addPoint(Vector(1770, 80, -40)); |
---|
| 256 | trackManager->addPoint(Vector(1800, 100, -40)); |
---|
[3634] | 257 | trackManager->setDuration(10); |
---|
[3522] | 258 | |
---|
[3433] | 259 | trackManager->finalize(); |
---|
[3522] | 260 | |
---|
[3433] | 261 | |
---|
[3368] | 262 | /*monitor progress*/ |
---|
| 263 | this->glmis->step(); |
---|
| 264 | |
---|
[3602] | 265 | // LIGHT initialisation |
---|
| 266 | lightMan = LightManager::getInstance(); |
---|
| 267 | lightMan->setAmbientColor(.1,.1,.1); |
---|
| 268 | lightMan->addLight(); |
---|
[3603] | 269 | // lightMan->setAttenuation(1.0, .01, 0.0); |
---|
[3602] | 270 | // lightMan->setDiffuseColor(1,1,1); |
---|
| 271 | // lightMan->addLight(1); |
---|
| 272 | // lightMan->setPosition(20, 10, -20); |
---|
| 273 | // lightMan->setDiffuseColor(0,0,0); |
---|
| 274 | lightMan->debug(); |
---|
| 275 | |
---|
[2636] | 276 | switch(this->debugWorldNr) |
---|
| 277 | { |
---|
[3225] | 278 | /* |
---|
| 279 | this loads the hard-coded debug world. this only for simplicity and will be |
---|
| 280 | removed by a reald world-loader, which interprets a world-file. |
---|
| 281 | if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and |
---|
| 282 | make whatever you want... |
---|
| 283 | */ |
---|
[2636] | 284 | case DEBUG_WORLD_0: |
---|
| 285 | { |
---|
[3602] | 286 | lightMan->setPosition(-5.0, 10.0, -40.0); |
---|
[3365] | 287 | this->nullParent = NullParent::getInstance (); |
---|
| 288 | this->nullParent->setName ("NullParent"); |
---|
| 289 | |
---|
[3194] | 290 | // !\todo old track-system has to be removed |
---|
| 291 | |
---|
[3365] | 292 | //create helper for player |
---|
[3586] | 293 | //HelperParent* hp = new HelperParent (); |
---|
[3365] | 294 | /* the player has to be added to this helper */ |
---|
| 295 | |
---|
[2636] | 296 | // create a player |
---|
[3586] | 297 | this->localPlayer = new Player (); |
---|
| 298 | this->localPlayer->setName ("player"); |
---|
| 299 | this->spawn (this->localPlayer); |
---|
[3368] | 300 | /*monitor progress*/ |
---|
[3715] | 301 | //this->glmis->step(); |
---|
[3675] | 302 | this->glmis->step(); |
---|
[3368] | 303 | |
---|
[2636] | 304 | // bind input |
---|
[3365] | 305 | Orxonox *orx = Orxonox::getInstance (); |
---|
[3586] | 306 | orx->getLocalInput()->bind (this->localPlayer); |
---|
[2636] | 307 | |
---|
| 308 | // bind camera |
---|
[3635] | 309 | this->localCamera = new Camera(); |
---|
[3365] | 310 | this->localCamera->setName ("camera"); |
---|
[3635] | 311 | this->localCamera->lookAt(this->localPlayer); |
---|
[3643] | 312 | this->localCamera->setParent(this->localPlayer); |
---|
[3637] | 313 | |
---|
[3507] | 314 | /*monitor progress*/ |
---|
[3675] | 315 | this->glmis->step(); |
---|
[2816] | 316 | |
---|
[3419] | 317 | // Create SkySphere |
---|
[3545] | 318 | this->skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
| 319 | this->skySphere->setName("SkySphere"); |
---|
[3527] | 320 | this->localCamera->addChild(this->skySphere); |
---|
[3565] | 321 | this->skySphere->setMode(PNODE_MOVEMENT); |
---|
[3419] | 322 | |
---|
[3368] | 323 | /*monitor progress*/ |
---|
| 324 | this->glmis->step(); |
---|
| 325 | |
---|
[3521] | 326 | |
---|
| 327 | WorldEntity* env = new Environment(); |
---|
| 328 | env->setName ("env"); |
---|
| 329 | this->spawn(env); |
---|
| 330 | |
---|
[3586] | 331 | |
---|
[3670] | 332 | /* |
---|
[3587] | 333 | Vector* es = new Vector (10, 5, 0); |
---|
| 334 | Quaternion* qs = new Quaternion (); |
---|
[3614] | 335 | WorldEntity* pr = new Primitive(P_CYLINDER); |
---|
[3587] | 336 | pr->setName("primitive"); |
---|
| 337 | this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT); |
---|
[3670] | 338 | */ |
---|
[3521] | 339 | |
---|
[3368] | 340 | /*monitor progress*/ |
---|
| 341 | this->glmis->step(); |
---|
[2816] | 342 | |
---|
[3535] | 343 | // trackManager->setBindSlave(env); |
---|
[3556] | 344 | PNode* tn = trackManager->getTrackNode(); |
---|
[3586] | 345 | tn->addChild(this->localPlayer); |
---|
[3715] | 346 | tmpFont->setBindNode(tn); |
---|
[3539] | 347 | |
---|
[3537] | 348 | //localCamera->setParent(TrackNode::getInstance()); |
---|
[3643] | 349 | tn->addChild(this->localCamera); |
---|
| 350 | // localCamera->lookAt(tn); |
---|
[3634] | 351 | this->localPlayer->setMode(PNODE_ALL); |
---|
[3538] | 352 | //Vector* cameraOffset = new Vector (0, 5, -10); |
---|
[3596] | 353 | trackManager->condition(2, LEFTRIGHT, this->localPlayer); |
---|
[3675] | 354 | this->glmis->step(); |
---|
[3522] | 355 | |
---|
[2636] | 356 | break; |
---|
| 357 | } |
---|
| 358 | case DEBUG_WORLD_1: |
---|
| 359 | { |
---|
[3602] | 360 | lightMan->setPosition(.0, .0, .0); |
---|
[3603] | 361 | lightMan->setAttenuation(1.0, .01, 0.0); |
---|
| 362 | lightMan->setSpecularColor(1,0,0); |
---|
[3365] | 363 | this->nullParent = NullParent::getInstance (); |
---|
| 364 | this->nullParent->setName ("NullParent"); |
---|
| 365 | |
---|
[2636] | 366 | // create a player |
---|
[2644] | 367 | WorldEntity* myPlayer = new Player(); |
---|
[3365] | 368 | myPlayer->setName ("player"); |
---|
[2644] | 369 | this->spawn(myPlayer); |
---|
[3194] | 370 | this->localPlayer = myPlayer; |
---|
[2636] | 371 | |
---|
| 372 | // bind input |
---|
| 373 | Orxonox *orx = Orxonox::getInstance(); |
---|
[3226] | 374 | orx->getLocalInput()->bind (myPlayer); |
---|
[2636] | 375 | |
---|
| 376 | // bind camera |
---|
[3635] | 377 | this->localCamera = new Camera (); |
---|
[3365] | 378 | this->localCamera->setName ("camera"); |
---|
[3637] | 379 | this->localCamera->lookAt(LightManager::getInstance()->getLight(0)); |
---|
| 380 | this->localCamera->setParent(this->localPlayer); |
---|
[3429] | 381 | |
---|
| 382 | // Create SkySphere |
---|
| 383 | skySphere = new Skysphere("../data/pictures/sky-replace.jpg"); |
---|
[3502] | 384 | this->localPlayer->addChild(this->skySphere); |
---|
[3429] | 385 | |
---|
[3586] | 386 | Vector* es = new Vector (20, 0, 0); |
---|
| 387 | Quaternion* qs = new Quaternion (); |
---|
| 388 | |
---|
[3602] | 389 | lightMan->getLight(0)->setParent(trackManager->getTrackNode()); |
---|
[2636] | 390 | break; |
---|
| 391 | } |
---|
| 392 | default: |
---|
| 393 | printf("World::load() - no world with ID %i found", this->debugWorldNr ); |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | else if(this->worldName != NULL) |
---|
| 397 | { |
---|
| 398 | |
---|
| 399 | } |
---|
[2731] | 400 | |
---|
| 401 | // initialize debug coord system |
---|
| 402 | objectList = glGenLists(1); |
---|
| 403 | glNewList (objectList, GL_COMPILE); |
---|
[3559] | 404 | |
---|
[3735] | 405 | // trackManager->drawGraph(.01); |
---|
[3365] | 406 | trackManager->debug(2); |
---|
[2731] | 407 | glEndList(); |
---|
[3526] | 408 | |
---|
[3567] | 409 | terrain = new Terrain("../data/worlds/newGround.obj"); |
---|
[3568] | 410 | terrain->setRelCoor(new Vector(0,-10,0)); |
---|
[3559] | 411 | this->spawn(terrain); |
---|
[3455] | 412 | |
---|
[2636] | 413 | } |
---|
| 414 | |
---|
[3629] | 415 | |
---|
[3459] | 416 | /** |
---|
[3629] | 417 | \brief initializes a new World shortly before start |
---|
| 418 | |
---|
| 419 | this is the function, that will be loaded shortly before the world is |
---|
| 420 | started |
---|
[3459] | 421 | */ |
---|
| 422 | ErrorMessage World::init() |
---|
| 423 | { |
---|
| 424 | this->bPause = false; |
---|
| 425 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
| 426 | cn->addToWorld(this); |
---|
| 427 | cn->enable(true); |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | /** |
---|
| 432 | \brief starts the World |
---|
| 433 | */ |
---|
| 434 | ErrorMessage World::start() |
---|
| 435 | { |
---|
[3546] | 436 | PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr); |
---|
[3459] | 437 | this->bQuitOrxonox = false; |
---|
| 438 | this->bQuitCurrentGame = false; |
---|
| 439 | this->mainLoop(); |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | /** |
---|
| 443 | \brief stops the world. |
---|
| 444 | |
---|
| 445 | This happens, when the player decides to end the Level. |
---|
| 446 | */ |
---|
| 447 | ErrorMessage World::stop() |
---|
| 448 | { |
---|
[3546] | 449 | PRINTF(3)("World::stop() - got stop signal\n"); |
---|
[3459] | 450 | this->bQuitCurrentGame = true; |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | /** |
---|
| 454 | \brief pauses the Game |
---|
| 455 | */ |
---|
| 456 | ErrorMessage World::pause() |
---|
| 457 | { |
---|
| 458 | this->isPaused = true; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | /** |
---|
| 462 | \brief ends the pause Phase |
---|
| 463 | */ |
---|
| 464 | ErrorMessage World::resume() |
---|
| 465 | { |
---|
| 466 | this->isPaused = false; |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | /** |
---|
| 470 | \brief destroys the World |
---|
| 471 | */ |
---|
| 472 | ErrorMessage World::destroy() |
---|
| 473 | { |
---|
[3566] | 474 | |
---|
[3459] | 475 | } |
---|
| 476 | |
---|
| 477 | /** |
---|
| 478 | \brief shows the loading screen |
---|
| 479 | */ |
---|
| 480 | void World::displayLoadScreen () |
---|
| 481 | { |
---|
[3546] | 482 | PRINTF(3)("World::displayLoadScreen - start\n"); |
---|
[3459] | 483 | |
---|
| 484 | //GLMenuImageScreen* |
---|
| 485 | this->glmis = GLMenuImageScreen::getInstance(); |
---|
| 486 | this->glmis->init(); |
---|
[3675] | 487 | this->glmis->setMaximum(8); |
---|
[3459] | 488 | this->glmis->draw(); |
---|
| 489 | |
---|
[3546] | 490 | PRINTF(3)("World::displayLoadScreen - end\n"); |
---|
[3459] | 491 | } |
---|
| 492 | |
---|
| 493 | /** |
---|
| 494 | \brief removes the loadscreen, and changes over to the game |
---|
| 495 | |
---|
| 496 | \todo take out the delay |
---|
| 497 | */ |
---|
| 498 | void World::releaseLoadScreen () |
---|
| 499 | { |
---|
[3546] | 500 | PRINTF(3)("World::releaseLoadScreen - start\n"); |
---|
[3459] | 501 | this->glmis->setValue(this->glmis->getMaximum()); |
---|
[3675] | 502 | //SDL_Delay(500); |
---|
[3546] | 503 | PRINTF(3)("World::releaseLoadScreen - end\n"); |
---|
[3459] | 504 | } |
---|
| 505 | |
---|
| 506 | |
---|
[3620] | 507 | /** |
---|
| 508 | \brief gets the list of entities from the world |
---|
| 509 | \returns entity list |
---|
| 510 | */ |
---|
| 511 | tList<WorldEntity>* World::getEntities() |
---|
| 512 | { |
---|
| 513 | return this->entities; |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | |
---|
[3646] | 517 | /** |
---|
| 518 | \brief this returns the current game time |
---|
| 519 | \returns elapsed game time |
---|
| 520 | */ |
---|
| 521 | double World::getGameTime() |
---|
| 522 | { |
---|
| 523 | return this->gameTime; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | |
---|
[2636] | 527 | /** |
---|
[2551] | 528 | \brief checks for collisions |
---|
| 529 | |
---|
| 530 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
| 531 | between them. In case of collisions the collide() method of the corresponding entities |
---|
| 532 | is called. |
---|
[1858] | 533 | */ |
---|
[2190] | 534 | void World::collide () |
---|
[1858] | 535 | { |
---|
[2816] | 536 | /* |
---|
| 537 | List *a, *b; |
---|
[2551] | 538 | WorldEntity *aobj, *bobj; |
---|
[2816] | 539 | |
---|
| 540 | a = entities; |
---|
[2551] | 541 | |
---|
| 542 | while( a != NULL) |
---|
| 543 | { |
---|
[2816] | 544 | aobj = a->nextElement(); |
---|
[2551] | 545 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
[2190] | 546 | { |
---|
[2816] | 547 | b = a->nextElement(); |
---|
[2551] | 548 | while( b != NULL ) |
---|
| 549 | { |
---|
[2816] | 550 | bobj = b->nextElement(); |
---|
[2551] | 551 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
[2190] | 552 | { |
---|
[2551] | 553 | unsigned long ahitflg, bhitflg; |
---|
| 554 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
| 555 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
| 556 | &bhitflg) ); |
---|
| 557 | { |
---|
| 558 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
| 559 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
| 560 | } |
---|
[2190] | 561 | } |
---|
[2816] | 562 | b = b->nextElement(); |
---|
[2551] | 563 | } |
---|
[2190] | 564 | } |
---|
[2816] | 565 | a = a->enumerate(); |
---|
[2551] | 566 | } |
---|
[2816] | 567 | */ |
---|
[1858] | 568 | } |
---|
| 569 | |
---|
| 570 | /** |
---|
[2551] | 571 | \brief runs through all entities calling their draw() methods |
---|
[1931] | 572 | */ |
---|
[2190] | 573 | void World::draw () |
---|
[2077] | 574 | { |
---|
[3462] | 575 | /* draw entities */ |
---|
[2551] | 576 | WorldEntity* entity; |
---|
[3526] | 577 | glLoadIdentity(); |
---|
| 578 | |
---|
[3653] | 579 | //entity = this->entities->enumerate(); |
---|
| 580 | tIterator<WorldEntity>* iterator = this->entities->getIterator(); |
---|
| 581 | entity = iterator->nextElement(); |
---|
[2816] | 582 | while( entity != NULL ) |
---|
[2551] | 583 | { |
---|
[2822] | 584 | if( entity->bDraw ) entity->draw(); |
---|
[3653] | 585 | //entity = this->entities->nextElement(); |
---|
| 586 | entity = iterator->nextElement(); |
---|
| 587 | } |
---|
| 588 | delete iterator; |
---|
[2551] | 589 | |
---|
[2731] | 590 | glCallList (objectList); |
---|
[3419] | 591 | //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list. |
---|
| 592 | skySphere->draw(); |
---|
| 593 | |
---|
[3693] | 594 | tmpFont->draw(); |
---|
[3455] | 595 | |
---|
[3602] | 596 | lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes // |
---|
[1931] | 597 | } |
---|
| 598 | |
---|
[2816] | 599 | |
---|
[2190] | 600 | /** |
---|
[3225] | 601 | \brief function to put your own debug stuff into it. it can display informations about |
---|
| 602 | the current class/procedure |
---|
| 603 | */ |
---|
[2640] | 604 | void World::debug() |
---|
| 605 | { |
---|
[3546] | 606 | PRINTF(2)("debug() - starting debug\n"); |
---|
[3365] | 607 | PNode* p1 = NullParent::getInstance (); |
---|
| 608 | PNode* p2 = new PNode (new Vector(2, 2, 2), p1); |
---|
| 609 | PNode* p3 = new PNode (new Vector(4, 4, 4), p1); |
---|
| 610 | PNode* p4 = new PNode (new Vector(6, 6, 6), p2); |
---|
| 611 | |
---|
| 612 | p1->debug (); |
---|
| 613 | p2->debug (); |
---|
| 614 | p3->debug (); |
---|
| 615 | p4->debug (); |
---|
| 616 | |
---|
| 617 | p1->shiftCoor (new Vector(-1, -1, -1)); |
---|
| 618 | |
---|
| 619 | printf("World::debug() - shift\n"); |
---|
| 620 | p1->debug (); |
---|
| 621 | p2->debug (); |
---|
| 622 | p3->debug (); |
---|
| 623 | p4->debug (); |
---|
| 624 | |
---|
[3644] | 625 | p1->update (0); |
---|
[3365] | 626 | |
---|
| 627 | printf ("World::debug() - update\n"); |
---|
| 628 | p1->debug (); |
---|
| 629 | p2->debug (); |
---|
| 630 | p3->debug (); |
---|
| 631 | p4->debug (); |
---|
| 632 | |
---|
| 633 | p2->shiftCoor (new Vector(-1, -1, -1)); |
---|
[3644] | 634 | p1->update (0); |
---|
[3365] | 635 | |
---|
| 636 | p1->debug (); |
---|
| 637 | p2->debug (); |
---|
| 638 | p3->debug (); |
---|
| 639 | p4->debug (); |
---|
| 640 | |
---|
| 641 | p2->setAbsCoor (new Vector(1,2,3)); |
---|
| 642 | |
---|
| 643 | |
---|
[3644] | 644 | p1->update (0); |
---|
[3365] | 645 | |
---|
| 646 | p1->debug (); |
---|
| 647 | p2->debug (); |
---|
| 648 | p3->debug (); |
---|
| 649 | p4->debug (); |
---|
| 650 | |
---|
[3544] | 651 | delete p1; |
---|
[3365] | 652 | |
---|
| 653 | |
---|
| 654 | /* |
---|
[2640] | 655 | WorldEntity* entity; |
---|
| 656 | printf("counting all entities\n"); |
---|
[2816] | 657 | printf("World::debug() - enumerate()\n"); |
---|
| 658 | entity = entities->enumerate(); |
---|
| 659 | while( entity != NULL ) |
---|
[2640] | 660 | { |
---|
| 661 | if( entity->bDraw ) printf("got an entity\n"); |
---|
[2816] | 662 | entity = entities->nextElement(); |
---|
[2640] | 663 | } |
---|
[3365] | 664 | */ |
---|
[2640] | 665 | } |
---|
[2636] | 666 | |
---|
[2640] | 667 | |
---|
[3449] | 668 | /** |
---|
[3225] | 669 | \brief main loop of the world: executing all world relevant function |
---|
| 670 | |
---|
| 671 | in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
| 672 | all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
| 673 | collisions drawing everything to the screen. |
---|
| 674 | */ |
---|
[2636] | 675 | void World::mainLoop() |
---|
| 676 | { |
---|
[3365] | 677 | this->lastFrame = SDL_GetTicks (); |
---|
[3546] | 678 | PRINTF(3)("World::mainLoop() - Entering main loop\n"); |
---|
[3215] | 679 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
[2551] | 680 | { |
---|
[3546] | 681 | PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize()); |
---|
[2636] | 682 | // Network |
---|
[3365] | 683 | this->synchronize (); |
---|
[2636] | 684 | // Process input |
---|
[3365] | 685 | this->handleInput (); |
---|
[3215] | 686 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
| 687 | break; |
---|
[2636] | 688 | // Process time |
---|
[3551] | 689 | this->tick (); |
---|
| 690 | // Update the state |
---|
| 691 | this->update (); |
---|
[2636] | 692 | // Process collision |
---|
[3459] | 693 | this->collide (); |
---|
[2636] | 694 | // Draw |
---|
[3365] | 695 | this->display (); |
---|
[3548] | 696 | |
---|
[3565] | 697 | // for( int i = 0; i < 5000000; i++) {} |
---|
[3365] | 698 | /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/ |
---|
[2551] | 699 | } |
---|
[3546] | 700 | PRINTF(3)("World::mainLoop() - Exiting the main loop\n"); |
---|
[1899] | 701 | } |
---|
| 702 | |
---|
[3459] | 703 | |
---|
[2190] | 704 | /** |
---|
[2636] | 705 | \brief synchronize local data with remote data |
---|
[1855] | 706 | */ |
---|
[2636] | 707 | void World::synchronize () |
---|
[1855] | 708 | { |
---|
[2636] | 709 | // Get remote input |
---|
| 710 | // Update synchronizables |
---|
[1855] | 711 | } |
---|
[2636] | 712 | |
---|
[3459] | 713 | |
---|
[2636] | 714 | /** |
---|
| 715 | \brief run all input processing |
---|
[3225] | 716 | |
---|
| 717 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
| 718 | sdl and has its own event-passing-queue. |
---|
[2636] | 719 | */ |
---|
[3225] | 720 | void World::handleInput () |
---|
[2636] | 721 | { |
---|
| 722 | // localinput |
---|
[3225] | 723 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
[3216] | 724 | cn->process(); |
---|
[2636] | 725 | // remoteinput |
---|
| 726 | } |
---|
| 727 | |
---|
[3459] | 728 | |
---|
[2636] | 729 | /** |
---|
| 730 | \brief advance the timeline |
---|
[3225] | 731 | |
---|
| 732 | this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
| 733 | the time is mesured in ms and passed to all world-entities and other classes that need |
---|
| 734 | a heart-beat. |
---|
[2636] | 735 | */ |
---|
[3551] | 736 | void World::tick () |
---|
[2636] | 737 | { |
---|
| 738 | Uint32 currentFrame = SDL_GetTicks(); |
---|
| 739 | if(!this->bPause) |
---|
| 740 | { |
---|
[3644] | 741 | this->dt = currentFrame - this->lastFrame; |
---|
[2816] | 742 | |
---|
[3646] | 743 | if( this->dt > 0) |
---|
[2636] | 744 | { |
---|
| 745 | float fps = 1000/dt; |
---|
[3707] | 746 | |
---|
| 747 | // temporary, only for showing how fast the text-engine is |
---|
[3701] | 748 | char tmpChar[20]; |
---|
[3707] | 749 | sprintf(tmpChar, "fps: %4.0f", fps); |
---|
[3701] | 750 | tmpFont->setText(tmpChar); |
---|
| 751 | tmpFont->createTexture(); |
---|
[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 | |
---|