Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5604 was 5324, checked in by bensch, 19 years ago

orxonox/trunk: better algorithm for loading the Shader through the ResourceManager
if a fragment-Shader was supplied, but the file did not exist, the Shader gets rejected

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