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