[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" |
---|
[3238] | 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; |
---|
[3238] | 38 | this->entities = new tList<WorldEntity>(); |
---|
[1855] | 39 | } |
---|
| 40 | |
---|
[2636] | 41 | World::World (int worldID) |
---|
| 42 | { |
---|
| 43 | this->debugWorldNr = worldID; |
---|
| 44 | this->worldName = NULL; |
---|
[3238] | 45 | this->entities = new tList<WorldEntity>(); |
---|
[2636] | 46 | } |
---|
| 47 | |
---|
[1858] | 48 | /** |
---|
[2551] | 49 | \brief remove the World from memory |
---|
[1858] | 50 | */ |
---|
[2190] | 51 | World::~World () |
---|
[1872] | 52 | { |
---|
[3238] | 53 | printf("World::~World() - deleting current world\n"); |
---|
| 54 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
| 55 | cn->unbind(this->localPlayer); |
---|
| 56 | cn->reset(); |
---|
| 57 | this->localCamera->destroy(); |
---|
| 58 | |
---|
| 59 | WorldEntity* entity = entities->enumerate(); |
---|
| 60 | while( entity != NULL ) |
---|
| 61 | { |
---|
| 62 | entity->destroy(); |
---|
| 63 | entity = entities->nextElement(); |
---|
| 64 | } |
---|
| 65 | this->entities->destroy(); |
---|
| 66 | |
---|
[2644] | 67 | delete this->entities; |
---|
| 68 | delete this->localCamera; |
---|
[3238] | 69 | /* this->localPlayer hasn't to be deleted explicitly, it is |
---|
| 70 | contained in entities*/ |
---|
[1872] | 71 | } |
---|
[1858] | 72 | |
---|
[2636] | 73 | |
---|
[3238] | 74 | ErrorMessage World::init() |
---|
[2636] | 75 | { |
---|
| 76 | this->bPause = false; |
---|
[3238] | 77 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
| 78 | cn->addToWorld(this); |
---|
| 79 | cn->enable(true); |
---|
[2636] | 80 | } |
---|
| 81 | |
---|
[3238] | 82 | ErrorMessage World::start() |
---|
[2636] | 83 | { |
---|
[3238] | 84 | printf("World::start() - starting current World: nr %i\n", this->debugWorldNr); |
---|
| 85 | this->bQuitOrxonox = false; |
---|
| 86 | this->bQuitCurrentGame = false; |
---|
[2636] | 87 | this->mainLoop(); |
---|
| 88 | } |
---|
| 89 | |
---|
[3238] | 90 | ErrorMessage World::stop() |
---|
[2636] | 91 | { |
---|
[3238] | 92 | printf("World::stop() - got stop signal\n"); |
---|
[2636] | 93 | this->bQuitCurrentGame = true; |
---|
| 94 | } |
---|
| 95 | |
---|
[3238] | 96 | ErrorMessage World::pause() |
---|
[2636] | 97 | { |
---|
| 98 | this->isPaused = true; |
---|
| 99 | } |
---|
| 100 | |
---|
[3238] | 101 | ErrorMessage World::resume() |
---|
[2636] | 102 | { |
---|
| 103 | this->isPaused = false; |
---|
| 104 | } |
---|
| 105 | |
---|
[3238] | 106 | void World::destroy() |
---|
| 107 | { |
---|
| 108 | |
---|
| 109 | } |
---|
| 110 | |
---|
[2636] | 111 | void World::load() |
---|
| 112 | { |
---|
| 113 | if(this->debugWorldNr != -1) |
---|
| 114 | { |
---|
| 115 | switch(this->debugWorldNr) |
---|
| 116 | { |
---|
[3238] | 117 | /* |
---|
| 118 | this loads the hard-coded debug world. this only for simplicity and will be |
---|
| 119 | removed by a reald world-loader, which interprets a world-file. |
---|
| 120 | if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and |
---|
| 121 | make whatever you want... |
---|
| 122 | */ |
---|
[2636] | 123 | case DEBUG_WORLD_0: |
---|
| 124 | { |
---|
| 125 | // create some path nodes |
---|
| 126 | this->pathnodes = new Vector[6]; |
---|
| 127 | this->pathnodes[0] = Vector(0, 0, 0); |
---|
[3238] | 128 | this->pathnodes[1] = Vector(1000, 0, 0); |
---|
| 129 | // this->pathnodes[2] = Vector(-100, 140, 0); |
---|
| 130 | // this->pathnodes[3] = Vector(0, 180, 0); |
---|
| 131 | // this->pathnodes[4] = Vector(100, 140, 0); |
---|
| 132 | // this->pathnodes[5] = Vector(100, 40, 0); |
---|
[2636] | 133 | |
---|
| 134 | // create the tracks |
---|
[3238] | 135 | this->tracklen = 2; |
---|
| 136 | this->track = new Track[2]; |
---|
[2636] | 137 | for( int i = 0; i < this->tracklen; i++) |
---|
| 138 | { |
---|
| 139 | this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); |
---|
| 140 | } |
---|
[3238] | 141 | // !\todo old track-system has to be removed |
---|
| 142 | |
---|
[2636] | 143 | // create a player |
---|
[2644] | 144 | WorldEntity* myPlayer = new Player(); |
---|
| 145 | this->spawn(myPlayer); |
---|
[2640] | 146 | this->localPlayer = myPlayer; |
---|
| 147 | |
---|
[2636] | 148 | // bind input |
---|
| 149 | Orxonox *orx = Orxonox::getInstance(); |
---|
[3238] | 150 | orx->getLocalInput()->bind (myPlayer); |
---|
[2636] | 151 | |
---|
| 152 | // bind camera |
---|
| 153 | this->localCamera = new Camera(this); |
---|
| 154 | this->getCamera()->bind (myPlayer); |
---|
[3238] | 155 | |
---|
| 156 | Placement* plc = new Placement; |
---|
| 157 | plc->r = Vector(100, 10, 10); |
---|
| 158 | plc->w = Quaternion(); |
---|
| 159 | WorldEntity* env = new Environment(); |
---|
| 160 | this->spawn(env, plc); |
---|
| 161 | |
---|
[2636] | 162 | break; |
---|
| 163 | } |
---|
| 164 | case DEBUG_WORLD_1: |
---|
| 165 | { |
---|
| 166 | // create some path nodes |
---|
| 167 | this->pathnodes = new Vector[6]; |
---|
| 168 | this->pathnodes[0] = Vector(0, 0, 0); |
---|
| 169 | this->pathnodes[1] = Vector(20, 10, 10); |
---|
| 170 | this->pathnodes[2] = Vector(40, 0, 10); |
---|
| 171 | this->pathnodes[3] = Vector(60, 10, 0); |
---|
| 172 | this->pathnodes[4] = Vector(80, 20, 10); |
---|
| 173 | this->pathnodes[5] = Vector(30, 50, 0); |
---|
| 174 | |
---|
| 175 | // create the tracks |
---|
| 176 | this->tracklen = 6; |
---|
| 177 | this->track = new Track[6]; |
---|
| 178 | for( int i = 0; i < this->tracklen; i++) |
---|
| 179 | { |
---|
| 180 | this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); |
---|
| 181 | } |
---|
[3238] | 182 | |
---|
[2636] | 183 | // create a player |
---|
[2644] | 184 | WorldEntity* myPlayer = new Player(); |
---|
| 185 | this->spawn(myPlayer); |
---|
[3238] | 186 | this->localPlayer = myPlayer; |
---|
[2636] | 187 | |
---|
| 188 | // bind input |
---|
| 189 | Orxonox *orx = Orxonox::getInstance(); |
---|
[3238] | 190 | orx->getLocalInput()->bind (myPlayer); |
---|
[2636] | 191 | |
---|
| 192 | // bind camera |
---|
| 193 | this->localCamera = new Camera(this); |
---|
| 194 | this->getCamera()->bind (myPlayer); |
---|
| 195 | break; |
---|
| 196 | } |
---|
| 197 | default: |
---|
| 198 | printf("World::load() - no world with ID %i found", this->debugWorldNr ); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | else if(this->worldName != NULL) |
---|
| 202 | { |
---|
| 203 | |
---|
| 204 | } |
---|
| 205 | |
---|
[3238] | 206 | // initialize debug coord system |
---|
| 207 | objectList = glGenLists(1); |
---|
| 208 | glNewList (objectList, GL_COMPILE); |
---|
| 209 | glLoadIdentity(); |
---|
| 210 | glColor3f(1.0,0,0); |
---|
| 211 | glBegin(GL_QUADS); |
---|
[2636] | 212 | |
---|
[3238] | 213 | int sizeX = 100; |
---|
| 214 | int sizeY = 80; |
---|
| 215 | float length = 1000; |
---|
| 216 | float width = 200; |
---|
| 217 | float widthX = float (length /sizeX); |
---|
| 218 | float widthY = float (width /sizeY); |
---|
[2551] | 219 | |
---|
[3238] | 220 | float height [sizeX][sizeY]; |
---|
| 221 | Vector normal_vectors[sizeX][sizeY]; |
---|
[2551] | 222 | |
---|
[3238] | 223 | |
---|
| 224 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
| 225 | for (int j = 0; j<sizeY-1;j+=1) |
---|
| 226 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
| 227 | #ifdef __WIN32__ |
---|
| 228 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
| 229 | #else |
---|
| 230 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
| 231 | #endif |
---|
[1858] | 232 | |
---|
[3238] | 233 | //Die Hügel ein wenig glätten |
---|
| 234 | for (int h=1; h<2;h++) |
---|
| 235 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
| 236 | for(int j=1;j<sizeY-2;j+=1) |
---|
| 237 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
[2551] | 238 | |
---|
[3238] | 239 | //Berechnung von normalen Vektoren |
---|
[2551] | 240 | |
---|
[3238] | 241 | for(int i=1;i<sizeX-2;i+=1) |
---|
| 242 | for(int j=1;j<sizeY-2 ;j+=1) |
---|
| 243 | { |
---|
| 244 | Vector v1 = Vector (widthX*(1), widthY*(j) , height[i][j]); |
---|
| 245 | Vector v2 = Vector (widthX*(i-1), widthY*(j) , height[i-1][j]); |
---|
| 246 | Vector v3 = Vector (widthX*(i), widthY*(j+1), height[i][j+1]); |
---|
| 247 | Vector v4 = Vector (widthX*(i+1), widthY*(j), height[i+1][j]); |
---|
| 248 | Vector v5 = Vector (widthX*(i), widthY*(j-1), height[i][j-1]); |
---|
| 249 | |
---|
| 250 | Vector c1 = v2 - v1; |
---|
| 251 | Vector c2 = v3 - v1; |
---|
| 252 | Vector c3= v4 - v1; |
---|
| 253 | Vector c4 = v5 - v1; |
---|
| 254 | Vector zero = Vector (0,0,0); |
---|
| 255 | normal_vectors[i][j]=c1.cross(v4-v2)+c2.cross(v1-v3)+c3.cross(v2-v4)+c4.cross(v3-v1); |
---|
| 256 | normal_vectors[i][j].normalize(); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | int snowheight=3; |
---|
| 260 | for ( int i = 0; i<sizeX; i+=1) |
---|
| 261 | for (int j = 0; j<sizeY;j+=1) |
---|
| 262 | { |
---|
| 263 | Vector v1 = Vector (widthX*(i), widthY*(j) -width/2, height[i][j]-20 ); |
---|
| 264 | Vector v2 = Vector (widthX*(i+1), widthY*(j) -width/2, height[i+1][j]-20); |
---|
| 265 | Vector v3 = Vector (widthX*(i+1), widthY*(j+1)-width/2, height[i+1][j+1]-20); |
---|
| 266 | Vector v4 = Vector (widthX*(i), widthY*(j+1)-width/2, height[i][j+1]-20); |
---|
| 267 | float a[3]; |
---|
| 268 | if(height[i][j]<snowheight){ |
---|
| 269 | a[0]=0; |
---|
| 270 | a[1]=1.0-height[i][j]/10-.3; |
---|
| 271 | a[2]=0; |
---|
| 272 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 273 | } |
---|
| 274 | else{ |
---|
| 275 | a[0]=1.0; |
---|
| 276 | a[1]=1.0; |
---|
| 277 | a[2]=1.0; |
---|
| 278 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 279 | |
---|
| 280 | } |
---|
| 281 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
| 282 | glVertex3f(v1.x, v1.y, v1.z); |
---|
| 283 | if(height[i+1][j]<snowheight){ |
---|
| 284 | a[0]=0; |
---|
| 285 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
| 286 | a[2]=0; |
---|
| 287 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 288 | } |
---|
| 289 | else{ |
---|
| 290 | a[0]=1.0; |
---|
| 291 | a[1]=1.0; |
---|
| 292 | a[2]=1.0; |
---|
| 293 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 294 | |
---|
| 295 | } |
---|
| 296 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
| 297 | glVertex3f(v2.x, v2.y, v2.z); |
---|
| 298 | if(height[i+1][j+1]<snowheight){ |
---|
| 299 | a[0]=0; |
---|
| 300 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
| 301 | a[2]=0; |
---|
| 302 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 303 | } |
---|
| 304 | else{ |
---|
| 305 | a[0]=1.0; |
---|
| 306 | a[1]=1.0; |
---|
| 307 | a[2]=1.0; |
---|
| 308 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | } |
---|
| 312 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
| 313 | glVertex3f(v3.x, v3.y, v3.z); |
---|
| 314 | if(height[i][j+1]<snowheight){ |
---|
| 315 | a[0]=0; |
---|
| 316 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
| 317 | a[2]=0; |
---|
| 318 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 319 | } |
---|
| 320 | else{ |
---|
| 321 | a[0]=1.0; |
---|
| 322 | a[1]=1.0; |
---|
| 323 | a[2]=1.0; |
---|
| 324 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 325 | } |
---|
| 326 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
| 327 | glVertex3f(v4.x, v4.y, v4.z); |
---|
| 328 | |
---|
| 329 | } |
---|
| 330 | glEnd(); |
---|
| 331 | /* |
---|
[2551] | 332 | glBegin(GL_LINES); |
---|
| 333 | for( float x = -128.0; x < 128.0; x += 25.0) |
---|
| 334 | { |
---|
| 335 | for( float y = -128.0; y < 128.0; y += 25.0) |
---|
[2190] | 336 | { |
---|
[2551] | 337 | glColor3f(1,0,0); |
---|
| 338 | glVertex3f(x,y,-128.0); |
---|
| 339 | glVertex3f(x,y,0.0); |
---|
| 340 | glColor3f(0.5,0,0); |
---|
| 341 | glVertex3f(x,y,0.0); |
---|
| 342 | glVertex3f(x,y,128.0); |
---|
[1931] | 343 | } |
---|
[2551] | 344 | } |
---|
| 345 | for( float y = -128.0; y < 128.0; y += 25.0) |
---|
| 346 | { |
---|
| 347 | for( float z = -128.0; z < 128.0; z += 25.0) |
---|
[2190] | 348 | { |
---|
[2551] | 349 | glColor3f(0,1,0); |
---|
| 350 | glVertex3f(-128.0,y,z); |
---|
| 351 | glVertex3f(0.0,y,z); |
---|
| 352 | glColor3f(0,0.5,0); |
---|
| 353 | glVertex3f(0.0,y,z); |
---|
| 354 | glVertex3f(128.0,y,z); |
---|
[2190] | 355 | } |
---|
[2551] | 356 | } |
---|
| 357 | for( float x = -128.0; x < 128.0; x += 25.0) |
---|
| 358 | { |
---|
| 359 | for( float z = -128.0; z < 128.0; z += 25.0) |
---|
[2190] | 360 | { |
---|
[2551] | 361 | glColor3f(0,0,1); |
---|
| 362 | glVertex3f(x,-128.0,z); |
---|
| 363 | glVertex3f(x,0.0,z); |
---|
| 364 | glColor3f(0,0,0.5); |
---|
| 365 | glVertex3f(x,0.0,z); |
---|
| 366 | glVertex3f(x,128.0,z); |
---|
[2190] | 367 | } |
---|
[2551] | 368 | |
---|
| 369 | } |
---|
[3238] | 370 | */ |
---|
[2551] | 371 | //draw track |
---|
[3238] | 372 | glBegin(GL_LINES); |
---|
[2551] | 373 | glColor3f(0,1,1); |
---|
| 374 | for( int i = 0; i < tracklen; i++) |
---|
| 375 | { |
---|
| 376 | glVertex3f(pathnodes[i].x,pathnodes[i].y,pathnodes[i].z); |
---|
| 377 | glVertex3f(pathnodes[(i+1)%tracklen].x,pathnodes[(i+1)%tracklen].y,pathnodes[(i+1)%tracklen].z); |
---|
| 378 | } |
---|
| 379 | glEnd(); |
---|
[3238] | 380 | glEndList(); |
---|
[1931] | 381 | } |
---|
| 382 | |
---|
[3238] | 383 | |
---|
[1931] | 384 | /** |
---|
[3238] | 385 | \brief checks for collisions |
---|
| 386 | |
---|
| 387 | This method runs through all WorldEntities known to the world and checks for collisions |
---|
| 388 | between them. In case of collisions the collide() method of the corresponding entities |
---|
| 389 | is called. |
---|
| 390 | */ |
---|
| 391 | void World::collide () |
---|
| 392 | { |
---|
| 393 | /* |
---|
| 394 | List *a, *b; |
---|
| 395 | WorldEntity *aobj, *bobj; |
---|
| 396 | |
---|
| 397 | a = entities; |
---|
| 398 | |
---|
| 399 | while( a != NULL) |
---|
| 400 | { |
---|
| 401 | aobj = a->nextElement(); |
---|
| 402 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
| 403 | { |
---|
| 404 | b = a->nextElement(); |
---|
| 405 | while( b != NULL ) |
---|
| 406 | { |
---|
| 407 | bobj = b->nextElement(); |
---|
| 408 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
| 409 | { |
---|
| 410 | unsigned long ahitflg, bhitflg; |
---|
| 411 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
---|
| 412 | &ahitflg, &bobj->place, bobj->collisioncluster, |
---|
| 413 | &bhitflg) ); |
---|
| 414 | { |
---|
| 415 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
| 416 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
| 417 | } |
---|
| 418 | } |
---|
| 419 | b = b->nextElement(); |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | a = a->enumerate(); |
---|
| 423 | } |
---|
| 424 | */ |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | /** |
---|
| 428 | \brief runs through all entities calling their draw() methods |
---|
| 429 | */ |
---|
| 430 | void World::draw () |
---|
| 431 | { |
---|
| 432 | // draw geometry |
---|
| 433 | |
---|
| 434 | // draw entities |
---|
| 435 | WorldEntity* entity; |
---|
| 436 | |
---|
| 437 | entity = this->entities->enumerate(); |
---|
| 438 | while( entity != NULL ) |
---|
| 439 | { |
---|
| 440 | if( entity->bDraw ) entity->draw(); |
---|
| 441 | entity = this->entities->nextElement(); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | // draw debug coord system |
---|
| 446 | glCallList (objectList); |
---|
| 447 | |
---|
| 448 | |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | /** |
---|
[2551] | 452 | \brief updates Placements and notifies entities when they left the |
---|
| 453 | world |
---|
| 454 | |
---|
| 455 | This runs trough all WorldEntities and maps Locations to Placements |
---|
| 456 | if they are bound, checks whether they left the level boundaries |
---|
| 457 | and calls appropriate functions. |
---|
[1883] | 458 | */ |
---|
[2190] | 459 | void World::update () |
---|
[1883] | 460 | { |
---|
[3238] | 461 | //List<WorldEntity> *l; |
---|
[2551] | 462 | WorldEntity* entity; |
---|
| 463 | Location* loc; |
---|
| 464 | Placement* plc; |
---|
| 465 | Uint32 t; |
---|
| 466 | |
---|
[3238] | 467 | // l = entities->enumerate(); |
---|
| 468 | entity = this->entities->enumerate(); |
---|
| 469 | while( entity != NULL ) |
---|
[2551] | 470 | { |
---|
[3238] | 471 | |
---|
[2551] | 472 | |
---|
| 473 | if( !entity->isFree() ) |
---|
| 474 | { |
---|
[3238] | 475 | loc = entity->getLocation(); |
---|
| 476 | plc = entity->getPlacement(); |
---|
[2551] | 477 | t = loc->part; |
---|
| 478 | |
---|
| 479 | /* check if entity has still a legal track-id */ |
---|
| 480 | if( t >= tracklen ) |
---|
| 481 | { |
---|
| 482 | printf("An entity is out of the game area\n"); |
---|
[3238] | 483 | entity->leftWorld (); |
---|
[2551] | 484 | } |
---|
| 485 | else |
---|
| 486 | { |
---|
[3238] | 487 | while( track[t].mapCoords( loc, plc) ) |
---|
[2190] | 488 | { |
---|
[3238] | 489 | track[t].postLeave (entity); |
---|
[2551] | 490 | if( loc->part >= tracklen ) |
---|
| 491 | { |
---|
| 492 | printf("An entity has left the game area\n"); |
---|
[3238] | 493 | entity->leftWorld (); |
---|
[2551] | 494 | break; |
---|
| 495 | } |
---|
[3238] | 496 | track[loc->part].postEnter (entity); |
---|
[2190] | 497 | } |
---|
[2551] | 498 | } |
---|
[2190] | 499 | } |
---|
[2551] | 500 | else |
---|
| 501 | { |
---|
[3238] | 502 | /* \todo: implement check whether this particular free entity |
---|
[2551] | 503 | is out of the game area |
---|
[3238] | 504 | \todo: call function to notify the entity that it left |
---|
[2551] | 505 | the game area |
---|
| 506 | */ |
---|
| 507 | } |
---|
| 508 | |
---|
[3238] | 509 | entity = entities->nextElement(); |
---|
[2551] | 510 | } |
---|
| 511 | |
---|
[1883] | 512 | } |
---|
| 513 | |
---|
[2077] | 514 | /** |
---|
[2551] | 515 | \brief relays the passed time since the last frame to entities and Track parts |
---|
| 516 | \param deltaT: the time passed since the last frame in milliseconds |
---|
[2077] | 517 | */ |
---|
[3238] | 518 | void World::timeSlice (Uint32 deltaT) |
---|
[2077] | 519 | { |
---|
[3238] | 520 | //List<WorldEntity> *l; |
---|
[2551] | 521 | WorldEntity* entity; |
---|
[3238] | 522 | float seconds = deltaT / 1000.0; |
---|
[2551] | 523 | |
---|
[3238] | 524 | entity = entities->enumerate(); |
---|
| 525 | while( entity != NULL) |
---|
[2551] | 526 | { |
---|
| 527 | entity->tick (seconds); |
---|
[3238] | 528 | entity = entities->nextElement(); |
---|
[2551] | 529 | } |
---|
[3238] | 530 | |
---|
| 531 | //for( int i = 0; i < tracklen; i++) track[i].tick (seconds); |
---|
[2077] | 532 | } |
---|
[1883] | 533 | |
---|
[2190] | 534 | /** |
---|
[2551] | 535 | \brief removes level data from memory |
---|
[1858] | 536 | */ |
---|
[2190] | 537 | void World::unload() |
---|
[1858] | 538 | { |
---|
[2551] | 539 | if( pathnodes) delete []pathnodes; |
---|
| 540 | if( track) delete []pathnodes; |
---|
[1883] | 541 | } |
---|
[1879] | 542 | |
---|
[2636] | 543 | |
---|
| 544 | |
---|
[2190] | 545 | /** |
---|
[2636] | 546 | \brief calls the correct mapping function to convert a given "look at"-Location to a |
---|
| 547 | Camera Placement |
---|
[1858] | 548 | */ |
---|
[3238] | 549 | void World::calcCameraPos (Location* loc, Placement* plc) |
---|
[1858] | 550 | { |
---|
[3238] | 551 | track[loc->part].mapCamera (loc, plc); |
---|
[2636] | 552 | } |
---|
| 553 | |
---|
| 554 | |
---|
| 555 | void World::setTrackLen(Uint32 len) |
---|
| 556 | { |
---|
| 557 | this->tracklen = len; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | int World::getTrackLen() |
---|
| 561 | { |
---|
| 562 | return this->tracklen; |
---|
| 563 | } |
---|
| 564 | |
---|
[3238] | 565 | |
---|
| 566 | |
---|
| 567 | /** |
---|
| 568 | \brief function to put your own debug stuff into it. it can display informations about |
---|
| 569 | the current class/procedure |
---|
| 570 | */ |
---|
[2640] | 571 | void World::debug() |
---|
| 572 | { |
---|
[3238] | 573 | //List<WorldEntity> *l; |
---|
[2640] | 574 | WorldEntity* entity; |
---|
| 575 | |
---|
| 576 | printf("counting all entities\n"); |
---|
[3238] | 577 | printf("World::debug() - enumerate()\n"); |
---|
| 578 | entity = entities->enumerate(); |
---|
| 579 | while( entity != NULL ) |
---|
[2640] | 580 | { |
---|
| 581 | if( entity->bDraw ) printf("got an entity\n"); |
---|
[3238] | 582 | entity = entities->nextElement(); |
---|
[2640] | 583 | } |
---|
| 584 | } |
---|
[2636] | 585 | |
---|
[2640] | 586 | |
---|
[3238] | 587 | /* |
---|
| 588 | \brief main loop of the world: executing all world relevant function |
---|
| 589 | |
---|
| 590 | in this loop we synchronize (if networked), handle input events, give the heart-beat to |
---|
| 591 | all other member-entities of the world (tick to player, enemies etc.), checking for |
---|
| 592 | collisions drawing everything to the screen. |
---|
| 593 | */ |
---|
[2636] | 594 | void World::mainLoop() |
---|
| 595 | { |
---|
| 596 | this->lastFrame = SDL_GetTicks(); |
---|
[3238] | 597 | printf("World::mainLoop() - Entering main loop\n"); |
---|
| 598 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
[2551] | 599 | { |
---|
[2636] | 600 | // Network |
---|
| 601 | synchronize(); |
---|
| 602 | // Process input |
---|
[3238] | 603 | handleInput(); |
---|
| 604 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
| 605 | { |
---|
| 606 | printf("World::mainLoop() - leaving loop earlier...\n"); |
---|
| 607 | break; |
---|
| 608 | } |
---|
[2636] | 609 | // Process time |
---|
[3238] | 610 | timeSlice(); |
---|
[2636] | 611 | // Process collision |
---|
| 612 | collision(); |
---|
| 613 | // Draw |
---|
| 614 | display(); |
---|
[3238] | 615 | |
---|
| 616 | for(int i = 0; i < 10000000; i++) {} |
---|
[2551] | 617 | } |
---|
[3238] | 618 | printf("World::mainLoop() - Exiting the main loop\n"); |
---|
[1899] | 619 | } |
---|
| 620 | |
---|
[2190] | 621 | /** |
---|
[2636] | 622 | \brief synchronize local data with remote data |
---|
[1855] | 623 | */ |
---|
[2636] | 624 | void World::synchronize () |
---|
[1855] | 625 | { |
---|
[2636] | 626 | // Get remote input |
---|
| 627 | // Update synchronizables |
---|
[1855] | 628 | } |
---|
[2636] | 629 | |
---|
| 630 | /** |
---|
| 631 | \brief run all input processing |
---|
[3238] | 632 | |
---|
| 633 | the command node is the central input event dispatcher. the node uses the even-queue from |
---|
| 634 | sdl and has its own event-passing-queue. |
---|
[2636] | 635 | */ |
---|
[3238] | 636 | void World::handleInput () |
---|
[2636] | 637 | { |
---|
| 638 | // localinput |
---|
[3238] | 639 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
| 640 | cn->process(); |
---|
[2636] | 641 | // remoteinput |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | /** |
---|
| 645 | \brief advance the timeline |
---|
[3238] | 646 | |
---|
| 647 | this calculates the time used to process one frame (with all input handling, drawing, etc) |
---|
| 648 | the time is mesured in ms and passed to all world-entities and other classes that need |
---|
| 649 | a heart-beat. |
---|
[2636] | 650 | */ |
---|
[3238] | 651 | void World::timeSlice () |
---|
[2636] | 652 | { |
---|
| 653 | Uint32 currentFrame = SDL_GetTicks(); |
---|
| 654 | if(!this->bPause) |
---|
| 655 | { |
---|
| 656 | Uint32 dt = currentFrame - this->lastFrame; |
---|
[3238] | 657 | |
---|
[2636] | 658 | if(dt > 0) |
---|
| 659 | { |
---|
| 660 | float fps = 1000/dt; |
---|
| 661 | printf("fps = %f\n", fps); |
---|
| 662 | } |
---|
| 663 | else |
---|
| 664 | { |
---|
[3238] | 665 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
| 666 | nothing. |
---|
| 667 | */ |
---|
| 668 | printf("fps = 1000 - frame rate is adjusted\n"); |
---|
| 669 | SDL_Delay(10); |
---|
| 670 | dt = 10; |
---|
[2636] | 671 | } |
---|
[3238] | 672 | this->timeSlice (dt); |
---|
[2636] | 673 | this->update (); |
---|
[3238] | 674 | this->localCamera->timeSlice(dt); |
---|
[2636] | 675 | } |
---|
| 676 | this->lastFrame = currentFrame; |
---|
| 677 | } |
---|
| 678 | |
---|
[3238] | 679 | |
---|
[2636] | 680 | /** |
---|
| 681 | \brief compute collision detection |
---|
| 682 | */ |
---|
| 683 | void World::collision () |
---|
| 684 | { |
---|
| 685 | this->collide (); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | |
---|
| 689 | /** |
---|
[3238] | 690 | \brief render the current frame |
---|
| 691 | |
---|
| 692 | clear all buffers and draw the world |
---|
[2636] | 693 | */ |
---|
| 694 | void World::display () |
---|
| 695 | { |
---|
| 696 | // clear buffer |
---|
| 697 | glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
| 698 | // set camera |
---|
| 699 | this->localCamera->apply (); |
---|
| 700 | // draw world |
---|
| 701 | this->draw(); |
---|
| 702 | // draw HUD |
---|
| 703 | // flip buffers |
---|
| 704 | SDL_GL_SwapBuffers(); |
---|
| 705 | } |
---|
| 706 | |
---|
[3238] | 707 | /** |
---|
| 708 | \brief give back active camera |
---|
| 709 | |
---|
| 710 | this passes back the actualy active camera |
---|
| 711 | \todo ability to define more than one camera or camera-places |
---|
| 712 | */ |
---|
[2636] | 713 | Camera* World::getCamera() |
---|
| 714 | { |
---|
| 715 | return this->localCamera; |
---|
| 716 | } |
---|
[2644] | 717 | |
---|
| 718 | |
---|
[3238] | 719 | /** |
---|
| 720 | \brief add and spawn a new entity to this world |
---|
| 721 | \param entity to be added |
---|
| 722 | */ |
---|
[2644] | 723 | void World::spawn(WorldEntity* entity) |
---|
| 724 | { |
---|
| 725 | Location zeroloc; |
---|
| 726 | Location* loc = NULL; |
---|
| 727 | WorldEntity* owner; |
---|
[3238] | 728 | |
---|
| 729 | entities->add (entity); |
---|
| 730 | zeroloc.dist = 0; |
---|
| 731 | zeroloc.part = 0; |
---|
| 732 | zeroloc.pos = Vector(); |
---|
| 733 | zeroloc.rot = Quaternion(); |
---|
| 734 | loc = &zeroloc; |
---|
[2644] | 735 | entity->init (loc, owner); |
---|
| 736 | if (entity->bFree) |
---|
| 737 | { |
---|
[3238] | 738 | this->track[loc->part].mapCoords( loc, entity->getPlacement()); |
---|
[2644] | 739 | } |
---|
[3238] | 740 | entity->postSpawn (); |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | |
---|
| 744 | /** |
---|
| 745 | \brief add and spawn a new entity to this world |
---|
| 746 | \param entity to be added |
---|
| 747 | \param location where to add |
---|
| 748 | */ |
---|
| 749 | void World::spawn(WorldEntity* entity, Location* loc) |
---|
| 750 | { |
---|
| 751 | Location zeroLoc; |
---|
| 752 | WorldEntity* owner; |
---|
| 753 | this->entities->add (entity); |
---|
| 754 | if( loc == NULL) |
---|
| 755 | { |
---|
| 756 | zeroLoc.dist = 0; |
---|
| 757 | zeroLoc.part = 0; |
---|
| 758 | zeroLoc.pos = Vector(); |
---|
| 759 | zeroLoc.rot = Quaternion(); |
---|
| 760 | loc = &zeroLoc; |
---|
| 761 | } |
---|
| 762 | entity->init (loc, owner); |
---|
| 763 | if (entity->bFree) |
---|
| 764 | { |
---|
| 765 | this->track[loc->part].mapCoords( loc, entity->getPlacement()); |
---|
| 766 | } |
---|
| 767 | entity->postSpawn (); |
---|
[2644] | 768 | //return entity; |
---|
| 769 | } |
---|
[3238] | 770 | |
---|
| 771 | |
---|
| 772 | /** |
---|
| 773 | \brief add and spawn a new entity to this world |
---|
| 774 | \param entity to be added |
---|
| 775 | \param place where to be added |
---|
| 776 | */ |
---|
| 777 | void World::spawn(WorldEntity* entity, Placement* plc) |
---|
| 778 | { |
---|
| 779 | Placement zeroPlc; |
---|
| 780 | WorldEntity* owner; |
---|
| 781 | if( plc == NULL) |
---|
| 782 | { |
---|
| 783 | zeroPlc.r = Vector(); |
---|
| 784 | zeroPlc.w = Quaternion(); |
---|
| 785 | plc = &zeroPlc; |
---|
| 786 | } |
---|
| 787 | this->entities->add (entity); |
---|
| 788 | entity->init (plc, owner); |
---|
| 789 | entity->postSpawn (); |
---|
| 790 | //return entity; |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | |
---|
| 794 | /* |
---|
| 795 | \brief commands that the world must catch |
---|
| 796 | \returns false if not used by the world |
---|
| 797 | */ |
---|
| 798 | bool World::command(Command* cmd) |
---|
| 799 | { |
---|
| 800 | return false; |
---|
| 801 | } |
---|