Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/guidedmissile/src/story_entities/campaign.cc @ 5848

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

orxonox/trunk: really cool hack, to let the fighter fly up and down :)

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