Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3238 in orxonox.OLD for orxonox/branches/nico/src/world.cc


Ignore:
Timestamp:
Dec 20, 2004, 2:42:54 AM (21 years ago)
Author:
bensch
Message:

orxonox/branches: updated branches: buerli, nico, sound. And moved bezierTrack to old.bezierTrack. Conflicts resolved in a usefull order.
Conflics mostly resolved in favor of trunk
merge.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/nico/src/world.cc

    r3005 r3238  
    5151World::~World ()
    5252{
    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
    5567  delete this->entities;
    5668  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
     74ErrorMessage World::init()
    6475{
    6576  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
     82ErrorMessage World::start()
     83{
     84  printf("World::start() - starting current World: nr %i\n", this->debugWorldNr);
     85  this->bQuitOrxonox = false;
     86  this->bQuitCurrentGame = false;
    7087  this->mainLoop();
    7188}
    7289
    73 Error World::stop()
    74 {
     90ErrorMessage World::stop()
     91{
     92  printf("World::stop() - got stop signal\n");
    7593  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
     96ErrorMessage World::pause()
    8397{
    8498  this->isPaused = true;
    8599}
    86100
    87 Error World::resume()
     101ErrorMessage World::resume()
    88102{
    89103  this->isPaused = false;
    90104}
    91105
     106void World::destroy()
     107{
     108
     109}
     110
    92111void World::load()
    93112{
     
    96115      switch(this->debugWorldNr)
    97116        {
     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           */
    98123        case DEBUG_WORLD_0:
    99124          {
     
    114139                this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]);
    115140              }
    116            
     141            // !\todo old track-system has to be removed
     142
    117143            // create a player
    118144            WorldEntity* myPlayer = new Player();
     
    122148            // bind input
    123149            Orxonox *orx = Orxonox::getInstance();
    124             orx->get_localinput()->bind (myPlayer);
     150            orx->getLocalInput()->bind (myPlayer);
    125151           
    126152            // bind camera
     
    154180                this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]);
    155181              }
    156            
     182
    157183            // create a player
    158             //WorldEntity* myPlayer = (WorldEntity*) this->spawn<Player>();
    159184            WorldEntity* myPlayer = new Player();
    160185            this->spawn(myPlayer);
    161             this->localPlayer = myPlayer;
     186            this->localPlayer = myPlayer;           
    162187           
    163188            // bind input
    164189            Orxonox *orx = Orxonox::getInstance();
    165             orx->get_localinput()->bind (myPlayer);
     190            orx->getLocalInput()->bind (myPlayer);
    166191           
    167192            // bind camera
     
    185210  glColor3f(1.0,0,0);
    186211  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)
    190243      {
    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);
    194279           
    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       
    196329      }
    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  /* 
    229332  glBegin(GL_LINES);
    230333  for( float x = -128.0; x < 128.0; x += 25.0)
     
    370473      if( !entity->isFree() )
    371474        {
    372           loc = entity->get_location();
    373           plc = entity->get_placement();
     475          loc = entity->getLocation();
     476          plc = entity->getPlacement();
    374477          t = loc->part;
    375478         
     
    378481            {
    379482              printf("An entity is out of the game area\n");
    380               entity->left_world ();
     483              entity->leftWorld ();
    381484            }
    382485          else
    383486            {
    384               while( track[t].map_coords( loc, plc) )
     487              while( track[t].mapCoords( loc, plc) )
    385488                {
    386                   track[t].post_leave (entity);
     489                  track[t].postLeave (entity);
    387490                  if( loc->part >= tracklen )
    388491                    {
    389492                      printf("An entity has left the game area\n");
    390                       entity->left_world ();
     493                      entity->leftWorld ();
    391494                      break;
    392495                    }
    393                   track[loc->part].post_enter (entity);
     496                  track[loc->part].postEnter (entity);
    394497                }
    395498            }
     
    397500      else
    398501        {
    399           /* TO DO: implement check whether this particular free entity
     502          /* \todo: implement check whether this particular free entity
    400503             is out of the game area
    401              TO DO: call function to notify the entity that it left
     504             \todo: call function to notify the entity that it left
    402505             the game area
    403506          */
     
    413516    \param deltaT: the time passed since the last frame in milliseconds
    414517*/
    415 void World::time_slice (Uint32 deltaT)
     518void World::timeSlice (Uint32 deltaT)
    416519{
    417520  //List<WorldEntity> *l;
    418521  WorldEntity* entity;
    419   float seconds = deltaT;
    420  
    421   seconds /= 1000;
     522  float seconds = deltaT / 1000.0;
    422523 
    423524  entity = entities->enumerate();
     
    428529    }
    429530
    430   for( int i = 0; i < tracklen; i++) track[i].tick (seconds);
     531  //for( int i = 0; i < tracklen; i++) track[i].tick (seconds);
    431532}
    432533
     
    446547   Camera Placement
    447548*/
    448 void World::calc_camera_pos (Location* loc, Placement* plc)
    449 {
    450   track[loc->part].map_camera (loc, plc);
     549void World::calcCameraPos (Location* loc, Placement* plc)
     550{
     551  track[loc->part].mapCamera (loc, plc);
    451552}
    452553
     
    462563}
    463564
     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*/
    464571void World::debug()
    465572{
     
    478585
    479586
     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*/
    480594void World::mainLoop()
    481595{
    482596  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    {
    490600      // Network
    491601      synchronize();
    492602      // 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        }
    494609      // Process time
    495       time_slice();
     610      timeSlice();
    496611      // Process collision
    497612      collision();
     
    499614      display();
    500615 
    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");
    505619}
    506620
     
    516630/**
    517631   \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*/
     636void World::handleInput ()
    520637{
    521638  // localinput
    522   Orxonox::getInstance()->get_localinput()->process();
     639  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
     640  cn->process();
    523641  // remoteinput
    524642}
     
    526644/**
    527645   \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*/
     651void World::timeSlice ()
    530652{
    531653  Uint32 currentFrame = SDL_GetTicks();
     
    541663      else
    542664        {
    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);
    547673      this->update ();
    548       this->localCamera->time_slice (dt);
     674      this->localCamera->timeSlice(dt);
    549675    }
    550676  this->lastFrame = currentFrame;
    551677}
    552678
     679
    553680/**
    554681   \brief compute collision detection
     
    559686}
    560687
    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
    578693*/
    579694void World::display ()
     
    590705}
    591706
     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*/
    592713Camera* World::getCamera()
    593714{
     
    596717
    597718
     719/**
     720   \brief add and spawn a new entity to this world
     721   \param entity to be added
     722*/
    598723void World::spawn(WorldEntity* entity)
    599724{
     
    611736  if (entity->bFree)
    612737    {
    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*/
    619749void World::spawn(WorldEntity* entity, Location* loc)
    620750{
     
    633763  if (entity->bFree)
    634764    {
    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 ();
    638768  //return entity;
    639769}
    640770
    641771
     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*/
    642777void World::spawn(WorldEntity* entity, Placement* plc)
    643778{
     
    652787  this->entities->add (entity);
    653788  entity->init (plc, owner);
    654   entity->post_spawn ();
     789  entity->postSpawn ();
    655790  //return entity;
    656791}
     792
     793
     794/*
     795  \brief commands that the world must catch
     796  \returns false if not used by the world
     797*/
     798bool World::command(Command* cmd)
     799{
     800  return false;
     801}
Note: See TracChangeset for help on using the changeset viewer.