Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/story_entities/network_world.cc @ 6093

Last change on this file since 6093 was 6093, checked in by patrick, 18 years ago

network: branche comiles again

File size: 22.9 KB
RevLine 
[4555]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
[2190]13   co-programmer: Christian Meyer
[5390]14   co-programmer: Benjamin Grauer
[1853]15*/
16
[3590]17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
18
[6093]19#include "network_world.h"
[3608]20
[5205]21#include "shell_command.h"
[3620]22
[4347]23#include "state.h"
24
[3608]25#include "p_node.h"
26#include "null_parent.h"
[4326]27#include "pilot_node.h"
[2190]28#include "world_entity.h"
[2036]29#include "player.h"
[2190]30#include "camera.h"
[2816]31#include "environment.h"
[3419]32#include "skysphere.h"
[3803]33#include "skybox.h"
[3750]34#include "satellite.h"
[4245]35#include "test_entity.h"
[3608]36#include "terrain.h"
[3436]37#include "light.h"
[4726]38#include "load_param.h"
[5174]39#include "shell.h"
[3620]40
[3646]41#include "garbage_collector.h"
[4940]42#include "fast_factory.h"
[3812]43#include "animation_player.h"
[4176]44#include "particle_engine.h"
[4245]45#include "graphics_engine.h"
[4338]46#include "physics_engine.h"
[4396]47#include "fields.h"
[3646]48
[4488]49#include "md2Model.h"
50
[3608]51#include "glmenu_imagescreen.h"
52#include "list.h"
[4010]53#include "game_loader.h"
[2036]54
[3964]55#include "animation3d.h"
[3608]56
[4010]57#include "substring.h"
[3608]58
[4261]59#include "factory.h"
[4245]60
[5556]61#include "weapons/projectile.h"
[4405]62#include "event_handler.h"
[4504]63#include "sound_engine.h"
[4961]64#include "ogg_player.h"
[4504]65
[4747]66#include "class_list.h"
67
[4917]68#include "cd_engine.h"
[5750]69#include "npcs/npc_test1.h"
[5318]70#include "shader.h"
[4820]71
[5915]72#include "playable.h"
[5996]73#include "network_manager.h"
74#include "playable.h"
[5915]75
[5996]76
[6093]77//SHELL_COMMAND(speed, NetworkWorld, setSpeed);
78//SHELL_COMMAND(togglePNodeVisibility, NetworkWorld, togglePNodeVisibility);
79//SHELL_COMMAND(toggleBVVisibility, NetworkWorld, toggleBVVisibility);
[5205]80
[1856]81using namespace std;
[1853]82
[6069]83//! This creates a Factory to fabricate a NetworkWorld
[6093]84//CREATE_FACTORY(NetworkWorld, CL_WORLD);
[3620]85
[6069]86NetworkWorld::NetworkWorld(const TiXmlElement* root)
[4010]87{
88  this->constuctorInit("", -1);
[4094]89  this->path = NULL;
[4555]90
[4261]91  this->loadParams(root);
[4010]92}
93
[4555]94/**
[6069]95  *  create a new NetworkWorld
[4555]96
[2551]97    This creates a new empty world!
[1858]98*/
[6069]99NetworkWorld::NetworkWorld (const char* name)
[1855]100{
[4094]101  this->path = NULL;
[4010]102  this->constuctorInit(name, -1);
[1855]103}
104
[3449]105/**
[6069]106 *  creates a new NetworkWorld...
[4836]107 * @param worldID with this ID
[3449]108*/
[6069]109NetworkWorld::NetworkWorld (int worldID)
[2636]110{
[4094]111  this->path = NULL;
[4010]112  this->constuctorInit(NULL, worldID);
[2636]113}
114
[4555]115/**
[6069]116 *  remove the NetworkWorld from memory
[4555]117
[3365]118    delete everything explicitly, that isn't contained in the parenting tree!
119    things contained in the tree are deleted automaticaly
[4838]120 */
[6069]121NetworkWorld::~NetworkWorld ()
[1872]122{
[5206]123  delete this->shell;
[6069]124  PRINTF(3)("NetworkWorld::~NetworkWorld() - deleting current world\n");
[3677]125
[5915]126
[6069]127  // here everything that is alocated by the NetworkWorld is deleted
[4837]128  delete this->entities;
[4830]129  State::setWorldEntityList(NULL);
130
[5915]131  delete this->localPlayer;
[5115]132
[5296]133  // delete all the initialized Engines.
[5447]134  FastFactory::flushAll(true);
[4735]135  delete LightManager::getInstance();
[4822]136  delete ParticleEngine::getInstance();
[5296]137  delete AnimationPlayer::getInstance();
[4978]138  delete PhysicsEngine::getInstance();
[4822]139
[4978]140  // external engines initialized by the orxonox-class get deleted
[4504]141  SoundEngine::getInstance()->flushAllBuffers();
[4830]142  SoundEngine::getInstance()->flushAllSources();
[4504]143
[5115]144
[4978]145  // erease everything that is left.
[4870]146  delete NullParent::getInstance();
[5915]147
[5318]148  Shader::suspendShader();
[4872]149
[4978]150  // unload the resources !!
[4136]151  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
[5218]152
153  delete[] this->path;
[1872]154}
[1858]155
[3526]156/**
[4978]157 * initializes the world.
158 * @param name the name of the world
159 * @param worldID the ID of this world
160 *
161 * set all stuff here that is world generic and does not use to much memory
162 * because the real init() function StoryEntity::init() will be called
163 * shortly before start of the game.
164 * since all worlds are initiated/referenced before they will be started.
165 * NO LEVEL LOADING HERE - NEVER!
[3526]166*/
[6069]167void NetworkWorld::constuctorInit(const char* name, int worldID)
[3526]168{
[6069]169  this->setClassID(CL_WORLD, "NetworkWorld");
[2636]170
[4978]171  this->setName(name);
[6069]172  this->debugNetworkWorldNr = worldID;
[5211]173  this->gameTime = 0.0f;
[5205]174  this->setSpeed(1.0);
[4961]175  this->music = NULL;
[5211]176  this->shell = NULL;
177  this->entities = NULL;
[5915]178  this->localPlayer = NULL;
179  this->localCamera = NULL;
[5389]180
181  this->showPNodes = false;
[5429]182  this->showBV = false;
[3629]183}
[3526]184
[4978]185/**
[6069]186 * loads the parameters of a NetworkWorld from an XML-element
[4978]187 * @param root the XML-element to load from
188 */
[6069]189void NetworkWorld::loadParams(const TiXmlElement* root)
[4261]190{
[6069]191  PRINTF(4)("Creating a NetworkWorld\n");
[4261]192
[6069]193  LoadParam(root, "identifier", this, NetworkWorld, setStoryID)
[4261]194    .describe("Sets the StoryID of this world");
[4834]195
[6069]196  LoadParam(root, "nextid", this, NetworkWorld, setNextStoryID)
[4261]197    .describe("Sets the ID of the next world");
[4834]198
[6069]199  LoadParam(root, "path", this, NetworkWorld, setPath)
200    .describe("The Filename of this NetworkWorld (relative from the data-dir)");
[4261]201}
202
[3629]203/**
[4978]204 * this is executed just before load
205 *
206 * since the load function sometimes needs data, that has been initialized
207 * before the load and after the proceeding storyentity has finished
[3629]208*/
[6069]209ErrorMessage NetworkWorld::preLoad()
[3629]210{
[4829]211  State::setWorldEntityList(this->entities = new tList<WorldEntity>());
212  this->cycle = 0;
213
[3620]214  /* init the world interface */
[5206]215  this->shell = new Shell();
[4010]216
[4735]217  LightManager::getInstance();
[4978]218  NullParent::getInstance ();
[3993]219
[4010]220  AnimationPlayer::getInstance(); // initializes the animationPlayer
[5915]221  ParticleEngine::getInstance();
[4338]222  PhysicsEngine::getInstance();
[4010]223
[4015]224  this->localCamera = new Camera();
[6069]225  this->localCamera->setName ("NetworkWorld-Camera");
[4555]226
[4827]227  State::setCamera(this->localCamera, this->localCamera->getTarget());
[4347]228
[4245]229  GraphicsEngine::getInstance()->displayFPS(true);
[4918]230
231  CDEngine::getInstance()->setEntityList( this->entities);
[3526]232}
233
234
[3449]235/**
[6069]236 *  loads the NetworkWorld by initializing all resources, and set their default values.
[3449]237*/
[6069]238ErrorMessage NetworkWorld::load()
[4555]239{
[4104]240  PRINTF(3)("> Loading world: '%s'\n", getPath());
241  TiXmlElement* element;
[4010]242  GameLoader* loader = GameLoader::getInstance();
[4555]243
[4010]244  if( getPath() == NULL)
[2636]245    {
[6069]246      PRINTF(1)("NetworkWorld has no path specified for loading");
247      this->loadDebugNetworkWorld(this->getStoryID());
248      return (ErrorMessage){213,"Path not specified","NetworkWorld::load()"};
[4010]249    }
[4555]250
[5915]251  TiXmlDocument* XMLDoc = new TiXmlDocument( getPath());
[4010]252  // load the campaign document
[4555]253  if( !XMLDoc->LoadFile())
[4010]254  {
255    // report an error
[5915]256    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getPath(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
[4010]257    delete XMLDoc;
[6069]258    return (ErrorMessage){213,"XML File parsing error","NetworkWorld::load()"};
[4010]259  }
[4555]260
[4010]261  // check basic validity
262  TiXmlElement* root = XMLDoc->RootElement();
263  assert( root != NULL);
[4555]264
[4010]265  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
266    {
267      // report an error
[4104]268      PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
[4010]269      delete XMLDoc;
[6069]270      return (ErrorMessage){213,"Path not a WorldDataFile","NetworkWorld::load()"};
[4010]271    }
[4555]272
[6069]273
[4010]274  // load the parameters
275  // name
276  const char* string = grabParameter( root, "name");
277  if( string == NULL)
278    {
[4104]279      PRINTF(2)("World is missing a proper 'name'\n");
[5211]280      this->setName("Unknown");
[4010]281    }
282  else
283    {
[5211]284      this->setName(string);
[4010]285    }
[4978]286
[6069]287
[4104]288  ////////////////
289  // LOADSCREEN //
290  ////////////////
291  element = root->FirstChildElement("LoadScreen");
292  if (element == NULL)
293    {
294      PRINTF(2)("no LoadScreen specified, loading default\n");
295
296      glmis->setBackgroundImage("pictures/load_screen.jpg");
297      this->glmis->setMaximum(8);
298      this->glmis->draw();
299    }
300  else
301    {
[4261]302      this->glmis->loadParams(element);
[4104]303      this->glmis->draw();
304    }
305  this->glmis->draw();
[4726]306
[6069]307
308
309  ////////////////////////////
310  // Loading Spawning Point //
311  ////////////////////////////
312  element = root->FirstChildElement("SpawningPoints");
313  if( element == NULL)
314  {
315    PRINTF(1)("NetworkWorld is missing 'SpawningPoints'\n");
316  }
317  else
318  {
319    element = element->FirstChildElement();
320      // load Players/Objects/Whatever
321    PRINTF(4)("Loading Spawning Points\n");
322    while( element != NULL)
323    {
324      BaseObject* created = Factory::fabricate(element);
325      if( created != NULL )
326      {
[6085]327        if(created->isA(CL_SPAWNING_POINT))
[6069]328          this->spawn(dynamic_cast<WorldEntity*>(created));
329        printf("Created a Spawning Point %s: %s\n", created->getClassName(), created->getName());
330      }
331
332
333      element = element->NextSiblingElement();
334      glmis->step(); //! @todo temporary
335    }
336    PRINTF(4)("Done loading NetworkWorldEntities\n");
337  }
338
339
[4726]340  ////////////////////////
341  // find WorldEntities //
342  ////////////////////////
[4104]343  element = root->FirstChildElement("WorldEntities");
[4010]344  if( element == NULL)
345    {
[6069]346      PRINTF(1)("NetworkWorld is missing 'WorldEntities'\n");
[4010]347    }
348  else
349    {
350      element = element->FirstChildElement();
351      // load Players/Objects/Whatever
[6069]352      PRINTF(4)("Loading NetworkWorldEntities\n");
[4010]353      while( element != NULL)
[4555]354        {
[5996]355          BaseObject* created = Factory::fabricate(element);
[5915]356          if( created != NULL )
357          {
358            if(created->isA(CL_WORLD_ENTITY))
359              this->spawn(dynamic_cast<WorldEntity*>(created));
[5996]360            printf("Created a %s: %s\n", created->getClassName(), created->getName());
[5915]361          }
362
[4555]363          // if we load a 'Player' we use it as localPlayer
[5915]364
365
[4555]366          //todo do this more elegant
[5915]367          if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
368            sky = dynamic_cast<SkyBox*>(created);
[4918]369          if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
370          {
[5915]371            terrain = dynamic_cast<Terrain*>(created);
[4918]372            CDEngine::getInstance()->setTerrain(terrain);
373          }
[4555]374          element = element->NextSiblingElement();
[4836]375          glmis->step(); //! @todo temporary
[4555]376        }
[6069]377      PRINTF(4)("Done loading NetworkWorldEntities\n");
[4010]378    }
[4555]379
[4726]380    //////////////////////////////
381    // LOADING ADDITIONAL STUFF //
382    //////////////////////////////
383
[5652]384    LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
[4735]385
[5915]386   LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
387//   LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
[4726]388
[4010]389  // free the XML data
[4015]390
[4010]391  delete XMLDoc;
[4015]392  /* GENERIC LOADING PROCESS FINISHED */
[4555]393
394
[5915]395  // Create a Player
396  this->localPlayer = new Player();
[4245]397
[5915]398  Playable* playable;
399  const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
400  if (playableList != NULL)
401  {
402    playable = dynamic_cast<Playable*>(playableList->front());
403    this->localPlayer->setControllable(playable);
404  }
[4555]405
[5915]406  // bind camera
407  playable->addChild (this->localCamera);
[4555]408
[5915]409//   //localCamera->setParent(TrackNode::getInstance());
410//  tn->addChild(this->localCamera);
[4620]411  localCamera->setClipRegion(1, 10000.0);
[5915]412  localCamera->lookAt(playable);
413//  this->localPlayer->setParentMode(PNODE_ALL);
[5355]414  if (sky != NULL)
415  {
416    this->sky->setParent(this->localCamera);
417    this->sky->setParentMode(PNODE_MOVEMENT);
418  }
[3368]419
[4010]420  // initialize debug coord system
421  objectList = glGenLists(1);
422  glNewList (objectList, GL_COMPILE);
[4555]423
[4010]424  glEndList();
[3993]425
[4504]426  SoundEngine::getInstance()->setListener(this->localCamera);
[4176]427
[4347]428
[4709]429
[4715]430  ////////////
431  // STATIC //
432  ////////////
433
434
[4721]435//   TestEntity* testEntity = new TestEntity();
436//   testEntity->setRelCoor(Vector(570, 10, -15));
437//   testEntity->setRelDir(Quaternion(M_PI, Vector(0, 1, 0)));
438//   this->spawn(testEntity);
[4397]439
[4976]440  for(int i = 0; i < 100; i++)
441  {
[5750]442    WorldEntity* tmp = new NPCTest1();
[5049]443    char npcChar[10];
444    sprintf (npcChar, "NPC_%d", i);
[5256]445        tmp->setName(npcChar);
[5336]446    tmp->setAbsCoor(((float)rand()/RAND_MAX) * 5000, 50/*+ (float)rand()/RAND_MAX*20*/, ((float)rand()/RAND_MAX -.5) *30);
[4976]447    this->spawn(tmp);
448  }
449
[5211]450  this->music = NULL;//(OggPlayer*)ResourceManager::getInstance()->load("sound/00-luke_grey_-_hypermode.ogg", OGG, RP_LEVEL);
451  //music->playback();
[4010]452}
[3365]453
[4245]454
[4324]455
[4326]456/**
[4978]457 * creates a debug world: only for experimental stuff
[4326]458*/
[6069]459void NetworkWorld::loadDebugNetworkWorld(int worldID)
[4010]460{
461  /*monitor progress*/
462  this->glmis->step();
[4228]463  // stuff beyond this point remains to be loaded properly
[3194]464
[4010]465  // LIGHT initialisation
[4735]466  LightManager::getInstance()->setAmbientColor(.1,.1,.1);
[4736]467//  LightManager::getInstance()->addLight();
[4735]468  LightManager::getInstance()->debug();
[3368]469
[6069]470  switch(this->debugNetworkWorldNr)
[4010]471    {
472      /*
[4555]473        this loads the hard-coded debug world. this only for simplicity and will be
474        removed by a reald world-loader, which interprets a world-file.
475        if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and
476        make whatever you want...
[4010]477      */
478    case DEBUG_WORLD_0:
479      {
[4735]480        LightManager::getInstance()->getLight()->setAbsCoor(-5.0, 10.0, -40.0);
[4555]481        /*monitor progress*/
482        this->glmis->step();
[4010]483
[4555]484        // bind camera
485        this->localCamera = new Camera();
486        this->localCamera->setName ("camera");
487        /*monitor progress*/
488        this->glmis->step();
[2816]489
[3419]490
[4555]491        // Create SkySphere
[4621]492        this->sky = new Skysphere("pictures/sky-replace.jpg");
493        this->sky->setName("SkySphere");
494        this->spawn(this->sky);
[4555]495        this->localCamera->addChild(this->sky);
496        this->sky->setParentMode(PNODE_MOVEMENT);
497        /*monitor progress*/
498        this->glmis->step();
[3368]499
[3521]500
[4555]501        terrain = new Terrain("worlds/newGround.obj");
502        terrain->setRelCoor(Vector(0,-10,0));
503        this->spawn(terrain);
504        /*monitor progress*/
505        this->glmis->step();
[2816]506
[4555]507        this->glmis->step();
508        break;
[4010]509      }
510    case DEBUG_WORLD_1:
511      {
[3365]512
[4555]513        break;
[4010]514      }
515    case DEBUG_WORLD_2:
516      {
[3727]517
[4555]518        break;
[4010]519      }
520    default:
[4324]521      break;
[2636]522    }
[4010]523}
[2636]524
[3459]525/**
[6069]526 *  initializes a new NetworkWorld shortly before start
[4978]527 *
528 * this is the function, that will be loaded shortly before the world is
529 * started
[3459]530*/
[6069]531ErrorMessage NetworkWorld::init()
[3459]532{
533  this->bPause = false;
[5051]534
535  /* update the object position before game start - so there are no wrong coordinates used in the first processing */
[5769]536  NullParent::getInstance()->updateNode (0.001f);
537  NullParent::getInstance()->updateNode (0.001f);
[5084]538
[3459]539}
540
541
542/**
[6069]543 *  starts the NetworkWorld
[3459]544*/
[6069]545ErrorMessage NetworkWorld::start()
[3459]546{
[6069]547  PRINTF(3)("NetworkWorld::start() - starting current NetworkWorld: nr %i\n", this->debugNetworkWorldNr);
[3459]548  this->bQuitOrxonox = false;
549  this->bQuitCurrentGame = false;
550  this->mainLoop();
551}
552
553/**
[4836]554 *  stops the world.
[3459]555
556   This happens, when the player decides to end the Level.
557*/
[6069]558ErrorMessage NetworkWorld::stop()
[3459]559{
[6069]560  PRINTF(3)("NetworkWorld::stop() - got stop signal\n");
[3459]561  this->bQuitCurrentGame = true;
562}
563
564/**
[4836]565 *  pauses the Game
[3459]566*/
[6069]567ErrorMessage NetworkWorld::pause()
[3459]568{
569  this->isPaused = true;
570}
571
572/**
[4836]573 *  ends the pause Phase
[3459]574*/
[6069]575ErrorMessage NetworkWorld::resume()
[3459]576{
577  this->isPaused = false;
578}
579
580/**
[6069]581 *  destroys the NetworkWorld
[3459]582*/
[6069]583ErrorMessage NetworkWorld::destroy()
[3459]584{
[3566]585
[3459]586}
587
588/**
[4836]589 *  shows the loading screen
[3459]590*/
[6069]591void NetworkWorld::displayLoadScreen ()
[3459]592{
[6069]593  PRINTF(3)("NetworkWorld::displayLoadScreen - start\n");
[4555]594
595  //GLMenuImageScreen*
[4099]596  this->glmis = new GLMenuImageScreen();
[3675]597  this->glmis->setMaximum(8);
[4555]598
[6069]599  PRINTF(3)("NetworkWorld::displayLoadScreen - end\n");
[3459]600}
601
602/**
[4836]603 *  removes the loadscreen, and changes over to the game
[3459]604
[4836]605   @todo take out the delay
[3459]606*/
[6069]607void NetworkWorld::releaseLoadScreen ()
[3459]608{
[6069]609  PRINTF(3)("NetworkWorld::releaseLoadScreen - start\n");
[3459]610  this->glmis->setValue(this->glmis->getMaximum());
[6069]611  PRINTF(3)("NetworkWorld::releaseLoadScreen - end\n");
[4099]612  delete this->glmis;
[3459]613}
614
615
[3620]616/**
[4836]617 *  gets the list of entities from the world
618 * @returns entity list
[3620]619*/
[6069]620tList<WorldEntity>* NetworkWorld::getEntities()
[3620]621{
622  return this->entities;
623}
624
625
[3646]626/**
[4836]627 *  this returns the current game time
628 * @returns elapsed game time
[3646]629*/
[6069]630double NetworkWorld::getGameTime()
[3646]631{
632  return this->gameTime;
633}
634
635
[4555]636/**
[4836]637 *  function to put your own debug stuff into it. it can display informations about
[3225]638   the current class/procedure
639*/
[6069]640void NetworkWorld::debug()
[2640]641{
[6069]642  PRINTF(0)("Printing out the List of alive NetworkWorldEntities:\n");
[5115]643  tIterator<WorldEntity>* iterator = this->entities->getIterator();
644  WorldEntity* entity = iterator->firstElement();
645  while( entity != NULL)
646  {
647    PRINTF(0)("%s::%s\n", entity->getClassName(), entity->getName());
648    entity = iterator->nextElement();
649  }
650  delete iterator;
[2640]651}
[2636]652
[2640]653
[3449]654/**
[3225]655  \brief main loop of the world: executing all world relevant function
656
657  in this loop we synchronize (if networked), handle input events, give the heart-beat to
658  all other member-entities of the world (tick to player, enemies etc.), checking for
659  collisions drawing everything to the screen.
660*/
[6069]661void NetworkWorld::mainLoop()
[2636]662{
[3365]663  this->lastFrame = SDL_GetTicks ();
[6069]664  PRINTF(3)("NetworkWorld::mainLoop() - Entering main loop\n");
[5045]665
[4836]666  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* @todo implement pause */
[2551]667    {
[4558]668      ++this->cycle;
[6069]669      PRINTF(4)("NetworkWorld::mainloop() - number of entities: %i\n", this->entities->getSize());
[2636]670      // Network
[3365]671      this->synchronize ();
[2636]672      // Process input
[3365]673      this->handleInput ();
[3215]674      if( this->bQuitCurrentGame || this->bQuitOrxonox)
[4555]675          break;
[2636]676      // Process time
[3551]677      this->tick ();
[5045]678      // Process collision
679      this->collide ();
[3551]680      // Update the state
[4555]681      this->update ();
[2636]682      // Draw
[3365]683      this->display ();
[5045]684    }
[3548]685
[6069]686  PRINTF(3)("NetworkWorld::mainLoop() - Exiting the main loop\n");
[1899]687}
688
[3459]689
[2190]690/**
[4836]691 *  synchronize local data with remote data
[1855]692*/
[6069]693void NetworkWorld::synchronize ()
[1855]694{
[2636]695  // Get remote input
696  // Update synchronizables
[5996]697/*  NetworkManager::getInstance()->synchronize();*/
[1855]698}
[2636]699
[3459]700
[2636]701/**
[4836]702 *  run all input processing
[3225]703
704   the command node is the central input event dispatcher. the node uses the even-queue from
705   sdl and has its own event-passing-queue.
[2636]706*/
[6069]707void NetworkWorld::handleInput ()
[2636]708{
[4407]709  EventHandler::getInstance()->process();
710
[2636]711  // remoteinput
712}
713
[3459]714
[2636]715/**
[4836]716 *  advance the timeline
[3225]717
718   this calculates the time used to process one frame (with all input handling, drawing, etc)
719   the time is mesured in ms and passed to all world-entities and other classes that need
720   a heart-beat.
[2636]721*/
[6069]722void NetworkWorld::tick ()
[2636]723{
724  Uint32 currentFrame = SDL_GetTicks();
725  if(!this->bPause)
726    {
[3644]727      this->dt = currentFrame - this->lastFrame;
[4555]728
[4610]729      if( this->dt > 10)
[4555]730        {
731          float fps = 1000/dt;
[3790]732
[4555]733          // temporary, only for showing how fast the text-engine is
734          char tmpChar[20];
735          sprintf(tmpChar, "fps: %4.0f", fps);
736        }
[2636]737      else
[4555]738        {
739          /* the frame-rate is limited to 100 frames per second, all other things are for
740             nothing.
741          */
[5048]742          PRINTF(3)("fps = 1000 - frame rate is adjusted\n");
[4610]743          SDL_Delay(10-dt);
[4555]744          this->dt = 10;
745        }
746
[5205]747      this->dtS = (float)this->dt / 1000.0 * this->speed;
[4145]748      this->gameTime += this->dtS;
[4833]749
[3654]750      tIterator<WorldEntity>* iterator = this->entities->getIterator();
[5115]751      WorldEntity* entity = iterator->firstElement();
[4555]752      while( entity != NULL)
753        {
754          entity->tick (this->dtS);
755          entity = iterator->nextElement();
756        }
[3654]757      delete iterator;
[4010]758
[3459]759      /* update tick the rest */
[4832]760      this->localCamera->tick(this->dtS);
[4558]761      // tick the engines
[4245]762      AnimationPlayer::getInstance()->tick(this->dtS);
[4979]763//      if (this->cycle > 5)
[4558]764        PhysicsEngine::getInstance()->tick(this->dtS);
[4396]765
[4558]766      ParticleEngine::getInstance()->tick(this->dtS);
767      GarbageCollector::getInstance()->tick(this->dtS);
[4396]768
[4831]769
[4558]770      /** actualy the Graphics Engine should tick the world not the other way around...
[4555]771         but since we like the things not too complicated we got it this way around
772         until there is need or time to do it the other way around.
[4836]773         @todo: GraphicsEngine ticks world: separation of processes and data...
[4681]774
775        bensch: in my opinion the GraphicsEngine could draw the world, but not tick it,
776         beceause graphics have nothing(or at least not much) to do with Motion.
[4245]777      */
778      GraphicsEngine::getInstance()->tick(this->dtS);
[2636]779    }
780  this->lastFrame = currentFrame;
781}
782
[3216]783
[2636]784/**
[4836]785 *  this function gives the world a consistant state
[3551]786
787   after ticking (updating the world state) this will give a constistant
788   state to the whole system.
789*/
[6069]790void NetworkWorld::update()
[3551]791{
[4822]792  GarbageCollector::getInstance()->update();
[5406]793  GraphicsEngine::getInstance()->update(this->dtS);
[5769]794  NullParent::getInstance()->updateNode (this->dtS);
[4504]795
796  SoundEngine::getInstance()->update();
[4978]797  //music->update();
[3551]798}
799
800
[6069]801void NetworkWorld::collide()
[4917]802{
[4918]803  CDEngine::getInstance()->checkCollisions();
[4917]804}
805
[3551]806/**
[4836]807 *  render the current frame
[4555]808
[3225]809   clear all buffers and draw the world
[2636]810*/
[6069]811void NetworkWorld::display ()
[2636]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
[4837]820  /** @todo draw HUD */
[2636]821  // flip buffers
[4681]822  GraphicsEngine::swapBuffers();
[3365]823  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
824  //SDL_Flip (screen);
[2636]825}
826
[2644]827
[3225]828/**
[4917]829 *  runs through all entities calling their draw() methods
830 */
[6069]831void NetworkWorld::draw ()
[4917]832{
833  /* draw entities */
834  WorldEntity* entity;
835  glLoadIdentity();
836  tIterator<WorldEntity>* iterator = this->entities->getIterator();
[5115]837  entity = iterator->firstElement();
[4917]838  while( entity != NULL )
839  {
[5055]840    if( entity->isVisible() ) entity->draw();
[5429]841    if( unlikely( this->showBV)) entity->drawBVTree(3, 226);  // to draw the bounding boxes of the objects at level 2 for debug purp
[4917]842    entity = iterator->nextElement();
843  }
844  delete iterator;
845
846  glCallList (objectList);
847
848  ParticleEngine::getInstance()->draw();
849
[5389]850  if (unlikely(this->showPNodes))
851    NullParent::getInstance()->debugDraw(0);
852
[4917]853  GraphicsEngine::getInstance()->draw();
854  //TextEngine::getInstance()->draw();
855}
856
857/**
[4836]858 *  add and spawn a new entity to this world
859 * @param entity to be added
[3225]860*/
[6069]861void NetworkWorld::spawn(WorldEntity* entity)
[2644]862{
[3365]863  this->entities->add (entity);
[3233]864  entity->postSpawn ();
[2816]865}
866
867
[3225]868/**
[4836]869 *  add and spawn a new entity to this world
870 * @param entity to be added
871 * @param absCoor At what coordinates to add this entity.
872 * @param absDir In which direction should it look.
[3225]873*/
[6069]874void NetworkWorld::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
[2816]875{
[3529]876  this->entities->add (entity);
877
[3809]878  entity->setAbsCoor (*absCoor);
879  entity->setAbsDir (*absDir);
[3365]880
[3233]881  entity->postSpawn ();
[2644]882}
[2816]883
884
[3521]885/**
[4836]886 *  add and spawn a new entity to this world
887 * @param entity to be added
888 * @param entity to be added to (PNode)
889 * @param At what relative  coordinates to add this entity.
890 * @param In which relative direction should it look.
[3521]891*/
[6069]892void NetworkWorld::spawn(WorldEntity* entity, PNode* parentNode,
[4765]893                  Vector* relCoor, Quaternion* relDir)
[3521]894{
[3529]895  if( parentNode != NULL)
[3521]896    {
897      parentNode->addChild (entity);
[4555]898
[3809]899      entity->setRelCoor (*relCoor);
900      entity->setRelDir (*relDir);
[4555]901
[3521]902      this->entities->add (entity);
[4555]903
[3521]904      entity->postSpawn ();
905    }
906}
907
[6069]908void NetworkWorld::setPath( const char* name)
[4010]909{
[4094]910  if (this->path)
911    delete this->path;
912  if (ResourceManager::isFile(name))
913  {
914    this->path = new char[strlen(name)+1];
915    strcpy(this->path, name);
916  }
917  else
918    {
919      this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1];
920      sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
921    }
[4010]922}
923
[6069]924const char* NetworkWorld::getPath( void)
[4010]925{
926  return path;
927}
Note: See TracBrowser for help on using the repository browser.