Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Factory-Redesign on the wish of manuel

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