Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4030 was 3746, checked in by chris, 20 years ago

orxonox/branches/levelloader: Merged trunk into branch… still not working though…

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