Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/game_loader.cc @ 4095

Last change on this file since 4095 was 4094, checked in by bensch, 20 years ago

orxonox/trunk: orxonox now runs from anywhere of the LINUX environment

File size: 6.9 KB
RevLine 
[2636]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: ...
16*/
17
18
19#include "game_loader.h"
20#include "campaign.h"
21#include "world.h"
22#include "player.h"
23#include "orxonox.h"
24#include "camera.h"
25#include "command_node.h"
26#include "vector.h"
[4094]27#include "resource_manager.h"
[4010]28#include "factory.h"
[2636]29
30#include <string.h>
31
32
33using namespace std;
34
35
36GameLoader* GameLoader::singletonRef = 0;
37
38
[4017]39GameLoader::GameLoader () 
40{
41  first = NULL;
42}
[2636]43
44
45GameLoader::~GameLoader () {}
46
47
[3225]48/**
49   \brief this class is a singleton class
50   \returns an instance of itself
51
52   if you are unsure about singleton classes, check the theory out on the internet :)
53*/
[2636]54GameLoader* GameLoader::getInstance()
55{
56  if(singletonRef == NULL)
57    singletonRef = new GameLoader();
58  return singletonRef;
59}
60
61
[3222]62ErrorMessage GameLoader::init()
[2636]63{
64  if(this->currentCampaign != NULL)
65    this->currentCampaign->init();
66}
67
68
[3225]69/**
70   \brief reads a campaign definition file into a campaign class
71   \param filename to be loaded
72   \returns the loaded campaign
73
74   this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
75*/
[3222]76ErrorMessage GameLoader::loadCampaign(char* name)
[2636]77{
[3222]78  ErrorMessage errorCode;
[4094]79  char* campaignName;
80  if (ResourceManager::isFile(name))
81    {
82      this->currentCampaign = this->fileToCampaign(name);
83    }
84  else
85    {
86      campaignName = new char[strlen(ResourceManager::getInstance()->getDataDir())+strlen(name)];
87      sprintf(campaignName, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
88      this->currentCampaign = this->fileToCampaign(campaignName);
89      delete campaignName;
90    }
[2636]91}
92
[3225]93
94/**
95   \brief loads a debug campaign for test purposes only.
96   \param the identifier of the campaign.
97   \returns error message if not able to do so.
98*/
[3222]99ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID)
[2636]100{
101  switch(campaignID)
102    {
[3629]103      /*
104         Debug Level 0: Debug level used to test the base frame work.
105         As you can see, all storyentity data is allocated before game
106         start. the storyentity will load themselfs shortly before start
107         through the StoryEntity::init() funtion.
108      */
[2636]109    case DEBUG_CAMPAIGN_0:
110      {
111        Campaign* debugCampaign = new Campaign();
[3220]112
[2636]113        World* world0 = new World(DEBUG_WORLD_0);
[3220]114        world0->setNextStoryID(WORLD_ID_1);
[2636]115        debugCampaign->addEntity(world0, WORLD_ID_0);
[3220]116
[2636]117        World* world1 = new World(DEBUG_WORLD_1);
[3727]118        world1->setNextStoryID(WORLD_ID_2);
[2636]119        debugCampaign->addEntity(world1, WORLD_ID_1);
120
[3727]121        World* world2 = new World(DEBUG_WORLD_2);
122        world2->setNextStoryID(WORLD_ID_GAMEEND);
123        debugCampaign->addEntity(world2, WORLD_ID_2);
124
[2636]125        this->currentCampaign = debugCampaign;
126        break;
127      }
128    }
129}
130
[3222]131ErrorMessage GameLoader::start()
[2636]132{
133  if(this->currentCampaign != NULL)
134    this->currentCampaign->start();
135}
136
137
[3222]138ErrorMessage GameLoader::stop()
[2636]139{
140  if(this->currentCampaign != NULL)
141    this->currentCampaign->stop();
142  this->currentCampaign = NULL;
143}
144
145
[3222]146ErrorMessage GameLoader::pause()
[2636]147{
148  this->isPaused = true;
149  if(this->currentCampaign != NULL)
150    this->currentCampaign->pause();
151}
152
153
[3222]154ErrorMessage GameLoader::resume()
[2636]155{
156  this->isPaused = false;
157  if(this->currentCampaign != NULL)
158    this->currentCampaign->resume();
159}
160
[3225]161/**
162   \brief release the mem
163 */
164ErrorMessage GameLoader::destroy()
[2636]165{}
166
167
[3225]168/**
169   \brief reads a campaign definition file into a campaign class
170   \param filename to be loaded
171   \returns the loaded campaign
172
173   this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
174*/
[2636]175Campaign* GameLoader::fileToCampaign(char *name)
176{
177  /* do not entirely load the campaign. just the current world
178     before start of each world, it has to be initialized so it
179     can load everything it needs into memory then.
180  */
[4010]181 
182  if( name == NULL)
183    {
184      PRINTF0("No filename specified for loading");
185      return NULL;
186    }
187 
188  TiXmlDocument* XMLDoc = new TiXmlDocument( name);
189  // load the campaign document
190  if( !XMLDoc->LoadFile())
191    {
192      // report an error
193      PRINTF0("Error loading XML File: %s @ %d:%d\n", XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
194      delete XMLDoc;
195      return NULL;
196    }
197       
198  // check basic validity
199  TiXmlElement* root = XMLDoc->RootElement();
200  assert( root != NULL);
201       
202  if( strcmp( root->Value(), "Campaign"))
203    {
204      // report an error
205      PRINTF(0)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
206      delete XMLDoc;
207      return NULL;
208    }
209       
210  // construct campaign
211  Campaign* c = new Campaign( root);
212       
213  // free the XML data
214  delete XMLDoc;
215       
216  return c;
[2636]217}
218
219
220/**
221   \brief handle keyboard commands
222   \param cmd: the command to handle
[3225]223   \returns true if the command was handled by the system
[2636]224*/
225bool GameLoader::worldCommand (Command* cmd)
226{
[4091]227  if( !strcmp( cmd->cmd, CONFIG_NAME_NEXT_WORLD))
[2636]228    {
229      if( !cmd->bUp) 
230        {
231          this->nextLevel();
232        }
233      return true;
234    }
[4091]235  else if( !strcmp( cmd->cmd, CONFIG_NAME_PREV_WORLD))
[2636]236    {
237      if( !cmd->bUp)
238        {
239          this->previousLevel();
240        }
241      return true;
242    }
[4091]243  else if( !strcmp( cmd->cmd, CONFIG_NAME_PAUSE))
[2636]244    {
245      if( !cmd->bUp)
246        {
247          if(this->isPaused)
248            this->resume();
249          else
250            this->pause();
251        }
252      return true;
253    }
[4091]254  else if( !strcmp( cmd->cmd, CONFIG_NAME_QUIT))
[3220]255    {
256      if( !cmd->bUp) this->stop();
257      return true;
258    }
[2636]259  return false;
260}
261
[3225]262
263/*
264  \brief this changes to the next level
265*/
[2636]266void GameLoader::nextLevel()
267{
268  if(this->currentCampaign != NULL)
269    this->currentCampaign->nextLevel();
270}
271
[3225]272
273/*
274  \brief change to the previous level - not implemented
275
276  this propably useless
277*/
[2636]278void GameLoader::previousLevel()
279{
280  if(this->currentCampaign != NULL)
281    this->currentCampaign->previousLevel();
282}
[4010]283
284/**
285   \brief add a Factory to the Factory Q
286   \param factory a Factory to be registered
287*/
288void GameLoader::registerFactory( Factory* factory)
289{
290        assert( factory != NULL);
291       
[4020]292        PRINTF0("Registered factory for '%s'\n", factory->getFactoryName());
[4010]293       
294        if( first == NULL) first = factory;
295        else first->registerFactory( factory);
296}
297
298/**
299   \brief load a StoryEntity
300   \param element a XMLElement containing all the needed info
301*/
302BaseObject* GameLoader::fabricate( TiXmlElement* element)
303{
304        assert( element != NULL);
305       
306        if( first == NULL)
307        {
308                PRINTF0("GameLoader does not know any factories, fabricate() failed\n");
309                return NULL;
310        }
311       
312        if( element->Value() != NULL)
313        {
314                PRINTF0("Attempting fabrication of a '%s'\n", element->Value());
315                BaseObject* b = first->fabricate( element);
316                if( b == NULL) PRINTF0("Failed to fabricate a '%s'\n", element->Value());
317                else PRINTF0("Successfully fabricated a '%s'\n", element->Value());
318                return b;
319        }
320       
321        PRINTF0("Fabricate failed, TiXmlElement did not contain a value\n");
322       
323        return NULL;
324}
Note: See TracBrowser for help on using the repository browser.