Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4014 was 4010, checked in by bensch, 20 years ago

orxonox/trunk: merged the levelloader from lltrunktemp to the trunk. Big thanks to fuzzy to make this so easy for us, and for implementing it in the first place.

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