Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/campaign.cc @ 5651

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

orxonox/trunk: new (LoadParam && ExecutorSpecials)-Functionality that enables the Loading of XML-elements.
The new Executor really works out, and makes the understanding of loadParam way easier (given you won't read Executor :)

File size: 6.7 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 "campaign.h"
20
21#include "game_loader.h"
22#include "story_entity.h"
23
24#include "world.h"
25#include "camera.h"
26
27#include "list.h"
28
29#include "load_param.h"
30
31
32using namespace std;
33
34
35Campaign::Campaign ()
36{
37  this->setClassID(CL_CAMPAIGN, "Campaign");
38  this->entities = new tList<StoryEntity>();
39  this->isInit = false;
40}
41
42Campaign::Campaign ( TiXmlElement* root)
43{
44  this->setClassID(CL_CAMPAIGN, "Campaign");
45
46  PRINTF(4)("Loading Campaign...\n");
47
48  assert( root != NULL);
49
50  this->entities = new tList<StoryEntity>();
51  this->isInit = false;
52
53  this->loadParams(root);
54
55
56  //if( lastCreated != NULL)
57  //lastCreated->setStoryID( WORLD_ID_GAMEEND);
58}
59
60Campaign::~Campaign ()
61{}
62
63
64ErrorMessage Campaign::init()
65{
66  this->isInit = true;
67}
68
69/**
70  \brief loads the Parameters of a Campaign
71* @param root: The XML-element to load from
72 */
73void Campaign::loadParams(const TiXmlElement* root)
74{
75  static_cast<BaseObject*>(this)->loadParams(root);
76
77  //LoadParam<Campaign>(root, "identifier", this, &Campaign::setStoryID)
78  LoadParamNEW(root, "identifier", this, Campaign, setStoryID)
79     .describe("A Unique Identifier for this Campaign");
80
81//  LoadParam<Campaign>(root, "WorldList", this, &Campaign::loadWorldListParams)
82   LoadParamXML(root, "WorldList", this, Campaign, loadWorldListParams)
83      .describe("A List of Worlds to be loaded in this Campaign");
84}
85
86/**
87  \brief loads a WorldList
88* @param root: the XML-element to load from
89 */
90void Campaign::loadWorldListParams(const TiXmlElement* root)
91{
92  if (root == NULL)
93    return;
94
95  const TiXmlElement* element = root->FirstChildElement();
96  // load Worlds/Subcampaigns/Whatever
97  StoryEntity* lastCreated = NULL;
98  while( element != NULL)
99  {
100    PRINTF(5)("Campaign: Constructor: adding a world\n");
101    StoryEntity* created = (StoryEntity*) GameLoader::getInstance()->fabricate(element);
102      /*
103    if( lastCreated != NULL)
104    created->setNextStoryID( lastCreated->getStoryID());
105    else
106    created->setNextStoryID( WORLD_ID_GAMEEND);
107      */
108    if( created != NULL)
109    {
110      this->addEntity( created);
111      lastCreated = created;
112    }
113    element = element->NextSiblingElement();
114  }
115}
116
117ErrorMessage Campaign::start()
118{
119  this->start(0);
120}
121
122//! @todo boky -> fix it
123ErrorMessage Campaign::start(int storyID = 0)
124{
125  ErrorMessage errorCode;
126  if( !this->isInit) return errorCode;
127  if( storyID == WORLD_ID_GAMEEND) return errorCode;
128  this->running = true;
129  StoryEntity* se = this->getStoryEntity(storyID);
130  this->currentEntity = se;
131  while( se != NULL && this->running)
132    {
133      PRINTF(0)("Starting new StoryEntity Nr:%i\n", se->getStoryID());
134      se->displayLoadScreen();
135      se->preLoad();
136      se->load();
137      se->init();
138      se->releaseLoadScreen();
139      se->start();
140      se->destroy();
141
142      int nextWorldID = se->getNextStoryID();
143
144      this->entities->remove(se);
145      delete se;
146
147      //printf("Campaing::start() - got nextWorldID = %i\n", nextWorldID);
148      se = this->getStoryEntity(nextWorldID);
149      this->currentEntity = se;
150      if( ( nextWorldID == WORLD_ID_GAMEEND) ||( se == NULL) )
151        {
152          PRINTF(4)("Quitting campaing story loop\n");
153          if(se != NULL)
154            delete se;
155          return errorCode;
156        }
157    }
158
159    /* clean up all world that have not been loaded and therefore are still in the world list  -
160       this probably does not belong into the start function. remove this later
161    */
162    tIterator<StoryEntity>* it = this->entities->getIterator();
163    se = it->firstElement();
164    while( se != NULL)
165    {
166      delete se;
167      se = it->nextElement();
168    }
169    delete this->entities;
170    delete it;
171}
172
173
174ErrorMessage Campaign::pause()
175{
176  if(this->currentEntity != NULL)
177    this->isPaused = true;
178}
179
180
181ErrorMessage Campaign::resume()
182{
183  if(this->currentEntity != NULL)
184    this->isPaused = false;
185}
186
187
188ErrorMessage Campaign::stop()
189{
190  this->running = false;
191  if(this->currentEntity != NULL)
192    {
193      this->currentEntity->stop();
194    }
195}
196
197
198ErrorMessage Campaign::destroy()
199{
200  if(this->currentEntity != NULL)
201    {
202      this->currentEntity->destroy();
203      delete this->currentEntity;
204      this->currentEntity = NULL;
205    }
206}
207
208
209/**
210  *  adds an game stroy entity to the campaign
211
212  * @param se: The entity
213  * @param storyID: The number that identifies the entity in the campaign. Each ID only used once in a Campaign
214
215    An entity can be a world (playable), a cinematic, a shop, sounds, whatever you
216    want to queue up in the campaign.
217*/
218void Campaign::addEntity(StoryEntity* se, int storyID)
219{
220  se->setStoryID(storyID);
221  this->addEntity(se);
222}
223
224void Campaign::addEntity(StoryEntity* se)
225{
226  this->entities->add(se);
227}
228
229
230void Campaign::removeEntity(int storyID)
231{
232  this->removeEntity(this->getStoryEntity(storyID));
233
234}
235
236
237void Campaign::removeEntity(StoryEntity* se)
238{
239  this->entities->remove(se);
240}
241
242
243/*
244  \brief this changes to the next level
245*/
246void Campaign::nextLevel()
247{
248  PRINTF(4)("Campaign:nextLevel()\n");
249  this->currentEntity->stop();
250}
251
252/*
253  \brief change to the previous level - not implemented
254
255  this propably useless
256*/
257void Campaign::previousLevel()
258{}
259
260
261/*
262  \brief lookup a entity with a given id
263* @param story id to be lookuped
264  @returns the entity found or NULL if search ended without match
265*/
266StoryEntity* Campaign::getStoryEntity(int storyID)
267{
268  //printf("Campaing::getStoryEntity(%i) - getting next Entity\n", storyID);
269  if( storyID == WORLD_ID_GAMEEND)
270    return NULL;
271
272  /*
273  tList<StoryEntity>* l;
274  StoryEntity* entity = NULL;
275  l = this->entities->getNext();
276  while( l != NULL)
277    {
278      entity = l->getObject();
279      l = l->getNext();
280
281      int id = entity->getStoryID();
282      //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id);
283      if(id == storyID)
284        {
285          //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n");
286          return entity;
287        }
288
289    }
290  */
291
292
293  tIterator<StoryEntity>* iterator = this->entities->getIterator();
294  StoryEntity* entity = iterator->firstElement();
295  while( entity != NULL)
296    {
297      int id = entity->getStoryID();
298      //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id);
299      if(id == storyID)
300        {
301          //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n");
302          delete iterator;
303          return entity;
304        }
305      entity = iterator->nextElement();
306    }
307  delete iterator;
308
309
310  return NULL;
311}
Note: See TracBrowser for help on using the repository browser.