Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: load param is now processed correctly

File size: 5.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
27/**
28 *  the constructor
29 * @param root the XML root element
30 *
31 * this constructor is always called in a XML context (loading procedure)
32 */
33Campaign::Campaign ( TiXmlElement* root)
34{
35  this->setClassID(CL_CAMPAIGN, "Campaign");
36  this->isInit = false;
37
38  PRINTF(4)("Loading Campaign...\n");
39  assert( root != NULL);
40
41  this->loadParams(root);
42}
43
44
45/**
46 * the campaign destructor
47 */
48Campaign::~Campaign ()
49{}
50
51
52/**
53 *  initializes the class
54 */
55ErrorMessage Campaign::init()
56{
57  this->isInit = true;
58}
59
60
61/**
62 *  loads the Parameters of a Campaign
63 * @param root: The XML-element to load from
64 */
65void Campaign::loadParams(const TiXmlElement* root)
66{
67  static_cast<StoryEntity*>(this)->loadParams(root);
68
69  LoadParamXML(root, "WorldList", this, Campaign, loadWorldListParams)
70  .describe("A List of Worlds to be loaded in this Campaign");
71
72  PRINTF(4)("Loaded Campaign specific stuff\n");
73}
74
75
76/**
77 *  loads a WorldList
78 * @param root: the XML-element to load from
79 */
80void Campaign::loadWorldListParams(const TiXmlElement* root)
81{
82  if( root == NULL)
83    return;
84
85  LOAD_PARAM_START_CYCLE(root, element);
86  {
87    StoryEntity* created = (StoryEntity*) Factory::fabricate(element);
88    if( created != NULL)
89      this->addEntity( created);
90    PRINTF(3)("Campaign: Constructor: adding a world with name \"%s\" and id %i\n",
91              created->getName(), created->getStoryID());
92  }
93  LOAD_PARAM_END_CYCLE(element);
94
95}
96
97
98/**
99 *  starts the campaing
100 */
101ErrorMessage Campaign::start()
102{
103  this->start(0);
104}
105
106
107/**
108 *  starts the campaing with a specific ID
109 * @param storyID the id of the StoryEntity
110 */
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)("Campaign is starting StoryEntity nr:%i\n", se->getStoryID());
122
123    se->init();
124    se->loadData();
125
126    se->preStart();
127    se->start();
128
129
130    int nextWorldID = se->getNextStoryID();
131
132    this->entities.remove(se);
133    delete se;
134
135    se = this->getStoryEntity(nextWorldID);
136    this->currentEntity = se;
137    if( ( nextWorldID == WORLD_ID_GAMEEND) || ( se == NULL) )
138    {
139      PRINTF(4)("Quitting campaing story loop\n");
140      if(se != NULL)
141        delete se;
142      return errorCode;
143    }
144  }
145
146  /* clean up all world that have not been loaded and therefore are still in the world list  -
147     this probably does not belong into the start function. remove this later
148  */
149  list<StoryEntity*>::iterator it;
150  for(it = this->entities.begin(); it != entities.end(); it++)
151  {
152    delete (*it);
153  }
154
155  PRINTF(2)("There is no StoryEnity left to play, quitting\n");
156}
157
158
159/**
160 *  pauses the campaing
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.push_back(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  if( storyID == WORLD_ID_GAMEEND)
257    return NULL;
258
259  int id;
260  list<StoryEntity*>::iterator it;
261  for( it = this->entities.begin(); it != this->entities.end(); it++)
262  {
263    id = (*it)->getStoryID();
264    if( id == storyID)
265      return (*it);
266  }
267
268  return NULL;
269}
Note: See TracBrowser for help on using the repository browser.