Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/story_entities/campaign.cc @ 6372

Last change on this file since 6372 was 6372, checked in by patrick, 18 years ago

network: more cleanup in the StoryEntity, thightening the interface and function renames

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