[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; |
---|
[2822] | 38 | this->entities = new tList<WorldEntity>(); |
---|
[1855] | 39 | } |
---|
| 40 | |
---|
[2636] | 41 | World::World (int worldID) |
---|
| 42 | { |
---|
| 43 | this->debugWorldNr = worldID; |
---|
| 44 | this->worldName = NULL; |
---|
[2822] | 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 | { |
---|
[3220] | 53 | printf("World::~World() - deleting current world\n"); |
---|
[3226] | 54 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
[3220] | 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; |
---|
[3220] | 69 | /* this->localPlayer hasn't to be deleted explicitly, it is |
---|
| 70 | contained in entities*/ |
---|
[1872] | 71 | } |
---|
[1858] | 72 | |
---|
[2636] | 73 | |
---|
[3222] | 74 | ErrorMessage World::init() |
---|
[2636] | 75 | { |
---|
| 76 | this->bPause = false; |
---|
[3226] | 77 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
[3216] | 78 | cn->addToWorld(this); |
---|
| 79 | cn->enable(true); |
---|
[2636] | 80 | } |
---|
| 81 | |
---|
[3222] | 82 | ErrorMessage World::start() |
---|
[2636] | 83 | { |
---|
[3220] | 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 | |
---|
[3222] | 90 | ErrorMessage World::stop() |
---|
[2636] | 91 | { |
---|
[3220] | 92 | printf("World::stop() - got stop signal\n"); |
---|
[2636] | 93 | this->bQuitCurrentGame = true; |
---|
| 94 | } |
---|
| 95 | |
---|
[3222] | 96 | ErrorMessage World::pause() |
---|
[2636] | 97 | { |
---|
| 98 | this->isPaused = true; |
---|
| 99 | } |
---|
| 100 | |
---|
[3222] | 101 | ErrorMessage World::resume() |
---|
[2636] | 102 | { |
---|
| 103 | this->isPaused = false; |
---|
| 104 | } |
---|
| 105 | |
---|
[3221] | 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 | { |
---|
[3225] | 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); |
---|
[2792] | 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 |
---|
[2816] | 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 | } |
---|
[3194] | 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(); |
---|
[3226] | 150 | orx->getLocalInput()->bind (myPlayer); |
---|
[2636] | 151 | |
---|
| 152 | // bind camera |
---|
| 153 | this->localCamera = new Camera(this); |
---|
| 154 | this->getCamera()->bind (myPlayer); |
---|
[2816] | 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 | } |
---|
[3194] | 182 | |
---|
[2636] | 183 | // create a player |
---|
[2644] | 184 | WorldEntity* myPlayer = new Player(); |
---|
| 185 | this->spawn(myPlayer); |
---|
[3194] | 186 | this->localPlayer = myPlayer; |
---|
[2636] | 187 | |
---|
| 188 | // bind input |
---|
| 189 | Orxonox *orx = Orxonox::getInstance(); |
---|
[3226] | 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 | } |
---|
[2731] | 205 | |
---|
| 206 | // initialize debug coord system |
---|
| 207 | objectList = glGenLists(1); |
---|
| 208 | glNewList (objectList, GL_COMPILE); |
---|
| 209 | glLoadIdentity(); |
---|
[2792] | 210 | glColor3f(1.0,0,0); |
---|
[2817] | 211 | glBegin(GL_QUADS); |
---|
[3200] | 212 | |
---|
| 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); |
---|
[3199] | 219 | |
---|
[3200] | 220 | float height [sizeX][sizeY]; |
---|
| 221 | Vector normal_vectors[sizeX][sizeY]; |
---|
[3199] | 222 | |
---|
[3200] | 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; |
---|
[3199] | 227 | #ifdef __WIN32__ |
---|
[3200] | 228 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
[3199] | 229 | #else |
---|
[3200] | 230 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
[3199] | 231 | #endif |
---|
[3200] | 232 | |
---|
[3199] | 233 | //Die Hügel ein wenig glätten |
---|
| 234 | for (int h=1; h<2;h++) |
---|
[3200] | 235 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
| 236 | for(int j=1;j<sizeY-2;j+=1) |
---|
[3199] | 237 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
| 238 | |
---|
| 239 | //Berechnung von normalen Vektoren |
---|
[3200] | 240 | |
---|
| 241 | for(int i=1;i<sizeX-2;i+=1) |
---|
| 242 | for(int j=1;j<sizeY-2 ;j+=1) |
---|
[2792] | 243 | { |
---|
[3200] | 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]); |
---|
[3199] | 249 | |
---|
[3200] | 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); |
---|
[3199] | 256 | normal_vectors[i][j].normalize(); |
---|
[3200] | 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); |
---|
[3199] | 273 | } |
---|
[3200] | 274 | else{ |
---|
[3199] | 275 | a[0]=1.0; |
---|
| 276 | a[1]=1.0; |
---|
| 277 | a[2]=1.0; |
---|
| 278 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
[2817] | 279 | |
---|
[3199] | 280 | } |
---|
[3200] | 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 | } |
---|
[3199] | 330 | glEnd(); |
---|
| 331 | /* |
---|
[2792] | 332 | glBegin(GL_LINES); |
---|
[2731] | 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) |
---|
| 336 | { |
---|
| 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); |
---|
| 343 | } |
---|
| 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) |
---|
| 348 | { |
---|
| 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); |
---|
| 355 | } |
---|
| 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) |
---|
| 360 | { |
---|
| 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); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | } |
---|
[2792] | 370 | */ |
---|
[2731] | 371 | //draw track |
---|
[2792] | 372 | glBegin(GL_LINES); |
---|
[2731] | 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(); |
---|
| 380 | glEndList(); |
---|
[2636] | 381 | } |
---|
| 382 | |
---|
| 383 | |
---|
| 384 | /** |
---|
[2551] | 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. |
---|
[1858] | 390 | */ |
---|
[2190] | 391 | void World::collide () |
---|
[1858] | 392 | { |
---|
[2816] | 393 | /* |
---|
| 394 | List *a, *b; |
---|
[2551] | 395 | WorldEntity *aobj, *bobj; |
---|
[2816] | 396 | |
---|
| 397 | a = entities; |
---|
[2551] | 398 | |
---|
| 399 | while( a != NULL) |
---|
| 400 | { |
---|
[2816] | 401 | aobj = a->nextElement(); |
---|
[2551] | 402 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
[2190] | 403 | { |
---|
[2816] | 404 | b = a->nextElement(); |
---|
[2551] | 405 | while( b != NULL ) |
---|
| 406 | { |
---|
[2816] | 407 | bobj = b->nextElement(); |
---|
[2551] | 408 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
[2190] | 409 | { |
---|
[2551] | 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 | } |
---|
[2190] | 418 | } |
---|
[2816] | 419 | b = b->nextElement(); |
---|
[2551] | 420 | } |
---|
[2190] | 421 | } |
---|
[2816] | 422 | a = a->enumerate(); |
---|
[2551] | 423 | } |
---|
[2816] | 424 | */ |
---|
[1858] | 425 | } |
---|
| 426 | |
---|
| 427 | /** |
---|
[2551] | 428 | \brief runs through all entities calling their draw() methods |
---|
[1931] | 429 | */ |
---|
[2190] | 430 | void World::draw () |
---|
[2077] | 431 | { |
---|
[2551] | 432 | // draw geometry |
---|
| 433 | |
---|
| 434 | // draw entities |
---|
| 435 | WorldEntity* entity; |
---|
| 436 | |
---|
[2822] | 437 | entity = this->entities->enumerate(); |
---|
[2816] | 438 | while( entity != NULL ) |
---|
[2551] | 439 | { |
---|
[2822] | 440 | if( entity->bDraw ) entity->draw(); |
---|
| 441 | entity = this->entities->nextElement(); |
---|
[2551] | 442 | } |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | // draw debug coord system |
---|
[2731] | 446 | glCallList (objectList); |
---|
[2551] | 447 | |
---|
[2731] | 448 | |
---|
[1931] | 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 | { |
---|
[2816] | 461 | //List<WorldEntity> *l; |
---|
[2551] | 462 | WorldEntity* entity; |
---|
| 463 | Location* loc; |
---|
| 464 | Placement* plc; |
---|
| 465 | Uint32 t; |
---|
| 466 | |
---|
[2816] | 467 | // l = entities->enumerate(); |
---|
| 468 | entity = this->entities->enumerate(); |
---|
| 469 | while( entity != NULL ) |
---|
[2551] | 470 | { |
---|
[2816] | 471 | |
---|
[2551] | 472 | |
---|
| 473 | if( !entity->isFree() ) |
---|
| 474 | { |
---|
[3233] | 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"); |
---|
[3233] | 483 | entity->leftWorld (); |
---|
[2551] | 484 | } |
---|
| 485 | else |
---|
| 486 | { |
---|
[3233] | 487 | while( track[t].mapCoords( loc, plc) ) |
---|
[2190] | 488 | { |
---|
[3233] | 489 | track[t].postLeave (entity); |
---|
[2551] | 490 | if( loc->part >= tracklen ) |
---|
| 491 | { |
---|
| 492 | printf("An entity has left the game area\n"); |
---|
[3233] | 493 | entity->leftWorld (); |
---|
[2551] | 494 | break; |
---|
| 495 | } |
---|
[3233] | 496 | track[loc->part].postEnter (entity); |
---|
[2190] | 497 | } |
---|
[2551] | 498 | } |
---|
[2190] | 499 | } |
---|
[2551] | 500 | else |
---|
| 501 | { |
---|
[3225] | 502 | /* \todo: implement check whether this particular free entity |
---|
[2551] | 503 | is out of the game area |
---|
[3225] | 504 | \todo: call function to notify the entity that it left |
---|
[2551] | 505 | the game area |
---|
| 506 | */ |
---|
| 507 | } |
---|
| 508 | |
---|
[2816] | 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 | */ |
---|
[3225] | 518 | void World::timeSlice (Uint32 deltaT) |
---|
[2077] | 519 | { |
---|
[2816] | 520 | //List<WorldEntity> *l; |
---|
[2551] | 521 | WorldEntity* entity; |
---|
[3175] | 522 | float seconds = deltaT / 1000.0; |
---|
[2551] | 523 | |
---|
[2816] | 524 | entity = entities->enumerate(); |
---|
| 525 | while( entity != NULL) |
---|
[2551] | 526 | { |
---|
| 527 | entity->tick (seconds); |
---|
[2816] | 528 | entity = entities->nextElement(); |
---|
[2551] | 529 | } |
---|
[2816] | 530 | |
---|
[3209] | 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 | */ |
---|
[3225] | 549 | void World::calcCameraPos (Location* loc, Placement* plc) |
---|
[1858] | 550 | { |
---|
[3233] | 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 | |
---|
[3225] | 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 | { |
---|
[2816] | 573 | //List<WorldEntity> *l; |
---|
[2640] | 574 | WorldEntity* entity; |
---|
| 575 | |
---|
| 576 | printf("counting all entities\n"); |
---|
[2816] | 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"); |
---|
[2816] | 582 | entity = entities->nextElement(); |
---|
[2640] | 583 | } |
---|
| 584 | } |
---|
[2636] | 585 | |
---|
[2640] | 586 | |
---|
[3225] | 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(); |
---|
[3220] | 597 | printf("World::mainLoop() - Entering main loop\n"); |
---|
[3215] | 598 | while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ |
---|
[2551] | 599 | { |
---|
[2636] | 600 | // Network |
---|
| 601 | synchronize(); |
---|
| 602 | // Process input |
---|
[3226] | 603 | handleInput(); |
---|
[3215] | 604 | if( this->bQuitCurrentGame || this->bQuitOrxonox) |
---|
| 605 | { |
---|
| 606 | printf("World::mainLoop() - leaving loop earlier...\n"); |
---|
| 607 | break; |
---|
| 608 | } |
---|
[2636] | 609 | // Process time |
---|
[3225] | 610 | timeSlice(); |
---|
[2636] | 611 | // Process collision |
---|
| 612 | collision(); |
---|
| 613 | // Draw |
---|
| 614 | display(); |
---|
[2816] | 615 | |
---|
[3210] | 616 | for(int i = 0; i < 10000000; i++) {} |
---|
[2551] | 617 | } |
---|
[3215] | 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 |
---|
[3225] | 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 | */ |
---|
[3225] | 636 | void World::handleInput () |
---|
[2636] | 637 | { |
---|
| 638 | // localinput |
---|
[3225] | 639 | CommandNode* cn = Orxonox::getInstance()->getLocalInput(); |
---|
[3216] | 640 | cn->process(); |
---|
[2636] | 641 | // remoteinput |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | /** |
---|
| 645 | \brief advance the timeline |
---|
[3225] | 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 | */ |
---|
[3225] | 651 | void World::timeSlice () |
---|
[2636] | 652 | { |
---|
| 653 | Uint32 currentFrame = SDL_GetTicks(); |
---|
| 654 | if(!this->bPause) |
---|
| 655 | { |
---|
| 656 | Uint32 dt = currentFrame - this->lastFrame; |
---|
[2816] | 657 | |
---|
[2636] | 658 | if(dt > 0) |
---|
| 659 | { |
---|
| 660 | float fps = 1000/dt; |
---|
| 661 | printf("fps = %f\n", fps); |
---|
| 662 | } |
---|
| 663 | else |
---|
| 664 | { |
---|
[3225] | 665 | /* the frame-rate is limited to 100 frames per second, all other things are for |
---|
| 666 | nothing. |
---|
| 667 | */ |
---|
[3194] | 668 | printf("fps = 1000 - frame rate is adjusted\n"); |
---|
| 669 | SDL_Delay(10); |
---|
| 670 | dt = 10; |
---|
[2636] | 671 | } |
---|
[3225] | 672 | this->timeSlice (dt); |
---|
[2636] | 673 | this->update (); |
---|
[3225] | 674 | this->localCamera->timeSlice(dt); |
---|
[2636] | 675 | } |
---|
| 676 | this->lastFrame = currentFrame; |
---|
| 677 | } |
---|
| 678 | |
---|
[3216] | 679 | |
---|
[2636] | 680 | /** |
---|
| 681 | \brief compute collision detection |
---|
| 682 | */ |
---|
| 683 | void World::collision () |
---|
| 684 | { |
---|
| 685 | this->collide (); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | |
---|
| 689 | /** |
---|
[3225] | 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 | |
---|
[3225] | 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 | |
---|
[3225] | 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; |
---|
[2816] | 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 | { |
---|
[3233] | 738 | this->track[loc->part].mapCoords( loc, entity->getPlacement()); |
---|
[2644] | 739 | } |
---|
[3233] | 740 | entity->postSpawn (); |
---|
[2816] | 741 | } |
---|
| 742 | |
---|
| 743 | |
---|
[3225] | 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 | */ |
---|
[2816] | 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 | { |
---|
[3233] | 765 | this->track[loc->part].mapCoords( loc, entity->getPlacement()); |
---|
[2816] | 766 | } |
---|
[3233] | 767 | entity->postSpawn (); |
---|
[2644] | 768 | //return entity; |
---|
| 769 | } |
---|
[2816] | 770 | |
---|
| 771 | |
---|
[3225] | 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 | */ |
---|
[2816] | 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); |
---|
[3233] | 789 | entity->postSpawn (); |
---|
[2816] | 790 | //return entity; |
---|
| 791 | } |
---|
[3216] | 792 | |
---|
| 793 | |
---|
[3225] | 794 | /* |
---|
| 795 | \brief commands that the world must catch |
---|
| 796 | \returns false if not used by the world |
---|
| 797 | */ |
---|
[3216] | 798 | bool World::command(Command* cmd) |
---|
| 799 | { |
---|
| 800 | return false; |
---|
| 801 | } |
---|