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