Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4225 was 4216, checked in by bensch, 20 years ago

orxonox/trunk: fix for levelloader, now uses resourceManager::getFullName

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