Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4336 was 4324, checked in by patrick, 20 years ago

orxonox/trunk: added a debug level at the end to be able to experiment a little with new load modules and game ideas

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