Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6394 was 6389, checked in by patrick, 19 years ago

network: removed segfault in the cleanup function

File size: 5.3 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
37  PRINTF(4)("Loading Campaign...\n");
38  assert( root != NULL);
39
40  this->campaignData = new CampaignData(root);
41  this->loadParams(root);
42
43}
44
45
46/**
47 * the campaign destructor
48 */
49Campaign::~Campaign ()
50{
51  if( this->campaignData)
52    delete this->campaignData;
53}
54
55
56/**
57 *  loads the Parameters of a Campaign
58 * @param root: The XML-element to load from
59 */
60void Campaign::loadParams(const TiXmlElement* root)
61{
62  static_cast<StoryEntity*>(this)->loadParams(root);
63
64  PRINTF(4)("Loaded Campaign specific stuff\n");
65
66}
67
68
69/**
70 *  starts the campaing with a specific ID
71 * @param storyID the id of the StoryEntity
72 */
73bool Campaign::start()
74{
75  PRINTF(2)("Starting Campaign nr. %i\n", this->getStoryID());
76
77  this->isRunning = true;
78  this->run();
79
80  PRINTF(2)("There is no StoryEnity left to play, quitting\n");
81}
82
83
84/**
85 *  pauses the campaign
86 */
87bool Campaign::pause()
88{
89  this->isPaused = true;
90}
91
92
93/**
94 *  resumes the campaign after a pause
95 */
96bool Campaign::resume()
97{
98  PRINTF(4)("Resuming the current Campaign\n");
99  this->isPaused = false;
100}
101
102
103/**
104 *  stops the campaign
105 */
106bool Campaign::stop()
107{
108  PRINTF(4)("Stopping the current Campaign\n");
109  this->isRunning = false;
110  if( this->currentEntity != NULL)
111  {
112    this->currentEntity->stop();
113  }
114}
115
116
117/**
118 *  runs the campaign
119 */
120void Campaign::run()
121{
122  ErrorMessage       errorCode;
123  int                storyID = WORLD_ID_0;
124
125  for( this->currentEntity = this->campaignData->getFirstLevel(), this->isRunning = true;
126       this->currentEntity != NULL && this->isRunning;
127       this->currentEntity = this->campaignData->getNextLevel())
128  {
129    PRINTF(0)("Campaign is starting StoryEntity nr:%i\n", this->currentEntity->getStoryID());
130
131    this->currentEntity->init();
132    this->currentEntity->loadData();
133    this->currentEntity->start();
134    this->currentEntity->unloadData();
135  }
136}
137
138
139/**
140 *  this changes to the next level
141 */
142void Campaign::switchToNextLevel()
143{
144  PRINTF(4)("Switching to the next StoryEntity\n");
145  this->currentEntity->stop();
146}
147
148
149
150/* ==========================================================================================================
151   CampaignData
152   ==========================================================================================================
153*/
154
155/**
156 * constructor for CampaignData
157 */
158CampaignData::CampaignData(const TiXmlElement* root)
159{
160  this->setClassID(CL_CAMPAIGN_DATA, "CampaignData");
161  this->currentEntity = NULL;
162
163  this->loadParams(root);
164}
165
166
167/**
168 * destructor for CampaignData
169 */
170CampaignData::~CampaignData()
171{
172  PRINTF(0)("Deleting CampaignData\n");
173  while( !this->storyEntities.empty())
174  {
175    StoryEntity* bo = this->storyEntities.back();
176    this->storyEntities.pop_back();
177    PRINTF(0)("CampaignData is been deleted: nr %i\n", bo->getStoryID());
178    delete bo;
179  }
180}
181
182
183
184/**
185 *  loads the Parameters of a Campaign
186 * @param root: The XML-element to load from
187 */
188void CampaignData::loadParams(const TiXmlElement* root)
189{
190  LoadParamXML(root, "WorldList", this, CampaignData, loadParamsWorldList)
191      .describe("A List of Worlds to be loaded in this Campaign");
192
193}
194
195
196/**
197 *  loads a WorldList
198 * @param root: the XML-element to load from
199 */
200void CampaignData::loadParamsWorldList(const TiXmlElement* root)
201{
202  if( root == NULL)
203    return;
204
205  LOAD_PARAM_START_CYCLE(root, element);
206  {
207    StoryEntity* created = (StoryEntity*) Factory::fabricate(element);
208    if( created != NULL)
209      this->addStoryEntity(created);
210  }
211  LOAD_PARAM_END_CYCLE(element);
212}
213
214
215/**
216 *  add the StoryEntity to the campaign data tank
217 * @param se the StoryEntity to add
218 */
219void CampaignData::addStoryEntity(StoryEntity* se)
220{
221  this->storyEntities.push_back(se);
222}
223
224
225/**
226 *  switch to the next level in the list and return it
227 */
228StoryEntity* CampaignData::getFirstLevel()
229{
230  int                            nextStoryID;
231  int                            storyID;
232  list<StoryEntity*>::iterator   it;
233
234  nextStoryID = 0;
235  this->currentEntity = NULL;
236  for( it = this->storyEntities.begin(); it != this->storyEntities.end(); it++)
237  {
238    storyID = (*it)->getStoryID();
239    if( storyID == nextStoryID)
240      this->currentEntity = (*it);
241  }
242  return this->currentEntity;
243}
244
245
246/**
247 *  switch to the next level in the list and return it
248 */
249StoryEntity* CampaignData::getNextLevel()
250{
251  int                            nextStoryID;
252  int                            storyID;
253  list<StoryEntity*>::iterator   it;
254
255  nextStoryID = this->currentEntity->getNextStoryID();
256  this->currentEntity = NULL;
257  for( it = this->storyEntities.begin(); it != this->storyEntities.end(); it++)
258  {
259    storyID = (*it)->getStoryID();
260    if( storyID == nextStoryID)
261      this->currentEntity = (*it);
262  }
263  return this->currentEntity;
264}
265
266
267
268
269
Note: See TracBrowser for help on using the repository browser.