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