Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/openAL/src/story_entities/world.cc @ 4197

Last change on this file since 4197 was 4197, checked in by bensch, 19 years ago

orxonox/branches/openAL: playing some sound.

File size: 32.0 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: Christian Meyer
16*/
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
19
20#include "world.h"
21
22#include "orxonox.h"
23
24#include "p_node.h"
25#include "null_parent.h"
26#include "helper_parent.h"
27#include "track_node.h"
28#include "world_entity.h"
29#include "player.h"
30#include "camera.h"
31#include "environment.h"
32#include "skysphere.h"
33#include "skybox.h"
34#include "satellite.h"
35#include "terrain.h"
36#include "light.h"
37#include "text_engine.h"
38
39#include "track_manager.h"
40#include "garbage_collector.h"
41#include "animation_player.h"
42#include "particle_engine.h"
43
44#include "command_node.h"
45#include "glmenu_imagescreen.h"
46#include "list.h"
47#include "game_loader.h"
48
49#include "animation3d.h"
50
51#include "substring.h"
52
53#include "sound_engine.h"
54
55using namespace std;
56
57WorldInterface* WorldInterface::singletonRef = 0;
58
59
60/**
61   \brief private constructor because of singleton
62*/
63WorldInterface::WorldInterface()
64{
65  this->worldIsInitialized = false;
66  this->worldReference = NULL;
67}
68
69/**
70   \brief public deconstructor
71*/
72WorldInterface::~WorldInterface()
73{
74  this->singletonRef = NULL;
75  this->worldIsInitialized = false;
76  this->worldReference = NULL;
77}
78
79/**
80   \brief gets the singleton instance
81   \returns singleton instance
82*/
83WorldInterface* WorldInterface::getInstance()
84{
85  if( singletonRef == NULL)
86    singletonRef = new WorldInterface();
87  return singletonRef;
88}
89
90
91/**
92   \brief initializes the interface
93   \param reference to the world
94
95   if the worldinterface is not initilizes, there wont be any
96   useable interface
97*/
98void WorldInterface::init(World* world)
99{
100  this->worldReference = world;
101  if( world != NULL)
102    {
103      this->worldIsInitialized = true;
104      PRINTF(3)("WorldInterface up and running\n");
105    }
106}
107
108
109/**
110   \brief gets the entity list from the world
111   \return entity list
112*/
113tList<WorldEntity>* WorldInterface::getEntityList()
114{
115  if( this->worldIsInitialized)
116    return this->worldReference->getEntities();
117  PRINT(1)("Someone tried to use the WorldInterface before it has been initizlized! this can result in SEGFAULTs!\n");
118  return NULL;
119}
120
121CREATE_FACTORY(World);
122
123World::World( TiXmlElement* root)
124{
125  this->constuctorInit("", -1);
126  this->path = NULL;
127  const char *string;
128  char *name;
129  int id;
130 
131  PRINTF0("Creating a World\n");
132 
133  // identifier
134  string = grabParameter( root, "identifier");
135  if( string == NULL || sscanf(string, "%d", &id) != 1)
136    {
137      PRINTF0("World is missing a proper 'identifier'\n");
138      this->setStoryID( -1);
139    }
140  else setStoryID( id);
141
142  // next id
143  string = grabParameter( root, "nextid");
144  if( string == NULL || sscanf(string, "%d", &id) != 1)
145    {
146      PRINTF0("World is missing a proper 'nextid'\n");
147      this->setStoryID( -1);
148    }
149  else setNextStoryID( id);
150 
151
152  // path
153  string = grabParameter( root, "path");
154  if( string == NULL)
155    {
156      PRINTF0("World is missing a proper 'path'\n");
157      this->setPath( NULL);
158    }
159  else
160    {
161      name = new char[strlen(string + 2)];
162      strcpy( name, string);
163      this->setPath( name);
164    }
165}
166
167/**
168    \brief create a new World
169   
170    This creates a new empty world!
171*/
172World::World (char* name)
173{
174  this->path = NULL;
175  this->constuctorInit(name, -1);
176  //NullParent* np = NullParent::getInstance();
177}
178
179/**
180   \brief creates a new World...
181   \param worldID with this ID
182*/
183World::World (int worldID)
184{
185  this->path = NULL;
186  this->constuctorInit(NULL, worldID);
187}
188
189/**
190    \brief remove the World from memory
191   
192    delete everything explicitly, that isn't contained in the parenting tree!
193    things contained in the tree are deleted automaticaly
194*/
195World::~World ()
196{
197  PRINTF(3)("World::~World() - deleting current world\n");
198  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
199  cn->unbind(this->localPlayer);
200  cn->reset();
201
202  delete WorldInterface::getInstance();
203
204  delete this->nullParent;
205  delete this->entities;
206  delete this->lightMan;
207  delete this->trackManager;
208  delete this->particleEngine;
209  TextEngine::getInstance()->flush();
210
211  delete AnimationPlayer::getInstance(); // this should be at the end of the unloading sequence.
212  //delete garbagecollecor
213  //delete animator
214
215  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
216}
217
218/**
219   \brief initializes the world.
220
221   set all stuff here that is world generic and does not use to much memory
222   because the real init() function StoryEntity::init() will be called
223   shortly before start of the game. 
224   since all worlds are initiated/referenced before they will be started.
225   NO LEVEL LOADING HERE - NEVER!
226*/
227void World::constuctorInit(char* name, int worldID)
228{
229  this->setClassName ("World");
230
231  //this->worldName = name;
232  //this->worldName = new char[strlen(name)+1];
233  //strcpy(this->worldName, name);
234  this->debugWorldNr = worldID;
235  this->entities = new tList<WorldEntity>();
236}
237
238
239/**
240   \brief this is executed before load
241
242   since the load function sometimes needs data, that has been init before
243   the load and after the proceeding storyentity has finished
244*/
245ErrorMessage World::preLoad()
246{
247  /* init the world interface */
248  WorldInterface* wi = WorldInterface::getInstance();
249  wi->init(this);
250  this->garbageCollector = GarbageCollector::getInstance();
251
252  this->particleEngine = ParticleEngine::getInstance();
253  this->trackManager = TrackManager::getInstance();
254  this->lightMan = LightManager::getInstance();
255  this->nullParent = NullParent::getInstance ();
256  this->nullParent->setName ("NullParent");
257
258  AnimationPlayer::getInstance(); // initializes the animationPlayer
259
260  this->localCamera = new Camera();
261  this->localCamera->setName ("camera");
262}
263
264
265/**
266   \brief loads the World by initializing all resources, and set their default values.
267*/
268ErrorMessage World::load()
269{       
270  PRINTF(3)("> Loading world: '%s'\n", getPath());
271  TiXmlElement* element;
272  GameLoader* loader = GameLoader::getInstance();
273 
274  if( getPath() == NULL)
275    {
276      PRINTF(1)("World has no path specified for loading");
277      return (ErrorMessage){213,"Path not specified","World::load()"};
278    }
279 
280  TiXmlDocument* XMLDoc = new TiXmlDocument( path);
281  // load the campaign document
282  if( !XMLDoc->LoadFile()) 
283  {
284    // report an error
285    PRINTF(1)("loading XML File: %s @ %d:%d\n", XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
286    delete XMLDoc;
287    return (ErrorMessage){213,"XML File parsing error","World::load()"};
288  }
289 
290  // check basic validity
291  TiXmlElement* root = XMLDoc->RootElement();
292  assert( root != NULL);
293 
294  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
295    {
296      // report an error
297      PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
298      delete XMLDoc;
299      return (ErrorMessage){213,"Path not a WorldDataFile","World::load()"};
300    }
301 
302  // load the parameters
303  // name
304  char* temp;
305  const char* string = grabParameter( root, "name");
306  if( string == NULL)
307    {
308      PRINTF(2)("World is missing a proper 'name'\n");
309      string = "Unknown";
310      temp = new char[strlen(string + 2)];
311      strcpy( temp, string);
312      this->worldName = temp;
313    }
314  else
315    {
316      temp = new char[strlen(string + 2)];
317      strcpy( temp, string);
318      this->worldName = temp;
319    }
320  ////////////////
321  // LOADSCREEN //
322  ////////////////
323  element = root->FirstChildElement("LoadScreen");
324  if (element == NULL)
325    {
326      PRINTF(2)("no LoadScreen specified, loading default\n");
327
328      glmis->setBackgroundImage("pictures/load_screen.jpg");
329      this->glmis->setMaximum(8);
330      this->glmis->draw();
331    }
332  else
333    {
334      this->glmis->load(element);
335      this->glmis->draw();
336    }
337  this->glmis->draw();
338  // find WorldEntities
339  element = root->FirstChildElement("WorldEntities");
340 
341  if( element == NULL)
342    {
343      PRINTF(1)("World is missing 'WorldEntities'\n");
344    }
345  else
346    {
347      element = element->FirstChildElement();
348      // load Players/Objects/Whatever
349      PRINTF(4)("Loading WorldEntities\n");
350      while( element != NULL)
351        {
352          WorldEntity* created = (WorldEntity*) loader->fabricate( element);
353          if( created != NULL) this->spawn( created);
354          // if we load a 'Player' we use it as localPlayer
355          //todo do this more elegant
356          if( element->Value() != NULL && !strcmp( element->Value(), "Player")) localPlayer = (Player*) created;
357          if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) sky = (SkyBox*) created;
358          element = element->NextSiblingElement();
359          glmis->step(); //! \todo temporary
360        }
361      PRINTF(4)("Done loading WorldEntities\n");
362    }
363 
364  // find Track
365  /*element = root->FirstChildElement( "Track");
366  if( element == NULL)
367    {
368      PRINTF0("============>>>>>>>>>>>>>>>>>World is missing a 'Track'\n");
369    }
370  else
371    {   
372      //load track
373      PRINTF0("============>>>>>>>>>>>>>>>>Loading Track\n");
374
375      trackManager->loadTrack( element);
376      trackManager->finalize();
377      PRINTF0("============>>>>>>>>>>>>>>>>Done loading Track\n");
378    }*/
379 
380  // free the XML data
381
382  delete XMLDoc;
383  /* GENERIC LOADING PROCESS FINISHED */
384 
385  // bind input
386  Orxonox *orx = Orxonox::getInstance ();
387  orx->getLocalInput()->bind (localPlayer);
388 
389  // bind camera
390  //this->localCamera->bind (localPlayer);
391  this->localPlayer->addChild (this->localCamera);
392 
393 
394  // stuff beyond this point remains to be loaded properly
395 
396      // initializing the TrackManager
397  this->trackManager = TrackManager::getInstance();
398      //trackManager->addPoint(Vector(0,0,0));
399      trackManager->addPoint(Vector(150, -35, 5));
400      trackManager->addPoint(Vector(200,-35, 5));
401      trackManager->addPoint(Vector(250, -35, 5));
402      trackManager->addPoint(Vector(320,-33,-.55));
403      trackManager->setDuration(2);
404      trackManager->setSavePoint();
405
406      trackManager->addPoint(Vector(410, 0, 0));
407      trackManager->addPoint(Vector(510, 20, -10));
408      trackManager->addPoint(Vector(550, 20, -10));
409      trackManager->addPoint(Vector(570, 20, -10));
410      trackManager->setDuration(5);
411     
412      int fork11, fork12;
413      trackManager->fork(2, &fork11, &fork12);
414      trackManager->workOn(fork11);
415      trackManager->addPoint(Vector(640, 25, -30));
416      trackManager->addPoint(Vector(700, 40, -120));
417      trackManager->addPoint(Vector(800, 50, -150));
418      trackManager->addPoint(Vector(900, 60, -100));
419      trackManager->addPoint(Vector(900, 60, -70));
420      trackManager->addPoint(Vector(990, 65, -15));
421      trackManager->addPoint(Vector(1050, 65, -10));
422      trackManager->addPoint(Vector(1100, 65, -20));
423      trackManager->setDuration(10);
424
425      trackManager->workOn(fork12);
426      trackManager->addPoint(Vector(640, 25, 20));
427      trackManager->addPoint(Vector(670, 50, 120));
428      trackManager->addPoint(Vector(700, 70, 80));
429      trackManager->addPoint(Vector(800, 70, 65));
430      trackManager->addPoint(Vector(850, 65, 65));
431      trackManager->addPoint(Vector(920, 35, 40));
432      trackManager->addPoint(Vector(945, 40, 40));
433      trackManager->addPoint(Vector(970, 24, 40));
434      trackManager->addPoint(Vector(1000, 40, -7));
435      trackManager->setDuration(10);
436     
437
438      trackManager->join(2, fork11, fork12);
439
440      trackManager->workOn(5);
441      trackManager->addPoint(Vector(1200, 60, -50));
442      trackManager->addPoint(Vector(1300, 50, -50));
443      trackManager->addPoint(Vector(1400, 40, -50));
444      trackManager->addPoint(Vector(1500, 40, -60));
445      trackManager->addPoint(Vector(1600, 35, -55));
446      trackManager->addPoint(Vector(1700, 45, -40));
447      trackManager->addPoint(Vector(1750, 60, -40));
448      trackManager->addPoint(Vector(1770, 80, -40));
449      trackManager->addPoint(Vector(1800, 100, -40));
450      trackManager->setDuration(10);
451
452      trackManager->finalize();
453
454     
455  lightMan->setAmbientColor(.1,.1,.1);
456  lightMan->addLight();
457  //      lightMan->setAttenuation(1.0, .01, 0.0);
458  //      lightMan->setDiffuseColor(1,1,1);
459  //  lightMan->addLight(1);
460  //  lightMan->setPosition(20, 10, -20);
461  //  lightMan->setDiffuseColor(0,0,0);
462  lightMan->debug();
463  lightMan->setPosition(-5.0, 10.0, -40.0);
464 
465  //        trackManager->setBindSlave(env);
466  PNode* tn = trackManager->getTrackNode();
467  tn->addChild(this->localPlayer);
468 
469  //localCamera->setParent(TrackNode::getInstance());
470  tn->addChild(this->localCamera);
471  localCamera->lookAt(tn);
472  localCamera->setMode(PNODE_MOVEMENT);
473  this->localPlayer->setMode(PNODE_ALL);
474  Vector* cameraOffset = new Vector (0, 5, -10);
475  trackManager->condition(2, LEFTRIGHT, this->localPlayer);
476 
477  this->sky->setParent(this->localCamera);
478
479  // initialize debug coord system
480  objectList = glGenLists(1);
481  glNewList (objectList, GL_COMPILE);
482 
483  //  trackManager->drawGraph(.01);
484  trackManager->debug(2);
485  glEndList();
486
487  terrain = new Terrain("worlds/newGround.obj");
488  terrain->setRelCoor(Vector(0,-10,0));
489  this->spawn(terrain);
490
491  SoundEngine::getInstance()->setListener(tn);
492  SoundBuffer* tBuffer =new SoundBuffer("explo.wav");
493  SoundSource* tSound = new SoundSource(tBuffer, this->localPlayer);
494  tSound->play();
495
496
497  ParticleSystem* system = new ParticleSystem(1000, PARTICLE_SPRITE);
498  system->setLifeSpan(.5);
499  system->setConserve(.99);
500  system->setRadius(2, 0, 2, 0);
501
502  ParticleEmitter* emitter = new ParticleEmitter(Vector(-1, 0, 0), M_PI_4, 100, .05);
503  emitter->setParent(this->localPlayer);
504 
505  particleEngine->addConnection(emitter, system);
506}
507
508void World::loadDebugWorld(int worldID)
509{
510  /*monitor progress*/
511  this->glmis->step();
512
513  // LIGHT initialisation
514
515  lightMan->setAmbientColor(.1,.1,.1);
516  lightMan->addLight();
517  //      lightMan->setAttenuation(1.0, .01, 0.0);
518  //      lightMan->setDiffuseColor(1,1,1);
519  //  lightMan->addLight(1);
520  //  lightMan->setPosition(20, 10, -20);
521  //  lightMan->setDiffuseColor(0,0,0);
522  lightMan->debug();
523
524  switch(this->debugWorldNr)
525    {
526      /*
527        this loads the hard-coded debug world. this only for simplicity and will be
528        removed by a reald world-loader, which interprets a world-file.
529        if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and
530        make whatever you want...
531      */
532    case DEBUG_WORLD_0:
533      {
534        lightMan->setPosition(-5.0, 10.0, -40.0);
535
536        // !\todo old track-system has to be removed
537
538        //create helper for player
539        //HelperParent* hp = new HelperParent ();
540        /* the player has to be added to this helper */
541
542        // create a player
543        this->localPlayer = new Player ();
544        this->localPlayer->setName ("player");
545        this->spawn (this->localPlayer);
546        /*monitor progress*/
547        //this->glmis->step();
548        this->glmis->step();
549
550        // bind input
551        Orxonox *orx = Orxonox::getInstance ();
552        orx->getLocalInput()->bind (this->localPlayer);
553           
554        // bind camera
555        this->localCamera = new Camera();
556        this->localCamera->setName ("camera");
557           
558        /*monitor progress*/
559        this->glmis->step();
560
561        sky = new SkyBox();
562        //      (SkyBox*)(sky)->setTexture("pictures/sky/skybox", "jpg");
563        sky->setParent(localCamera);
564        this->spawn(sky);
565
566        /*monitor progress*/
567        this->glmis->step();
568
569           
570        WorldEntity* env = new Environment();
571        env->setName ("env");
572        this->spawn(env);
573
574           
575        /*
576          Vector* es = new Vector (10, 5, 0);
577          Quaternion* qs = new Quaternion ();
578          WorldEntity* pr = new Primitive(P_CYLINDER);
579          pr->setName("primitive");
580          this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT);
581        */
582
583        /*monitor progress*/
584        this->glmis->step();
585
586        //          trackManager->setBindSlave(env);
587        PNode* tn = trackManager->getTrackNode();
588        tn->addChild(this->localPlayer);
589        this->localCamera->lookAt(tn);
590
591        //localCamera->setParent(TrackNode::getInstance());
592        tn->addChild(this->localCamera);
593        //          localCamera->lookAt(tn);
594        this->localPlayer->setMode(PNODE_ALL);
595        //Vector* cameraOffset = new Vector (0, 5, -10);
596        trackManager->condition(2, LEFTRIGHT, this->localPlayer);
597        this->glmis->step();
598        break;
599      }
600    case DEBUG_WORLD_1:
601      {
602        lightMan->setPosition(.0, .0, .0);
603        lightMan->setAttenuation(1.0, .01, 0.0);
604        lightMan->setSpecularColor(1,0,0);
605        this->nullParent = NullParent::getInstance ();
606        this->nullParent->setName ("NullParent");
607
608        // create a player
609        WorldEntity* myPlayer = new Player();
610        myPlayer->setName ("player");
611        this->spawn(myPlayer);
612        this->localPlayer = myPlayer;       
613           
614        // bind input
615        Orxonox *orx = Orxonox::getInstance();
616        orx->getLocalInput()->bind (myPlayer);
617           
618        // bind camera
619        this->localCamera = new Camera ();
620        this->localCamera->setName ("camera");
621        this->localCamera->lookAt(LightManager::getInstance()->getLight(0));
622        this->localCamera->setParent(this->localPlayer);
623
624        // Create SkySphere
625        sky = new Skysphere("pictures/sky-replace.jpg");
626        this->localPlayer->addChild(this->sky);
627        this->spawn(this->sky);
628        Vector* es = new Vector (20, 0, 0);
629        Quaternion* qs = new Quaternion ();
630
631        lightMan->getLight(0)->setParent(trackManager->getTrackNode());
632        break;
633      }
634    case DEBUG_WORLD_2:
635      {
636        lightMan->setAmbientColor(.1,.1,.1);
637        lightMan->addLight();
638        lightMan->setPosition(-5.0, 10.0, -40.0);
639        this->nullParent = NullParent::getInstance ();
640        this->nullParent->setName ("NullParent");
641
642        // !\todo old track-system has to be removed
643
644        //create helper for player
645        //HelperParent* hp = new HelperParent ();
646        /* the player has to be added to this helper */
647
648        // create a player
649        this->localPlayer = new Player ();
650        this->localPlayer->setName ("player");
651        this->spawn (this->localPlayer);
652        /*monitor progress*/
653        //this->glmis->step();     
654        this->glmis->step();
655
656        // bind input
657        Orxonox *orx = Orxonox::getInstance ();
658        orx->getLocalInput()->bind (this->localPlayer);
659           
660        // bind camera
661        this->localCamera = new Camera();
662        this->localCamera->setName ("camera");
663        this->localCamera->lookAt(this->localPlayer);
664        this->localCamera->setParent(this->localPlayer);
665           
666        /*monitor progress*/
667        this->glmis->step();
668
669        // Create SkySphere
670        this->sky = new Skysphere("pictures/sky-replace.jpg");
671        this->sky->setName("SkySphere");
672        this->spawn(this->sky);
673        this->localCamera->addChild(this->sky);
674        this->sky->setMode(PNODE_MOVEMENT);
675        /*monitor progress*/
676        this->glmis->step();
677
678
679        WorldEntity* baseNode = new Satellite(Vector(1,0,1), 1.2);
680        this->localPlayer->addChild(baseNode);
681        baseNode->setRelCoor(Vector(10.0, 2.0, 1.0));
682        this->spawn(baseNode);
683
684        WorldEntity* secondNode = new Satellite(Vector(0,0,1), 2.0);
685        baseNode->addChild(secondNode);
686        secondNode->setRelCoor(Vector(0.0, 0.0, 3.0));
687        this->spawn(secondNode);
688
689
690        WorldEntity* thirdNode = new Satellite(Vector(0,0,1), 1.0);
691        secondNode->addChild(thirdNode);
692        thirdNode->setRelCoor(Vector(2.0, 0.0, 0.0));
693        this->spawn(thirdNode);
694
695           
696   
697        WorldEntity* c = new Environment();
698        this->localPlayer->addChild(c);
699        c->setRelCoor(Vector(10.0, 2.0, -1.0));
700        this->spawn(c);
701
702
703           
704        Animation3D* animation = new Animation3D(c);
705        animation->setInfinity(ANIM_INF_REPLAY);
706
707
708        animation->addKeyFrame(Vector(0, 0, 0), Quaternion(0, Vector(0,1,0)), 1.0, ANIM_NEG_EXP, ANIM_LINEAR); 
709        animation->addKeyFrame(Vector(0, 2, 0), Quaternion(M_PI, Vector(0,1,0)), 1.0, ANIM_NEG_EXP, ANIM_LINEAR); 
710        animation->addKeyFrame(Vector(0, 0, 0), Quaternion(0, Vector(0,1,0)), 1.0, ANIM_NEG_EXP, ANIM_LINEAR); 
711
712
713
714
715
716
717        /*         
718          KeyFrame* f1 = new KeyFrame;
719          f1->position = new Vector(-1.1, 0.0, 2.6);
720          f1->direction = new Quaternion();
721          f1->time = 1.0;
722          f1->mode = NEG_EXP;
723                 
724                 
725          KeyFrame* f2 = new KeyFrame;
726          f2->position = new Vector(-2.1, 0.0, 2.6);
727          f2->direction = new Quaternion();
728          f2->time = 0.1;
729          f2->mode = NEG_EXP;
730                 
731          KeyFrame* f3 = new KeyFrame;
732          f3->position = new Vector(10.0, 2.0, -1.0);
733          f3->direction = new Quaternion();
734          f3->time = 0.2;
735          f3->mode = NEG_EXP;
736                 
737          KeyFrame* f4 = new KeyFrame;
738          f4->position = new Vector(10.0, 5.0, -1.0);
739          f4->direction = new Quaternion();
740          f4->time = 1.0;
741          f4->mode = NEG_EXP;
742                 
743                 
744                 
745          this->simpleAnimation->animatorBegin();
746          this->simpleAnimation->selectObject(b);
747          this->simpleAnimation->setAnimationMode(SINGLE);
748          this->simpleAnimation->addKeyFrame(f1);
749          this->simpleAnimation->addKeyFrame(f2);
750          this->simpleAnimation->start();
751          this->simpleAnimation->selectObject(c);
752          this->simpleAnimation->addKeyFrame(f3);
753          this->simpleAnimation->addKeyFrame(f4);
754          this->simpleAnimation->start();
755          this->simpleAnimation->animatorEnd();
756        */
757
758        /*
759          Vector* es = new Vector (10, 5, 0);
760          Quaternion* qs = new Quaternion ();
761          WorldEntity* pr = new Primitive(P_CYLINDER);
762          pr->setName("primitive");
763          this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT);
764        */
765
766        /*monitor progress*/
767        this->glmis->step();
768
769        //          trackManager->setBindSlave(env);
770        PNode* tn = trackManager->getTrackNode();
771        tn->addChild(this->localPlayer);
772
773        //localCamera->setParent(TrackNode::getInstance());
774        tn->addChild(this->localCamera);
775        //          localCamera->lookAt(tn);
776        this->localPlayer->setMode(PNODE_ALL);
777        //Vector* cameraOffset = new Vector (0, 5, -10);
778        trackManager->condition(2, LEFTRIGHT, this->localPlayer);
779        this->glmis->step();
780
781        break;
782      }
783    default:
784      printf("World::load() - no world with ID %i found", this->debugWorldNr );
785    }
786}
787
788
789
790/**
791   \brief initializes a new World shortly before start
792
793   this is the function, that will be loaded shortly before the world is
794   started
795*/
796ErrorMessage World::init()
797{
798  this->bPause = false;
799  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
800  cn->addToWorld(this);
801  cn->enable(true);
802}
803
804
805/**
806   \brief starts the World
807*/
808ErrorMessage World::start()
809{
810  PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr);
811  this->bQuitOrxonox = false;
812  this->bQuitCurrentGame = false;
813  this->mainLoop();
814}
815
816/**
817   \brief stops the world.
818
819   This happens, when the player decides to end the Level.
820*/
821ErrorMessage World::stop()
822{
823  PRINTF(3)("World::stop() - got stop signal\n");
824  this->bQuitCurrentGame = true;
825}
826
827/**
828   \brief pauses the Game
829*/
830ErrorMessage World::pause()
831{
832  this->isPaused = true;
833}
834
835/**
836   \brief ends the pause Phase
837*/
838ErrorMessage World::resume()
839{
840  this->isPaused = false;
841}
842
843/**
844   \brief destroys the World
845*/
846ErrorMessage World::destroy()
847{
848
849}
850
851/**
852   \brief shows the loading screen
853*/
854void World::displayLoadScreen ()
855{
856  PRINTF(3)("World::displayLoadScreen - start\n"); 
857 
858  //GLMenuImageScreen*
859  this->glmis = new GLMenuImageScreen();
860  this->glmis->init();
861  this->glmis->setMaximum(8);
862  //  this->glmis->draw();
863 
864  PRINTF(3)("World::displayLoadScreen - end\n"); 
865}
866
867/**
868   \brief removes the loadscreen, and changes over to the game
869
870   \todo take out the delay
871*/
872void World::releaseLoadScreen ()
873{
874  PRINTF(3)("World::releaseLoadScreen - start\n"); 
875  this->glmis->setValue(this->glmis->getMaximum());
876  PRINTF(3)("World::releaseLoadScreen - end\n"); 
877  delete this->glmis;
878}
879
880
881/**
882   \brief gets the list of entities from the world
883   \returns entity list
884*/
885tList<WorldEntity>* World::getEntities()
886{
887  return this->entities;
888}
889
890
891/**
892   \brief this returns the current game time
893   \returns elapsed game time
894*/
895double World::getGameTime()
896{
897  return this->gameTime;
898}
899
900
901/**
902    \brief checks for collisions
903   
904    This method runs through all WorldEntities known to the world and checks for collisions
905    between them. In case of collisions the collide() method of the corresponding entities
906    is called.
907*/
908void World::collide ()
909{
910  /*
911  List *a, *b;
912  WorldEntity *aobj, *bobj;
913   
914  a = entities;
915 
916  while( a != NULL)
917    {
918      aobj = a->nextElement();
919      if( aobj->bCollide && aobj->collisioncluster != NULL)
920        {
921          b = a->nextElement();
922          while( b != NULL )
923            {
924              bobj = b->nextElement();
925              if( bobj->bCollide && bobj->collisioncluster != NULL )
926                {
927                  unsigned long ahitflg, bhitflg;
928                  if( check_collision ( &aobj->place, aobj->collisioncluster,
929                                        &ahitflg, &bobj->place, bobj->collisioncluster,
930                                        &bhitflg) );
931                  {
932                    aobj->collide (bobj, ahitflg, bhitflg);
933                    bobj->collide (aobj, bhitflg, ahitflg);
934                  }
935                }
936              b = b->nextElement();
937            }
938        }
939      a = a->enumerate();
940    }
941  */
942}
943
944/**
945    \brief runs through all entities calling their draw() methods
946*/
947void World::draw ()
948{
949  /* draw entities */
950  WorldEntity* entity;
951  glLoadIdentity();
952
953  //entity = this->entities->enumerate();
954  tIterator<WorldEntity>* iterator = this->entities->getIterator();
955  entity = iterator->nextElement();
956  while( entity != NULL ) 
957    { 
958      if( entity->bDraw ) entity->draw();
959      //entity = this->entities->nextElement();
960      entity = iterator->nextElement();
961    }
962  delete iterator;
963 
964  glCallList (objectList);
965
966  TextEngine::getInstance()->draw();
967  particleEngine->draw(this->dtS); //!< \todo should be dts like in the Trunk;
968
969  lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes //
970}
971
972
973/**
974   \brief function to put your own debug stuff into it. it can display informations about
975   the current class/procedure
976*/
977void World::debug()
978{
979  PRINTF(2)("debug() - starting debug\n");
980  PNode* p1 = NullParent::getInstance ();
981  PNode* p2 = new PNode (Vector(2, 2, 2), p1);
982  PNode* p3 = new PNode (Vector(4, 4, 4), p1);
983  PNode* p4 = new PNode (Vector(6, 6, 6), p2);
984
985  p1->debug ();
986  p2->debug ();
987  p3->debug ();
988  p4->debug ();
989
990  p1->shiftCoor (Vector(-1, -1, -1));
991
992  printf("World::debug() - shift\n");
993  p1->debug ();
994  p2->debug ();
995  p3->debug ();
996  p4->debug ();
997 
998  p1->update (0);
999
1000  printf ("World::debug() - update\n");
1001  p1->debug ();
1002  p2->debug ();
1003  p3->debug ();
1004  p4->debug ();
1005
1006  p2->shiftCoor (Vector(-1, -1, -1));
1007  p1->update (0);
1008
1009  p1->debug ();
1010  p2->debug ();
1011  p3->debug ();
1012  p4->debug ();
1013
1014  p2->setAbsCoor (Vector(1,2,3));
1015
1016
1017 p1->update (0);
1018
1019  p1->debug ();
1020  p2->debug ();
1021  p3->debug ();
1022  p4->debug ();
1023
1024  delete p1;
1025 
1026 
1027  /*
1028  WorldEntity* entity;
1029  printf("counting all entities\n");
1030  printf("World::debug() - enumerate()\n");
1031  entity = entities->enumerate(); 
1032  while( entity != NULL )
1033    {
1034      if( entity->bDraw ) printf("got an entity\n");
1035      entity = entities->nextElement();
1036    }
1037  */
1038}
1039
1040
1041/**
1042  \brief main loop of the world: executing all world relevant function
1043
1044  in this loop we synchronize (if networked), handle input events, give the heart-beat to
1045  all other member-entities of the world (tick to player, enemies etc.), checking for
1046  collisions drawing everything to the screen.
1047*/
1048void World::mainLoop()
1049{
1050  this->lastFrame = SDL_GetTicks ();
1051  PRINTF(3)("World::mainLoop() - Entering main loop\n");
1052  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
1053    {
1054      PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize());
1055      // Network
1056      this->synchronize ();
1057      // Process input
1058      this->handleInput ();
1059      if( this->bQuitCurrentGame || this->bQuitOrxonox)
1060          break;
1061      // Process time
1062      this->tick ();
1063      // Update the state
1064      this->update ();     
1065      // Process collision
1066      this->collide ();
1067      // Draw
1068      this->display ();
1069
1070      //      for( int i = 0; i < 5000000; i++) {}
1071      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
1072    }
1073  PRINTF(3)("World::mainLoop() - Exiting the main loop\n");
1074}
1075
1076
1077/**
1078   \brief synchronize local data with remote data
1079*/
1080void World::synchronize ()
1081{
1082  // Get remote input
1083  // Update synchronizables
1084}
1085
1086
1087/**
1088   \brief run all input processing
1089
1090   the command node is the central input event dispatcher. the node uses the even-queue from
1091   sdl and has its own event-passing-queue.
1092*/
1093void World::handleInput ()
1094{
1095  // localinput
1096  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
1097  cn->process();
1098  // remoteinput
1099}
1100
1101
1102/**
1103   \brief advance the timeline
1104
1105   this calculates the time used to process one frame (with all input handling, drawing, etc)
1106   the time is mesured in ms and passed to all world-entities and other classes that need
1107   a heart-beat.
1108*/
1109void World::tick ()
1110{
1111  Uint32 currentFrame = SDL_GetTicks();
1112  if(!this->bPause)
1113    {
1114      this->dt = currentFrame - this->lastFrame;
1115     
1116      if( this->dt > 0)
1117        {
1118          float fps = 1000/dt;
1119
1120          // temporary, only for showing how fast the text-engine is
1121          char tmpChar[20];
1122          sprintf(tmpChar, "fps: %4.0f", fps);
1123        }
1124      else
1125        {
1126          /* the frame-rate is limited to 100 frames per second, all other things are for
1127             nothing.
1128          */
1129          PRINTF(2)("fps = 1000 - frame rate is adjusted\n");
1130          SDL_Delay(10);
1131          this->dt = 10;
1132        }
1133      //this->timeSlice (dt);
1134     
1135      /* function to let all entities tick (iterate through list) */
1136      this->dtS = (float)this->dt / 1000.0;     
1137      this->gameTime += this->dtS;
1138      //entity = entities->enumerate();
1139      tIterator<WorldEntity>* iterator = this->entities->getIterator();
1140      WorldEntity* entity = iterator->nextElement();
1141      while( entity != NULL) 
1142        { 
1143          entity->tick (this->dtS);
1144          entity = iterator->nextElement();
1145        }
1146      delete iterator;
1147
1148      /* update tick the rest */
1149      this->trackManager->tick(this->dt);
1150      this->localCamera->tick(this->dt);
1151      this->garbageCollector->tick(this->dtS);
1152
1153      AnimationPlayer::getInstance()->tick(this->dtS);
1154      particleEngine->tick(this->dtS);
1155    }
1156  this->lastFrame = currentFrame;
1157}
1158
1159
1160/**
1161   \brief this function gives the world a consistant state
1162
1163   after ticking (updating the world state) this will give a constistant
1164   state to the whole system.
1165*/
1166void World::update()
1167{
1168  this->garbageCollector->update();
1169  this->nullParent->update (this->dtS);
1170
1171  SoundEngine::getInstance()->update();
1172}
1173
1174
1175/**
1176   \brief render the current frame
1177   
1178   clear all buffers and draw the world
1179*/
1180void World::display ()
1181{
1182  // clear buffer
1183  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1184  // set camera
1185  this->localCamera->apply ();
1186  // draw world
1187  this->draw();
1188  // draw HUD
1189  /* \todo draw HUD */
1190  // flip buffers
1191  SDL_GL_SwapBuffers();
1192  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
1193  //SDL_Flip (screen);
1194}
1195
1196
1197/**
1198   \brief add and spawn a new entity to this world
1199   \param entity to be added
1200*/
1201void World::spawn(WorldEntity* entity)
1202{
1203  this->entities->add (entity);
1204  entity->postSpawn ();
1205}
1206
1207
1208/**
1209   \brief add and spawn a new entity to this world
1210   \param entity to be added
1211   \param absCoor At what coordinates to add this entity.
1212   \param absDir In which direction should it look.
1213*/
1214void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
1215{
1216  this->entities->add (entity);
1217
1218  entity->setAbsCoor (*absCoor);
1219  entity->setAbsDir (*absDir);
1220
1221  entity->postSpawn ();
1222}
1223
1224
1225/**
1226   \brief add and spawn a new entity to this world
1227   \param entity to be added
1228   \param entity to be added to (PNode)
1229   \param At what relative  coordinates to add this entity.
1230   \param In which relative direction should it look.
1231*/
1232void World::spawn(WorldEntity* entity, PNode* parentNode, 
1233                  Vector* relCoor, Quaternion* relDir, 
1234                  int parentingMode)
1235{
1236  this->nullParent = NullParent::getInstance();
1237  if( parentNode != NULL)
1238    {
1239      parentNode->addChild (entity);
1240     
1241      entity->setRelCoor (*relCoor);
1242      entity->setRelDir (*relDir);
1243      entity->setMode(parentingMode);
1244     
1245      this->entities->add (entity);
1246     
1247      entity->postSpawn ();
1248    }
1249}
1250
1251
1252
1253/**
1254  \brief commands that the world must catch
1255  \returns false if not used by the world
1256*/
1257bool World::command(Command* cmd)
1258{
1259  if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW0)) this->localCamera->setViewMode(VIEW_NORMAL);
1260  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW1)) this->localCamera->setViewMode(VIEW_BEHIND);
1261  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW2)) this->localCamera->setViewMode(VIEW_FRONT);
1262  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW3)) this->localCamera->setViewMode(VIEW_LEFT);
1263  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW4)) this->localCamera->setViewMode(VIEW_RIGHT);
1264  else if( !strcmp( cmd->cmd, CONFIG_NAME_VIEW5)) this->localCamera->setViewMode(VIEW_TOP);
1265
1266  return false;
1267}
1268
1269void World::setPath( const char* name)
1270{
1271  if (this->path)
1272    delete this->path;
1273  if (ResourceManager::isFile(name))
1274  {
1275    this->path = new char[strlen(name)+1];
1276    strcpy(this->path, name);
1277  }
1278  else
1279    {
1280      this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1];
1281      sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
1282    }
1283}
1284
1285const char* World::getPath( void)
1286{
1287  return path;
1288}
Note: See TracBrowser for help on using the repository browser.