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 | |
---|
24 | using 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 | */ |
---|
33 | Campaign::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 | */ |
---|
48 | Campaign::~Campaign () |
---|
49 | {} |
---|
50 | |
---|
51 | |
---|
52 | /** |
---|
53 | * initializes the class |
---|
54 | */ |
---|
55 | ErrorMessage 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 | */ |
---|
65 | void Campaign::loadParams(const TiXmlElement* root) |
---|
66 | { |
---|
67 | static_cast<BaseObject*>(this)->loadParams(root); |
---|
68 | |
---|
69 | LoadParam(root, "identifier", this, Campaign, setStoryID) |
---|
70 | .describe("A Unique Identifier for this Campaign"); |
---|
71 | |
---|
72 | LoadParamXML(root, "WorldList", this, Campaign, loadWorldListParams) |
---|
73 | .describe("A List of Worlds to be loaded in this Campaign"); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /** |
---|
78 | * loads a WorldList |
---|
79 | * @param root: the XML-element to load from |
---|
80 | */ |
---|
81 | void Campaign::loadWorldListParams(const TiXmlElement* root) |
---|
82 | { |
---|
83 | if( root == NULL) |
---|
84 | return; |
---|
85 | |
---|
86 | LOAD_PARAM_START_CYCLE(root, element); |
---|
87 | { |
---|
88 | StoryEntity* created = (StoryEntity*) Factory::fabricate(element); |
---|
89 | if( created != NULL) |
---|
90 | this->addEntity( created); |
---|
91 | PRINTF(3)("Campaign: Constructor: adding a world with name \"%s\" and id %i\n", |
---|
92 | created->getName(), created->getStoryID()); |
---|
93 | } |
---|
94 | LOAD_PARAM_END_CYCLE(element); |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | /** |
---|
100 | * starts the campaing |
---|
101 | */ |
---|
102 | ErrorMessage Campaign::start() |
---|
103 | { |
---|
104 | this->start(0); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | * starts the campaing with a specific ID |
---|
110 | * @param storyID the id of the StoryEntity |
---|
111 | */ |
---|
112 | ErrorMessage Campaign::start(int storyID = 0) |
---|
113 | { |
---|
114 | ErrorMessage errorCode; |
---|
115 | if( !this->isInit) return errorCode; |
---|
116 | if( storyID == WORLD_ID_GAMEEND) return errorCode; |
---|
117 | this->running = true; |
---|
118 | StoryEntity* se = this->getStoryEntity(storyID); |
---|
119 | this->currentEntity = se; |
---|
120 | while( se != NULL && this->running) |
---|
121 | { |
---|
122 | PRINTF(0)("Campaign is starting StoryEntity nr:%i\n", se->getStoryID()); |
---|
123 | |
---|
124 | se->init(); |
---|
125 | se->loadData(); |
---|
126 | |
---|
127 | se->preStart(); |
---|
128 | se->start(); |
---|
129 | |
---|
130 | |
---|
131 | int nextWorldID = se->getNextStoryID(); |
---|
132 | |
---|
133 | this->entities.remove(se); |
---|
134 | delete se; |
---|
135 | |
---|
136 | se = this->getStoryEntity(nextWorldID); |
---|
137 | this->currentEntity = se; |
---|
138 | if( ( nextWorldID == WORLD_ID_GAMEEND) || ( se == NULL) ) |
---|
139 | { |
---|
140 | PRINTF(4)("Quitting campaing story loop\n"); |
---|
141 | if(se != NULL) |
---|
142 | delete se; |
---|
143 | return errorCode; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /* clean up all world that have not been loaded and therefore are still in the world list - |
---|
148 | this probably does not belong into the start function. remove this later |
---|
149 | */ |
---|
150 | list<StoryEntity*>::iterator it; |
---|
151 | for(it = this->entities.begin(); it != entities.end(); it++) |
---|
152 | { |
---|
153 | delete (*it); |
---|
154 | } |
---|
155 | |
---|
156 | PRINTF(2)("There is no StoryEnity left to play, quitting\n"); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * pauses the campaing |
---|
162 | */ |
---|
163 | ErrorMessage Campaign::pause() |
---|
164 | { |
---|
165 | if(this->currentEntity != NULL) |
---|
166 | this->isPaused = true; |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | ErrorMessage Campaign::resume() |
---|
171 | { |
---|
172 | if(this->currentEntity != NULL) |
---|
173 | this->isPaused = false; |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | ErrorMessage Campaign::stop() |
---|
178 | { |
---|
179 | this->running = false; |
---|
180 | if(this->currentEntity != NULL) |
---|
181 | { |
---|
182 | this->currentEntity->stop(); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | /* |
---|
187 | ErrorMessage Campaign::destroy() |
---|
188 | { |
---|
189 | if(this->currentEntity != NULL) |
---|
190 | { |
---|
191 | this->currentEntity->destroy(); |
---|
192 | delete this->currentEntity; |
---|
193 | this->currentEntity = NULL; |
---|
194 | } |
---|
195 | }*/ |
---|
196 | |
---|
197 | |
---|
198 | /** |
---|
199 | * adds an game stroy entity to the campaign |
---|
200 | |
---|
201 | * @param se: The entity |
---|
202 | * @param storyID: The number that identifies the entity in the campaign. Each ID only used once in a Campaign |
---|
203 | |
---|
204 | An entity can be a world (playable), a cinematic, a shop, sounds, whatever you |
---|
205 | want to queue up in the campaign. |
---|
206 | */ |
---|
207 | void Campaign::addEntity(StoryEntity* se, int storyID) |
---|
208 | { |
---|
209 | se->setStoryID(storyID); |
---|
210 | this->addEntity(se); |
---|
211 | } |
---|
212 | |
---|
213 | void Campaign::addEntity(StoryEntity* se) |
---|
214 | { |
---|
215 | this->entities.push_back(se); |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | void Campaign::removeEntity(int storyID) |
---|
220 | { |
---|
221 | this->removeEntity(this->getStoryEntity(storyID)); |
---|
222 | |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | void Campaign::removeEntity(StoryEntity* se) |
---|
227 | { |
---|
228 | this->entities.remove(se); |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | /* |
---|
233 | \brief this changes to the next level |
---|
234 | */ |
---|
235 | void Campaign::nextLevel() |
---|
236 | { |
---|
237 | PRINTF(4)("Campaign:nextLevel()\n"); |
---|
238 | this->currentEntity->stop(); |
---|
239 | } |
---|
240 | |
---|
241 | /* |
---|
242 | \brief change to the previous level - not implemented |
---|
243 | |
---|
244 | this propably useless |
---|
245 | */ |
---|
246 | void Campaign::previousLevel() |
---|
247 | {} |
---|
248 | |
---|
249 | |
---|
250 | /* |
---|
251 | \brief lookup a entity with a given id |
---|
252 | * @param story id to be lookuped |
---|
253 | @returns the entity found or NULL if search ended without match |
---|
254 | */ |
---|
255 | StoryEntity* Campaign::getStoryEntity(int storyID) |
---|
256 | { |
---|
257 | if( storyID == WORLD_ID_GAMEEND) |
---|
258 | return NULL; |
---|
259 | |
---|
260 | int id; |
---|
261 | list<StoryEntity*>::iterator it; |
---|
262 | for( it = this->entities.begin(); it != this->entities.end(); it++) |
---|
263 | { |
---|
264 | id = (*it)->getStoryID(); |
---|
265 | if( id == storyID) |
---|
266 | return (*it); |
---|
267 | } |
---|
268 | |
---|
269 | return NULL; |
---|
270 | } |
---|