Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/heightMap/src/game_loader.cc @ 4213

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

orxonox/branches/heightMap: merged the Trunk back into branches/heightMap:
merged with Command
svn merge -r 3918:HEAD trunk branches/heightMap
conflicts resolved in favor of the Trunk

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