[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 | |
---|
[2190] | 17 | #include "world.h" |
---|
| 18 | #include "world_entity.h" |
---|
| 19 | #include "collision.h" |
---|
| 20 | #include "track.h" |
---|
[2036] | 21 | #include "player.h" |
---|
[2190] | 22 | #include "command_node.h" |
---|
| 23 | #include "camera.h" |
---|
[2816] | 24 | #include "environment.h" |
---|
[2036] | 25 | |
---|
[1856] | 26 | using namespace std; |
---|
[1853] | 27 | |
---|
| 28 | |
---|
[1858] | 29 | /** |
---|
[2551] | 30 | \brief create a new World |
---|
| 31 | |
---|
| 32 | This creates a new empty world! |
---|
[1858] | 33 | */ |
---|
[2636] | 34 | World::World (char* name) |
---|
[1855] | 35 | { |
---|
[2636] | 36 | this->worldName = name; |
---|
| 37 | this->debugWorldNr = -1; |
---|
[2816] | 38 | this->entities = new List(); |
---|
[1855] | 39 | } |
---|
| 40 | |
---|
[2636] | 41 | World::World (int worldID) |
---|
| 42 | { |
---|
| 43 | this->debugWorldNr = worldID; |
---|
| 44 | this->worldName = NULL; |
---|
[2816] | 45 | this->entities = new List(); |
---|
[2636] | 46 | } |
---|
| 47 | |
---|
[1858] | 48 | /** |
---|
[2551] | 49 | \brief remove the World from memory |
---|
[1858] | 50 | */ |
---|
[2190] | 51 | World::~World () |
---|
[1872] | 52 | { |
---|
[2640] | 53 | Orxonox *orx = Orxonox::getInstance(); |
---|
[2644] | 54 | orx->get_localinput()->unbind (this->localPlayer); |
---|
| 55 | delete this->entities; |
---|
| 56 | delete this->localCamera; |
---|
[1872] | 57 | } |
---|
[1858] | 58 | |
---|
[2636] | 59 | |
---|
[1855] | 60 | /** |
---|
[2636] | 61 | \brief initialize the world before use. |
---|
| 62 | */ |
---|
| 63 | Error World::init() |
---|
| 64 | { |
---|
| 65 | this->bPause = false; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | Error World::start() |
---|
| 69 | { |
---|
| 70 | this->mainLoop(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | Error World::stop() |
---|
| 74 | { |
---|
| 75 | this->bQuitCurrentGame = true; |
---|
| 76 | this->localCamera->setWorld(NULL); |
---|
[2816] | 77 | this->entities->clear(); |
---|
| 78 | Orxonox::getInstance()->get_localinput()->reset(); |
---|
[2640] | 79 | this->~World(); |
---|
[2636] | 80 | } |
---|
| 81 | |
---|
| 82 | Error World::pause() |
---|
| 83 | { |
---|
| 84 | this->isPaused = true; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | Error World::resume() |
---|
| 88 | { |
---|
| 89 | this->isPaused = false; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void World::load() |
---|
| 93 | { |
---|
| 94 | if(this->debugWorldNr != -1) |
---|
| 95 | { |
---|
| 96 | switch(this->debugWorldNr) |
---|
| 97 | { |
---|
| 98 | case DEBUG_WORLD_0: |
---|
| 99 | { |
---|
| 100 | // create some path nodes |
---|
| 101 | this->pathnodes = new Vector[6]; |
---|
| 102 | this->pathnodes[0] = Vector(0, 0, 0); |
---|
[2792] | 103 | this->pathnodes[1] = Vector(1000, 0, 0); |
---|
| 104 | // this->pathnodes[2] = Vector(-100, 140, 0); |
---|
| 105 | // this->pathnodes[3] = Vector(0, 180, 0); |
---|
| 106 | // this->pathnodes[4] = Vector(100, 140, 0); |
---|
| 107 | // this->pathnodes[5] = Vector(100, 40, 0); |
---|
[2636] | 108 | |
---|
| 109 | // create the tracks |
---|
[2816] | 110 | this->tracklen = 2; |
---|
| 111 | this->track = new Track[2]; |
---|
[2636] | 112 | for( int i = 0; i < this->tracklen; i++) |
---|
| 113 | { |
---|
| 114 | this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | // create a player |
---|
[2644] | 118 | WorldEntity* myPlayer = new Player(); |
---|
| 119 | this->spawn(myPlayer); |
---|
[2640] | 120 | this->localPlayer = myPlayer; |
---|
| 121 | |
---|
[2636] | 122 | // bind input |
---|
| 123 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 124 | orx->get_localinput()->bind (myPlayer); |
---|
| 125 | |
---|
| 126 | // bind camera |
---|
| 127 | this->localCamera = new Camera(this); |
---|
| 128 | this->getCamera()->bind (myPlayer); |
---|
[2816] | 129 | |
---|
| 130 | Placement* plc = new Placement; |
---|
| 131 | plc->r = Vector(100, 10, 10); |
---|
| 132 | plc->w = Quaternion(); |
---|
| 133 | WorldEntity* env = new Environment(); |
---|
| 134 | this->spawn(env, plc); |
---|
| 135 | |
---|
| 136 | this->entities->debug(); |
---|
[2636] | 137 | break; |
---|
| 138 | } |
---|
| 139 | case DEBUG_WORLD_1: |
---|
| 140 | { |
---|
| 141 | // create some path nodes |
---|
| 142 | this->pathnodes = new Vector[6]; |
---|
| 143 | this->pathnodes[0] = Vector(0, 0, 0); |
---|
| 144 | this->pathnodes[1] = Vector(20, 10, 10); |
---|
| 145 | this->pathnodes[2] = Vector(40, 0, 10); |
---|
| 146 | this->pathnodes[3] = Vector(60, 10, 0); |
---|
| 147 | this->pathnodes[4] = Vector(80, 20, 10); |
---|
| 148 | this->pathnodes[5] = Vector(30, 50, 0); |
---|
| 149 | |
---|
| 150 | // create the tracks |
---|
| 151 | this->tracklen = 6; |
---|
| 152 | this->track = new Track[6]; |
---|
| 153 | for( int i = 0; i < this->tracklen; i++) |
---|
| 154 | { |
---|
| 155 | this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // create a player |
---|
[2644] | 159 | //WorldEntity* myPlayer = (WorldEntity*) this->spawn<Player>(); |
---|
| 160 | WorldEntity* myPlayer = new Player(); |
---|
| 161 | this->spawn(myPlayer); |
---|
[2640] | 162 | this->localPlayer = myPlayer; |
---|
[2636] | 163 | |
---|
| 164 | // bind input |
---|
| 165 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 166 | orx->get_localinput()->bind (myPlayer); |
---|
| 167 | |
---|
| 168 | // bind camera |
---|
| 169 | this->localCamera = new Camera(this); |
---|
| 170 | this->getCamera()->bind (myPlayer); |
---|
| 171 | break; |
---|
| 172 | } |
---|
| 173 | default: |
---|
| 174 | printf("World::load() - no world with ID %i found", this->debugWorldNr ); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | else if(this->worldName != NULL) |
---|
| 178 | { |
---|
| 179 | |
---|
| 180 | } |
---|
[2731] | 181 | |
---|
| 182 | // initialize debug coord system |
---|
| 183 | objectList = glGenLists(1); |
---|
| 184 | glNewList (objectList, GL_COMPILE); |
---|
| 185 | glLoadIdentity(); |
---|
[2792] | 186 | glColor3f(1.0,0,0); |
---|
[2731] | 187 | glBegin(GL_LINES); |
---|
[2792] | 188 | float height [500][100]; |
---|
[2811] | 189 | float size = 2.0; |
---|
[2792] | 190 | for ( int i = 0; i<=200; i+=1) |
---|
| 191 | { |
---|
| 192 | for (int j = 0; j<=50;j+=1) |
---|
| 193 | { |
---|
[2811] | 194 | height[i][j] = rand()/200321400 + (j-25)*(j-25)/30; |
---|
[2792] | 195 | |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | for ( int i = 0; i<=200; i+=1) |
---|
| 199 | { |
---|
| 200 | for (int j = 0; j<=50;j+=1) |
---|
| 201 | { |
---|
[2811] | 202 | glVertex3f(size*i, size*j-25*size, height[i][j] -20); |
---|
| 203 | glVertex3f(size*i+size, size*j-25*size, height[i+1][j]-20); |
---|
| 204 | // glVertex3f(size*i+5.0, size*j+5.0-12size, height[i+1][j+1]-20); |
---|
| 205 | glVertex3f(size*i, size*j-25*size, height[i][j] -20); |
---|
| 206 | glVertex3f(size*i, size*j+size -25*size, height[i][j+1]-20); |
---|
[2792] | 207 | } |
---|
| 208 | } |
---|
| 209 | glEnd(); |
---|
| 210 | /* |
---|
| 211 | glBegin(GL_LINES); |
---|
[2731] | 212 | for( float x = -128.0; x < 128.0; x += 25.0) |
---|
| 213 | { |
---|
| 214 | for( float y = -128.0; y < 128.0; y += 25.0) |
---|
| 215 | { |
---|
| 216 | glColor3f(1,0,0); |
---|
| 217 | glVertex3f(x,y,-128.0); |
---|
| 218 | glVertex3f(x,y,0.0); |
---|
| 219 | glColor3f(0.5,0,0); |
---|
| 220 | glVertex3f(x,y,0.0); |
---|
| 221 | glVertex3f(x,y,128.0); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | for( float y = -128.0; y < 128.0; y += 25.0) |
---|
| 225 | { |
---|
| 226 | for( float z = -128.0; z < 128.0; z += 25.0) |
---|
| 227 | { |
---|
| 228 | glColor3f(0,1,0); |
---|
| 229 | glVertex3f(-128.0,y,z); |
---|
| 230 | glVertex3f(0.0,y,z); |
---|
| 231 | glColor3f(0,0.5,0); |
---|
| 232 | glVertex3f(0.0,y,z); |
---|
| 233 | glVertex3f(128.0,y,z); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | for( float x = -128.0; x < 128.0; x += 25.0) |
---|
| 237 | { |
---|
| 238 | for( float z = -128.0; z < 128.0; z += 25.0) |
---|
| 239 | { |
---|
| 240 | glColor3f(0,0,1); |
---|
| 241 | glVertex3f(x,-128.0,z); |
---|
| 242 | glVertex3f(x,0.0,z); |
---|
| 243 | glColor3f(0,0,0.5); |
---|
| 244 | glVertex3f(x,0.0,z); |
---|
| 245 | glVertex3f(x,128.0,z); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | } |
---|
[2792] | 249 | */ |
---|
[2731] | 250 | //draw track |
---|
[2792] | 251 | glBegin(GL_LINES); |
---|
[2731] | 252 | glColor3f(0,1,1); |
---|
| 253 | for( int i = 0; i < tracklen; i++) |
---|
| 254 | { |
---|
| 255 | glVertex3f(pathnodes[i].x,pathnodes[i].y,pathnodes[i].z); |
---|
| 256 | glVertex3f(pathnodes[(i+1)%tracklen].x,pathnodes[(i+1)%tracklen].y,pathnodes[(i+1)%tracklen].z); |
---|
| 257 | } |
---|
| 258 | glEnd(); |
---|
| 259 | glEndList(); |
---|
[2636] | 260 | } |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | /** |
---|
[2551] | 264 | \brief checks for collisions |
---|
| 265 | |
---|
| 266 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
| 267 | between them. In case of collisions the collide() method of the corresponding entities |
---|
| 268 | is called. |
---|
[1858] | 269 | */ |
---|
[2190] | 270 | void World::collide () |
---|
[1858] | 271 | { |
---|
[2816] | 272 | /* |
---|
| 273 | List *a, *b; |
---|
[2551] | 274 | WorldEntity *aobj, *bobj; |
---|
[2816] | 275 | |
---|
| 276 | a = entities; |
---|
[2551] | 277 | |
---|
| 278 | while( a != NULL) |
---|
| 279 | { |
---|
[2816] | 280 | aobj = a->nextElement(); |
---|
[2551] | 281 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
[2190] | 282 | { |
---|
[2816] | 283 | b = a->nextElement(); |
---|
[2551] | 284 | while( b != NULL ) |
---|
| 285 | { |
---|
[2816] | 286 | bobj = b->nextElement(); |
---|
[2551] | 287 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
[2190] | 288 | { |
---|
[2551] | 289 | unsigned long ahitflg, bhitflg; |
---|
| 290 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
| 291 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
| 292 | &bhitflg) ); |
---|
| 293 | { |
---|
| 294 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
| 295 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
| 296 | } |
---|
[2190] | 297 | } |
---|
[2816] | 298 | b = b->nextElement(); |
---|
[2551] | 299 | } |
---|
[2190] | 300 | } |
---|
[2816] | 301 | a = a->enumerate(); |
---|
[2551] | 302 | } |
---|
[2816] | 303 | */ |
---|
[1858] | 304 | } |
---|
| 305 | |
---|
| 306 | /** |
---|
[2551] | 307 | \brief runs through all entities calling their draw() methods |
---|
[1931] | 308 | */ |
---|
[2190] | 309 | void World::draw () |
---|
[2077] | 310 | { |
---|
[2551] | 311 | // draw geometry |
---|
| 312 | |
---|
| 313 | // draw entities |
---|
[2816] | 314 | List *l; |
---|
[2551] | 315 | WorldEntity* entity; |
---|
| 316 | |
---|
[2816] | 317 | l = entities; |
---|
| 318 | entity = l->enumerate(); |
---|
| 319 | while( entity != NULL ) |
---|
[2551] | 320 | { |
---|
| 321 | if( entity->bDraw ) entity->draw(); |
---|
[2816] | 322 | entity = l->nextElement(); |
---|
[2551] | 323 | } |
---|
| 324 | |
---|
| 325 | |
---|
| 326 | // draw debug coord system |
---|
[2731] | 327 | glCallList (objectList); |
---|
[2551] | 328 | |
---|
[2731] | 329 | |
---|
[1931] | 330 | } |
---|
| 331 | |
---|
| 332 | /** |
---|
[2551] | 333 | \brief updates Placements and notifies entities when they left the |
---|
| 334 | world |
---|
| 335 | |
---|
| 336 | This runs trough all WorldEntities and maps Locations to Placements |
---|
| 337 | if they are bound, checks whether they left the level boundaries |
---|
| 338 | and calls appropriate functions. |
---|
[1883] | 339 | */ |
---|
[2190] | 340 | void World::update () |
---|
[1883] | 341 | { |
---|
[2816] | 342 | //List<WorldEntity> *l; |
---|
[2551] | 343 | WorldEntity* entity; |
---|
| 344 | Location* loc; |
---|
| 345 | Placement* plc; |
---|
| 346 | Uint32 t; |
---|
| 347 | |
---|
[2816] | 348 | // l = entities->enumerate(); |
---|
| 349 | entity = this->entities->enumerate(); |
---|
| 350 | while( entity != NULL ) |
---|
[2551] | 351 | { |
---|
[2816] | 352 | |
---|
[2551] | 353 | |
---|
| 354 | if( !entity->isFree() ) |
---|
| 355 | { |
---|
| 356 | loc = entity->get_location(); |
---|
| 357 | plc = entity->get_placement(); |
---|
| 358 | t = loc->part; |
---|
| 359 | |
---|
| 360 | /* check if entity has still a legal track-id */ |
---|
| 361 | if( t >= tracklen ) |
---|
| 362 | { |
---|
| 363 | printf("An entity is out of the game area\n"); |
---|
| 364 | entity->left_world (); |
---|
| 365 | } |
---|
| 366 | else |
---|
| 367 | { |
---|
| 368 | while( track[t].map_coords( loc, plc) ) |
---|
[2190] | 369 | { |
---|
[2551] | 370 | track[t].post_leave (entity); |
---|
| 371 | if( loc->part >= tracklen ) |
---|
| 372 | { |
---|
| 373 | printf("An entity has left the game area\n"); |
---|
| 374 | entity->left_world (); |
---|
| 375 | break; |
---|
| 376 | } |
---|
| 377 | track[loc->part].post_enter (entity); |
---|
[2190] | 378 | } |
---|
[2551] | 379 | } |
---|
[2190] | 380 | } |
---|
[2551] | 381 | else |
---|
| 382 | { |
---|
| 383 | /* TO DO: implement check whether this particular free entity |
---|
| 384 | is out of the game area |
---|
| 385 | TO DO: call function to notify the entity that it left |
---|
| 386 | the game area |
---|
| 387 | */ |
---|
| 388 | } |
---|
| 389 | |
---|
[2816] | 390 | entity = entities->nextElement(); |
---|
[2551] | 391 | } |
---|
| 392 | |
---|
[1883] | 393 | } |
---|
| 394 | |
---|
[2077] | 395 | /** |
---|
[2551] | 396 | \brief relays the passed time since the last frame to entities and Track parts |
---|
| 397 | \param deltaT: the time passed since the last frame in milliseconds |
---|
[2077] | 398 | */ |
---|
[2190] | 399 | void World::time_slice (Uint32 deltaT) |
---|
[2077] | 400 | { |
---|
[2816] | 401 | //List<WorldEntity> *l; |
---|
[2551] | 402 | WorldEntity* entity; |
---|
| 403 | float seconds = deltaT; |
---|
| 404 | |
---|
| 405 | seconds /= 1000; |
---|
| 406 | |
---|
[2816] | 407 | entity = entities->enumerate(); |
---|
| 408 | while( entity != NULL) |
---|
[2551] | 409 | { |
---|
| 410 | entity->tick (seconds); |
---|
[2816] | 411 | entity = entities->nextElement(); |
---|
[2551] | 412 | } |
---|
[2816] | 413 | |
---|
[2551] | 414 | for( int i = 0; i < tracklen; i++) track[i].tick (seconds); |
---|
[2077] | 415 | } |
---|
[1883] | 416 | |
---|
[2190] | 417 | /** |
---|
[2551] | 418 | \brief removes level data from memory |
---|
[1858] | 419 | */ |
---|
[2190] | 420 | void World::unload() |
---|
[1858] | 421 | { |
---|
[2551] | 422 | if( pathnodes) delete []pathnodes; |
---|
| 423 | if( track) delete []pathnodes; |
---|
[1883] | 424 | } |
---|
[1879] | 425 | |
---|
[2636] | 426 | |
---|
| 427 | |
---|
[2190] | 428 | /** |
---|
[2636] | 429 | \brief calls the correct mapping function to convert a given "look at"-Location to a |
---|
| 430 | Camera Placement |
---|
[1858] | 431 | */ |
---|
[2636] | 432 | void World::calc_camera_pos (Location* loc, Placement* plc) |
---|
[1858] | 433 | { |
---|
[2636] | 434 | track[loc->part].map_camera (loc, plc); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | void World::setTrackLen(Uint32 len) |
---|
| 439 | { |
---|
| 440 | this->tracklen = len; |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | int World::getTrackLen() |
---|
| 444 | { |
---|
| 445 | return this->tracklen; |
---|
| 446 | } |
---|
| 447 | |
---|
[2640] | 448 | void World::debug() |
---|
| 449 | { |
---|
[2816] | 450 | //List<WorldEntity> *l; |
---|
[2640] | 451 | WorldEntity* entity; |
---|
| 452 | |
---|
| 453 | printf("counting all entities\n"); |
---|
[2816] | 454 | printf("World::debug() - enumerate()\n"); |
---|
| 455 | entity = entities->enumerate(); |
---|
| 456 | while( entity != NULL ) |
---|
[2640] | 457 | { |
---|
| 458 | if( entity->bDraw ) printf("got an entity\n"); |
---|
[2816] | 459 | entity = entities->nextElement(); |
---|
[2640] | 460 | } |
---|
| 461 | } |
---|
[2636] | 462 | |
---|
[2640] | 463 | |
---|
[2636] | 464 | void World::mainLoop() |
---|
| 465 | { |
---|
| 466 | this->lastFrame = SDL_GetTicks(); |
---|
| 467 | this->bQuitOrxonox = false; |
---|
| 468 | this->bQuitCurrentGame = false; |
---|
| 469 | printf("World|Entering main loop\n"); |
---|
| 470 | while(!this->bQuitOrxonox && !this->bQuitCurrentGame) /* pause pause pause ?!?!?*/ |
---|
[2551] | 471 | { |
---|
[2640] | 472 | //debug routine |
---|
| 473 | //debug(); |
---|
[2636] | 474 | // Network |
---|
| 475 | synchronize(); |
---|
| 476 | // Process input |
---|
| 477 | handle_input(); |
---|
| 478 | // Process time |
---|
| 479 | time_slice(); |
---|
| 480 | // Process collision |
---|
| 481 | collision(); |
---|
| 482 | // Draw |
---|
| 483 | display(); |
---|
[2816] | 484 | |
---|
| 485 | for(int i = 0; i < 1000000; i++){} |
---|
| 486 | |
---|
[2551] | 487 | } |
---|
[2636] | 488 | printf("World|Exiting the main loop\n"); |
---|
[1899] | 489 | } |
---|
| 490 | |
---|
[2190] | 491 | /** |
---|
[2636] | 492 | \brief synchronize local data with remote data |
---|
[1855] | 493 | */ |
---|
[2636] | 494 | void World::synchronize () |
---|
[1855] | 495 | { |
---|
[2636] | 496 | // Get remote input |
---|
| 497 | // Update synchronizables |
---|
[1855] | 498 | } |
---|
[2636] | 499 | |
---|
| 500 | /** |
---|
| 501 | \brief run all input processing |
---|
| 502 | */ |
---|
| 503 | void World::handle_input () |
---|
| 504 | { |
---|
| 505 | // localinput |
---|
| 506 | Orxonox::getInstance()->get_localinput()->process(); |
---|
| 507 | // remoteinput |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | /** |
---|
| 511 | \brief advance the timeline |
---|
| 512 | */ |
---|
| 513 | void World::time_slice () |
---|
| 514 | { |
---|
| 515 | Uint32 currentFrame = SDL_GetTicks(); |
---|
| 516 | if(!this->bPause) |
---|
| 517 | { |
---|
| 518 | Uint32 dt = currentFrame - this->lastFrame; |
---|
[2816] | 519 | |
---|
[2636] | 520 | if(dt > 0) |
---|
| 521 | { |
---|
| 522 | float fps = 1000/dt; |
---|
| 523 | printf("fps = %f\n", fps); |
---|
| 524 | } |
---|
| 525 | else |
---|
| 526 | { |
---|
[2816] | 527 | printf("fps = 1000 but 0ms!\n"); |
---|
[2636] | 528 | } |
---|
[2816] | 529 | |
---|
[2636] | 530 | this->time_slice (dt); |
---|
| 531 | this->update (); |
---|
| 532 | this->localCamera->time_slice (dt); |
---|
| 533 | } |
---|
| 534 | this->lastFrame = currentFrame; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | /** |
---|
| 538 | \brief compute collision detection |
---|
| 539 | */ |
---|
| 540 | void World::collision () |
---|
| 541 | { |
---|
| 542 | this->collide (); |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | /** |
---|
| 546 | \brief handle keyboard commands that are not meant for WorldEntities |
---|
| 547 | \param cmd: the command to handle |
---|
| 548 | \return true if the command was handled by the system or false if it may be passed to the WorldEntities |
---|
| 549 | */ |
---|
| 550 | bool World::system_command (Command* cmd) |
---|
| 551 | { |
---|
| 552 | if( !strcmp( cmd->cmd, "quit")) |
---|
| 553 | { |
---|
| 554 | if( !cmd->bUp) this->bQuitOrxonox = true; |
---|
| 555 | return true; |
---|
| 556 | } |
---|
| 557 | return false; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | /** |
---|
| 561 | \brief render the current frame |
---|
| 562 | */ |
---|
| 563 | void World::display () |
---|
| 564 | { |
---|
| 565 | // clear buffer |
---|
| 566 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
| 567 | // set camera |
---|
| 568 | this->localCamera->apply (); |
---|
| 569 | // draw world |
---|
| 570 | this->draw(); |
---|
| 571 | // draw HUD |
---|
| 572 | // flip buffers |
---|
| 573 | SDL_GL_SwapBuffers(); |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | Camera* World::getCamera() |
---|
| 577 | { |
---|
| 578 | return this->localCamera; |
---|
| 579 | } |
---|
[2644] | 580 | |
---|
| 581 | |
---|
| 582 | void World::spawn(WorldEntity* entity) |
---|
| 583 | { |
---|
| 584 | Location zeroloc; |
---|
| 585 | Location* loc = NULL; |
---|
| 586 | WorldEntity* owner; |
---|
[2816] | 587 | |
---|
| 588 | entities->add (entity); |
---|
| 589 | zeroloc.dist = 0; |
---|
| 590 | zeroloc.part = 0; |
---|
| 591 | zeroloc.pos = Vector(); |
---|
| 592 | zeroloc.rot = Quaternion(); |
---|
| 593 | loc = &zeroloc; |
---|
[2644] | 594 | entity->init (loc, owner); |
---|
| 595 | if (entity->bFree) |
---|
| 596 | { |
---|
| 597 | this->track[loc->part].map_coords( loc, entity->get_placement()); |
---|
| 598 | } |
---|
| 599 | entity->post_spawn (); |
---|
[2816] | 600 | } |
---|
| 601 | |
---|
| 602 | |
---|
| 603 | void World::spawn(WorldEntity* entity, Location* loc) |
---|
| 604 | { |
---|
| 605 | Location zeroLoc; |
---|
| 606 | WorldEntity* owner; |
---|
| 607 | this->entities->add (entity); |
---|
| 608 | if( loc == NULL) |
---|
| 609 | { |
---|
| 610 | zeroLoc.dist = 0; |
---|
| 611 | zeroLoc.part = 0; |
---|
| 612 | zeroLoc.pos = Vector(); |
---|
| 613 | zeroLoc.rot = Quaternion(); |
---|
| 614 | loc = &zeroLoc; |
---|
| 615 | } |
---|
| 616 | entity->init (loc, owner); |
---|
| 617 | if (entity->bFree) |
---|
| 618 | { |
---|
| 619 | this->track[loc->part].map_coords( loc, entity->get_placement()); |
---|
| 620 | } |
---|
| 621 | entity->post_spawn (); |
---|
[2644] | 622 | //return entity; |
---|
| 623 | } |
---|
[2816] | 624 | |
---|
| 625 | |
---|
| 626 | void World::spawn(WorldEntity* entity, Placement* plc) |
---|
| 627 | { |
---|
| 628 | Placement zeroPlc; |
---|
| 629 | WorldEntity* owner; |
---|
| 630 | if( plc == NULL) |
---|
| 631 | { |
---|
| 632 | zeroPlc.r = Vector(); |
---|
| 633 | zeroPlc.w = Quaternion(); |
---|
| 634 | plc = &zeroPlc; |
---|
| 635 | } |
---|
| 636 | this->entities->add (entity); |
---|
| 637 | entity->init (plc, owner); |
---|
| 638 | entity->post_spawn (); |
---|
| 639 | //return entity; |
---|
| 640 | } |
---|