Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/loading/game_loader.cc @ 4262

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

orxonox/trunk: moved around some files, and deleted the obsolete DataTank

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