Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/parenting/src/world.cc @ 3332

Last change on this file since 3332 was 3328, checked in by bensch, 20 years ago

orxonox/branches/updater: derivation now works too for the UPointCurve

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