Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: digging deeper to bring more sense into the framework

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      se->preLoad();
125      se->load();
126      se->postLoad();
127
128      se->init();
129      se->preStart();
130      se->start();
131      se->destroy();
132
133      int nextWorldID = se->getNextStoryID();
134
135      this->entities.remove(se);
136      delete se;
137
138      //printf("Campaing::start() - got nextWorldID = %i\n", nextWorldID);
139      se = this->getStoryEntity(nextWorldID);
140      this->currentEntity = se;
141      if( ( nextWorldID == WORLD_ID_GAMEEND) || ( se == NULL) )
142        {
143          PRINTF(4)("Quitting campaing story loop\n");
144          if(se != NULL)
145            delete se;
146          return errorCode;
147        }
148    }
149
150    /* clean up all world that have not been loaded and therefore are still in the world list  -
151       this probably does not belong into the start function. remove this later
152    */
153    list<StoryEntity*>::iterator it;
154    for(it = this->entities.begin(); it != entities.end(); it++)
155    {
156      delete (*it);
157    }
158    PRINTF(1)("There is no StoryEnity left to play, quitting\n");
159}
160
161
162/**
163 *  pauses the campaing
164 */
165ErrorMessage Campaign::pause()
166{
167  if(this->currentEntity != NULL)
168    this->isPaused = true;
169}
170
171
172ErrorMessage Campaign::resume()
173{
174  if(this->currentEntity != NULL)
175    this->isPaused = false;
176}
177
178
179ErrorMessage Campaign::stop()
180{
181  this->running = false;
182  if(this->currentEntity != NULL)
183    {
184      this->currentEntity->stop();
185    }
186}
187
188
189ErrorMessage Campaign::destroy()
190{
191  if(this->currentEntity != NULL)
192    {
193      this->currentEntity->destroy();
194      delete this->currentEntity;
195      this->currentEntity = NULL;
196    }
197}
198
199
200/**
201  *  adds an game stroy entity to the campaign
202
203  * @param se: The entity
204  * @param storyID: The number that identifies the entity in the campaign. Each ID only used once in a Campaign
205
206    An entity can be a world (playable), a cinematic, a shop, sounds, whatever you
207    want to queue up in the campaign.
208*/
209void Campaign::addEntity(StoryEntity* se, int storyID)
210{
211  se->setStoryID(storyID);
212  this->addEntity(se);
213}
214
215void Campaign::addEntity(StoryEntity* se)
216{
217  this->entities.push_back(se);
218}
219
220
221void Campaign::removeEntity(int storyID)
222{
223  this->removeEntity(this->getStoryEntity(storyID));
224
225}
226
227
228void Campaign::removeEntity(StoryEntity* se)
229{
230  this->entities.remove(se);
231}
232
233
234/*
235  \brief this changes to the next level
236*/
237void Campaign::nextLevel()
238{
239  PRINTF(4)("Campaign:nextLevel()\n");
240  this->currentEntity->stop();
241}
242
243/*
244  \brief change to the previous level - not implemented
245
246  this propably useless
247*/
248void Campaign::previousLevel()
249{}
250
251
252/*
253  \brief lookup a entity with a given id
254* @param story id to be lookuped
255  @returns the entity found or NULL if search ended without match
256*/
257StoryEntity* Campaign::getStoryEntity(int storyID)
258{
259  //printf("Campaing::getStoryEntity(%i) - getting next Entity\n", storyID);
260  if( storyID == WORLD_ID_GAMEEND)
261    return NULL;
262
263  /*
264  tList<StoryEntity>* l;
265  StoryEntity* entity = NULL;
266  l = this->entities->getNext();
267  while( l != NULL)
268    {
269      entity = l->getObject();
270      l = l->getNext();
271
272      int id = entity->getStoryID();
273      //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id);
274      if(id == storyID)
275        {
276          //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n");
277          return entity;
278        }
279
280    }
281  */
282
283
284  list<StoryEntity*>::iterator it;
285  for (it = this->entities.begin(); it != this->entities.end(); it++)
286  {
287      int id = (*it)->getStoryID();
288      //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id);
289      if(id == storyID)
290        {
291          //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n");
292          return (*it);
293        }
294    }
295
296
297
298  return NULL;
299}
Note: See TracBrowser for help on using the repository browser.