Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3501 was 3501, checked in by chris, 19 years ago

orxonox/branches/levelloader: Feeble attempt to load anything… neither finished nor useful

File size: 5.6 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
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                PRINTF(ERR)("No filename specified for loading");
164  }
165 
166        TiXMLDocument* XMLDoc = new TiXMLDocument( name);
167        // load the campaign document
168        if( !XMLDoc.LoadFile())
169        {
170                // report an error
171                PRINTF(ERR)("Error loading XML File: %s @ %d:%d\n", XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol());
172                delete XMLDoc;
173                return NULL;
174        }
175       
176        // check basic validity
177        TiXMLElement* element = XMLDoc.RootElement();
178        assert( element != NULL);
179       
180        element = element->FirstChildElement( "Campaign");
181       
182        if( element == NULL )
183        {
184                // report an error
185                PRINTF(ERR)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
186                delete XMLDoc;
187                return NULL;
188        }
189       
190        // construct campaign
191        Campaign c = new Campaign( element);
192       
193        // free the XML data
194        delete XMLDoc;
195}
196
197
198/**
199   \brief handle keyboard commands
200   \param cmd: the command to handle
201   \returns true if the command was handled by the system
202*/
203bool GameLoader::worldCommand (Command* cmd)
204{
205  if( !strcmp( cmd->cmd, "up_world"))
206    {
207      if( !cmd->bUp) 
208        {
209          this->nextLevel();
210        }
211      return true;
212    }
213  else if( !strcmp( cmd->cmd, "down_world"))
214    {
215      if( !cmd->bUp)
216        {
217          this->previousLevel();
218        }
219      return true;
220    }
221  else if( !strcmp( cmd->cmd, "pause"))
222    {
223      if( !cmd->bUp)
224        {
225          if(this->isPaused)
226            this->resume();
227          else
228            this->pause();
229        }
230      return true;
231    }
232  else if( !strcmp( cmd->cmd, "quit"))
233    {
234      if( !cmd->bUp) this->stop();
235      return true;
236    }
237  return false;
238}
239
240
241/*
242  \brief this changes to the next level
243*/
244void GameLoader::nextLevel()
245{
246  if(this->currentCampaign != NULL)
247    this->currentCampaign->nextLevel();
248}
249
250
251/*
252  \brief change to the previous level - not implemented
253
254  this propably useless
255*/
256void GameLoader::previousLevel()
257{
258  if(this->currentCampaign != NULL)
259    this->currentCampaign->previousLevel();
260}
261
262
263/**
264   \brief add a Factory to the Factory Q
265   \param factory a Factory to be registered
266*/
267void GameLoader::registerFactory( Factory* factory)
268{
269        if( next == NULL) next = factory;
270        else next->registerFactory( factory);
271}
272
273/**
274   \brief load a StoryEntity
275   \param element a XMLElement containing all the needed info
276*/
277StoryEntity* GameLoader::fabricate( TiXMLElement* element)
278{
279        if( first == NULL)
280        {
281                PRINTF(ERR)("GameLoader does not know any factories, fabricate() failed\n");
282                return NULL;
283        }
284       
285        return first->fabricate( element);
286}
Note: See TracBrowser for help on using the repository browser.