Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/levelloader/src/game_loader.cc @ 3604

Last change on this file since 3604 was 3557, checked in by chris, 20 years ago

orxonox/branches/levelloader: Rolling toward bug-free-ness

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