Changeset 3238 in orxonox.OLD for orxonox/branches/nico/src/world.cc
- Timestamp:
- Dec 20, 2004, 2:42:54 AM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/nico/src/world.cc
r3005 r3238 51 51 World::~World () 52 52 { 53 Orxonox *orx = Orxonox::getInstance(); 54 orx->get_localinput()->unbind (this->localPlayer); 53 printf("World::~World() - deleting current world\n"); 54 CommandNode* cn = Orxonox::getInstance()->getLocalInput(); 55 cn->unbind(this->localPlayer); 56 cn->reset(); 57 this->localCamera->destroy(); 58 59 WorldEntity* entity = entities->enumerate(); 60 while( entity != NULL ) 61 { 62 entity->destroy(); 63 entity = entities->nextElement(); 64 } 65 this->entities->destroy(); 66 55 67 delete this->entities; 56 68 delete this->localCamera; 57 } 58 59 60 /** 61 \brief initialize the world before use. 62 */ 63 Error World::init() 69 /* this->localPlayer hasn't to be deleted explicitly, it is 70 contained in entities*/ 71 } 72 73 74 ErrorMessage World::init() 64 75 { 65 76 this->bPause = false; 66 } 67 68 Error World::start() 69 { 77 CommandNode* cn = Orxonox::getInstance()->getLocalInput(); 78 cn->addToWorld(this); 79 cn->enable(true); 80 } 81 82 ErrorMessage World::start() 83 { 84 printf("World::start() - starting current World: nr %i\n", this->debugWorldNr); 85 this->bQuitOrxonox = false; 86 this->bQuitCurrentGame = false; 70 87 this->mainLoop(); 71 88 } 72 89 73 Error World::stop() 74 { 90 ErrorMessage World::stop() 91 { 92 printf("World::stop() - got stop signal\n"); 75 93 this->bQuitCurrentGame = true; 76 this->localCamera->setWorld(NULL); 77 this->entities->clear(); 78 Orxonox::getInstance()->get_localinput()->reset(); 79 this->~World(); 80 } 81 82 Error World::pause() 94 } 95 96 ErrorMessage World::pause() 83 97 { 84 98 this->isPaused = true; 85 99 } 86 100 87 Error World::resume()101 ErrorMessage World::resume() 88 102 { 89 103 this->isPaused = false; 90 104 } 91 105 106 void World::destroy() 107 { 108 109 } 110 92 111 void World::load() 93 112 { … … 96 115 switch(this->debugWorldNr) 97 116 { 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 */ 98 123 case DEBUG_WORLD_0: 99 124 { … … 114 139 this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); 115 140 } 116 141 // !\todo old track-system has to be removed 142 117 143 // create a player 118 144 WorldEntity* myPlayer = new Player(); … … 122 148 // bind input 123 149 Orxonox *orx = Orxonox::getInstance(); 124 orx->get _localinput()->bind (myPlayer);150 orx->getLocalInput()->bind (myPlayer); 125 151 126 152 // bind camera … … 154 180 this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); 155 181 } 156 182 157 183 // create a player 158 //WorldEntity* myPlayer = (WorldEntity*) this->spawn<Player>();159 184 WorldEntity* myPlayer = new Player(); 160 185 this->spawn(myPlayer); 161 this->localPlayer = myPlayer; 186 this->localPlayer = myPlayer; 162 187 163 188 // bind input 164 189 Orxonox *orx = Orxonox::getInstance(); 165 orx->get _localinput()->bind (myPlayer);190 orx->getLocalInput()->bind (myPlayer); 166 191 167 192 // bind camera … … 185 210 glColor3f(1.0,0,0); 186 211 glBegin(GL_QUADS); 187 float height [500][100]; 188 float size = 2.0; 189 for ( int i = 0; i<=200; i+=1) 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); 219 220 float height [sizeX][sizeY]; 221 Vector normal_vectors[sizeX][sizeY]; 222 223 224 for ( int i = 0; i<sizeX-1; i+=1) 225 for (int j = 0; j<sizeY-1;j+=1) 226 //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; 227 #ifdef __WIN32__ 228 height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; 229 #else 230 height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; 231 #endif 232 233 //Die Hügel ein wenig glätten 234 for (int h=1; h<2;h++) 235 for (int i=1;i<sizeX-2 ;i+=1 ) 236 for(int j=1;j<sizeY-2;j+=1) 237 height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; 238 239 //Berechnung von normalen Vektoren 240 241 for(int i=1;i<sizeX-2;i+=1) 242 for(int j=1;j<sizeY-2 ;j+=1) 190 243 { 191 for (int j = 0; j<=50;j+=1) 192 { 193 height[i][j] = rand()/200321400 + (j-25)*(j-25)/30; 244 Vector v1 = Vector (widthX*(1), widthY*(j) , height[i][j]); 245 Vector v2 = Vector (widthX*(i-1), widthY*(j) , height[i-1][j]); 246 Vector v3 = Vector (widthX*(i), widthY*(j+1), height[i][j+1]); 247 Vector v4 = Vector (widthX*(i+1), widthY*(j), height[i+1][j]); 248 Vector v5 = Vector (widthX*(i), widthY*(j-1), height[i][j-1]); 249 250 Vector c1 = v2 - v1; 251 Vector c2 = v3 - v1; 252 Vector c3= v4 - v1; 253 Vector c4 = v5 - v1; 254 Vector zero = Vector (0,0,0); 255 normal_vectors[i][j]=c1.cross(v4-v2)+c2.cross(v1-v3)+c3.cross(v2-v4)+c4.cross(v3-v1); 256 normal_vectors[i][j].normalize(); 257 } 258 259 int snowheight=3; 260 for ( int i = 0; i<sizeX; i+=1) 261 for (int j = 0; j<sizeY;j+=1) 262 { 263 Vector v1 = Vector (widthX*(i), widthY*(j) -width/2, height[i][j]-20 ); 264 Vector v2 = Vector (widthX*(i+1), widthY*(j) -width/2, height[i+1][j]-20); 265 Vector v3 = Vector (widthX*(i+1), widthY*(j+1)-width/2, height[i+1][j+1]-20); 266 Vector v4 = Vector (widthX*(i), widthY*(j+1)-width/2, height[i][j+1]-20); 267 float a[3]; 268 if(height[i][j]<snowheight){ 269 a[0]=0; 270 a[1]=1.0-height[i][j]/10-.3; 271 a[2]=0; 272 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 273 } 274 else{ 275 a[0]=1.0; 276 a[1]=1.0; 277 a[2]=1.0; 278 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 194 279 195 } 280 } 281 glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); 282 glVertex3f(v1.x, v1.y, v1.z); 283 if(height[i+1][j]<snowheight){ 284 a[0]=0; 285 a[1] =1.0-height[i+1][j]/10-.3; 286 a[2]=0; 287 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 288 } 289 else{ 290 a[0]=1.0; 291 a[1]=1.0; 292 a[2]=1.0; 293 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 294 295 } 296 glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); 297 glVertex3f(v2.x, v2.y, v2.z); 298 if(height[i+1][j+1]<snowheight){ 299 a[0]=0; 300 a[1] =1.0-height[i+1][j+1]/10-.3; 301 a[2]=0; 302 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 303 } 304 else{ 305 a[0]=1.0; 306 a[1]=1.0; 307 a[2]=1.0; 308 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 309 310 311 } 312 glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); 313 glVertex3f(v3.x, v3.y, v3.z); 314 if(height[i][j+1]<snowheight){ 315 a[0]=0; 316 a[1] =1.0-height[i+1][j+1]/10-.3; 317 a[2]=0; 318 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 319 } 320 else{ 321 a[0]=1.0; 322 a[1]=1.0; 323 a[2]=1.0; 324 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 325 } 326 glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); 327 glVertex3f(v4.x, v4.y, v4.z); 328 196 329 } 197 for ( int i = 0; i<=200; i+=1) 198 { 199 for (int j = 0; j<=50;j+=1) 200 { 201 Vector* v1 = new Vector (size*i, size*j-25*size, height[i][j] -20); 202 Vector* v2 = new Vector (size*i+size, size*j-25*size, height[i+1][j]-20); 203 Vector* v3 = new Vector (size*i+size, size*j+size-25*size, height[i+1][j+1]-20); 204 Vector* v4 = new Vector (size*i, size*j+size -25*size, height[i][j+1]-20); 205 206 Vector c1 = *v2 - *v1; 207 Vector c2 = *v3 - *v2; 208 Vector c3 = *v4 - *v3; 209 Vector c4 = *v1 - *v4; 210 211 c1.cross(*v4 - *v1); 212 c2.cross(*v1 - *v2); 213 c3.cross(*v2 - *v3); 214 c4.cross(*v3 - *v4); 215 216 217 glVertex3f(v1->x, v1->y, v1->z); 218 219 glVertex3f(v2->x, v2->y, v2->z); 220 221 glVertex3f(v3->x, v3->y, v3->z); 222 glNormal3f(c4.x, c4.y, c4.z); 223 glVertex3f(v4->x, v4->y, v4->z); 224 225 } 226 } 227 glEnd(); 228 /* 330 glEnd(); 331 /* 229 332 glBegin(GL_LINES); 230 333 for( float x = -128.0; x < 128.0; x += 25.0) … … 370 473 if( !entity->isFree() ) 371 474 { 372 loc = entity->get _location();373 plc = entity->get _placement();475 loc = entity->getLocation(); 476 plc = entity->getPlacement(); 374 477 t = loc->part; 375 478 … … 378 481 { 379 482 printf("An entity is out of the game area\n"); 380 entity->left _world ();483 entity->leftWorld (); 381 484 } 382 485 else 383 486 { 384 while( track[t].map _coords( loc, plc) )487 while( track[t].mapCoords( loc, plc) ) 385 488 { 386 track[t].post _leave (entity);489 track[t].postLeave (entity); 387 490 if( loc->part >= tracklen ) 388 491 { 389 492 printf("An entity has left the game area\n"); 390 entity->left _world ();493 entity->leftWorld (); 391 494 break; 392 495 } 393 track[loc->part].post _enter (entity);496 track[loc->part].postEnter (entity); 394 497 } 395 498 } … … 397 500 else 398 501 { 399 /* TO DO: implement check whether this particular free entity502 /* \todo: implement check whether this particular free entity 400 503 is out of the game area 401 TO DO: call function to notify the entity that it left504 \todo: call function to notify the entity that it left 402 505 the game area 403 506 */ … … 413 516 \param deltaT: the time passed since the last frame in milliseconds 414 517 */ 415 void World::time _slice (Uint32 deltaT)518 void World::timeSlice (Uint32 deltaT) 416 519 { 417 520 //List<WorldEntity> *l; 418 521 WorldEntity* entity; 419 float seconds = deltaT; 420 421 seconds /= 1000; 522 float seconds = deltaT / 1000.0; 422 523 423 524 entity = entities->enumerate(); … … 428 529 } 429 530 430 for( int i = 0; i < tracklen; i++) track[i].tick (seconds);531 //for( int i = 0; i < tracklen; i++) track[i].tick (seconds); 431 532 } 432 533 … … 446 547 Camera Placement 447 548 */ 448 void World::calc _camera_pos (Location* loc, Placement* plc)449 { 450 track[loc->part].map _camera (loc, plc);549 void World::calcCameraPos (Location* loc, Placement* plc) 550 { 551 track[loc->part].mapCamera (loc, plc); 451 552 } 452 553 … … 462 563 } 463 564 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 */ 464 571 void World::debug() 465 572 { … … 478 585 479 586 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 */ 480 594 void World::mainLoop() 481 595 { 482 596 this->lastFrame = SDL_GetTicks(); 483 this->bQuitOrxonox = false; 484 this->bQuitCurrentGame = false; 485 printf("World|Entering main loop\n"); 486 while(!this->bQuitOrxonox && !this->bQuitCurrentGame) /* pause pause pause ?!?!?*/ 487 { 488 //debug routine 489 //debug(); 597 printf("World::mainLoop() - Entering main loop\n"); 598 while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ 599 { 490 600 // Network 491 601 synchronize(); 492 602 // Process input 493 handle_input(); 603 handleInput(); 604 if( this->bQuitCurrentGame || this->bQuitOrxonox) 605 { 606 printf("World::mainLoop() - leaving loop earlier...\n"); 607 break; 608 } 494 609 // Process time 495 time _slice();610 timeSlice(); 496 611 // Process collision 497 612 collision(); … … 499 614 display(); 500 615 501 //for(int i = 0; i < 1000000; i++){} 502 503 } 504 printf("World|Exiting the main loop\n"); 616 for(int i = 0; i < 10000000; i++) {} 617 } 618 printf("World::mainLoop() - Exiting the main loop\n"); 505 619 } 506 620 … … 516 630 /** 517 631 \brief run all input processing 518 */ 519 void World::handle_input () 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. 635 */ 636 void World::handleInput () 520 637 { 521 638 // localinput 522 Orxonox::getInstance()->get_localinput()->process(); 639 CommandNode* cn = Orxonox::getInstance()->getLocalInput(); 640 cn->process(); 523 641 // remoteinput 524 642 } … … 526 644 /** 527 645 \brief advance the timeline 528 */ 529 void World::time_slice () 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. 650 */ 651 void World::timeSlice () 530 652 { 531 653 Uint32 currentFrame = SDL_GetTicks(); … … 541 663 else 542 664 { 543 printf("fps = 1000 but 0ms!\n"); 544 } 545 546 this->time_slice (dt); 665 /* the frame-rate is limited to 100 frames per second, all other things are for 666 nothing. 667 */ 668 printf("fps = 1000 - frame rate is adjusted\n"); 669 SDL_Delay(10); 670 dt = 10; 671 } 672 this->timeSlice (dt); 547 673 this->update (); 548 this->localCamera->time _slice(dt);674 this->localCamera->timeSlice(dt); 549 675 } 550 676 this->lastFrame = currentFrame; 551 677 } 552 678 679 553 680 /** 554 681 \brief compute collision detection … … 559 686 } 560 687 561 /** 562 \brief handle keyboard commands that are not meant for WorldEntities 563 \param cmd: the command to handle 564 \return true if the command was handled by the system or false if it may be passed to the WorldEntities 565 */ 566 bool World::system_command (Command* cmd) 567 { 568 if( !strcmp( cmd->cmd, "quit")) 569 { 570 if( !cmd->bUp) this->bQuitOrxonox = true; 571 return true; 572 } 573 return false; 574 } 575 576 /** 577 \brief render the current frame 688 689 /** 690 \brief render the current frame 691 692 clear all buffers and draw the world 578 693 */ 579 694 void World::display () … … 590 705 } 591 706 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 */ 592 713 Camera* World::getCamera() 593 714 { … … 596 717 597 718 719 /** 720 \brief add and spawn a new entity to this world 721 \param entity to be added 722 */ 598 723 void World::spawn(WorldEntity* entity) 599 724 { … … 611 736 if (entity->bFree) 612 737 { 613 this->track[loc->part].map_coords( loc, entity->get_placement()); 614 } 615 entity->post_spawn (); 616 } 617 618 738 this->track[loc->part].mapCoords( loc, entity->getPlacement()); 739 } 740 entity->postSpawn (); 741 } 742 743 744 /** 745 \brief add and spawn a new entity to this world 746 \param entity to be added 747 \param location where to add 748 */ 619 749 void World::spawn(WorldEntity* entity, Location* loc) 620 750 { … … 633 763 if (entity->bFree) 634 764 { 635 this->track[loc->part].map _coords( loc, entity->get_placement());636 } 637 entity->post _spawn ();765 this->track[loc->part].mapCoords( loc, entity->getPlacement()); 766 } 767 entity->postSpawn (); 638 768 //return entity; 639 769 } 640 770 641 771 772 /** 773 \brief add and spawn a new entity to this world 774 \param entity to be added 775 \param place where to be added 776 */ 642 777 void World::spawn(WorldEntity* entity, Placement* plc) 643 778 { … … 652 787 this->entities->add (entity); 653 788 entity->init (plc, owner); 654 entity->post _spawn ();789 entity->postSpawn (); 655 790 //return entity; 656 791 } 792 793 794 /* 795 \brief commands that the world must catch 796 \returns false if not used by the world 797 */ 798 bool World::command(Command* cmd) 799 { 800 return false; 801 }
Note: See TracChangeset
for help on using the changeset viewer.