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 | |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | |
---|
35 | Campaign::Campaign () |
---|
36 | { |
---|
37 | this->setClassID(CL_CAMPAIGN, "Campaign"); |
---|
38 | this->entities = new tList<StoryEntity>(); |
---|
39 | this->isInit = false; |
---|
40 | } |
---|
41 | |
---|
42 | Campaign::Campaign ( TiXmlElement* root) |
---|
43 | { |
---|
44 | this->setClassID(CL_CAMPAIGN, "Campaign"); |
---|
45 | |
---|
46 | PRINTF(4)("Loading Campaign...\n"); |
---|
47 | |
---|
48 | assert( root != NULL); |
---|
49 | |
---|
50 | this->entities = new tList<StoryEntity>(); |
---|
51 | this->isInit = false; |
---|
52 | |
---|
53 | this->loadParams(root); |
---|
54 | |
---|
55 | |
---|
56 | //if( lastCreated != NULL) |
---|
57 | //lastCreated->setStoryID( WORLD_ID_GAMEEND); |
---|
58 | } |
---|
59 | |
---|
60 | Campaign::~Campaign () |
---|
61 | {} |
---|
62 | |
---|
63 | |
---|
64 | ErrorMessage Campaign::init() |
---|
65 | { |
---|
66 | this->isInit = true; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | \brief loads the Parameters of a Campaign |
---|
71 | * @param root: The XML-element to load from |
---|
72 | */ |
---|
73 | void Campaign::loadParams(const TiXmlElement* root) |
---|
74 | { |
---|
75 | static_cast<BaseObject*>(this)->loadParams(root); |
---|
76 | |
---|
77 | LoadParam(root, "identifier", this, Campaign, setStoryID) |
---|
78 | .describe("A Unique Identifier for this Campaign"); |
---|
79 | |
---|
80 | LoadParamXML(root, "WorldList", this, Campaign, loadWorldListParams) |
---|
81 | .describe("A List of Worlds to be loaded in this Campaign"); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | \brief loads a WorldList |
---|
86 | * @param root: the XML-element to load from |
---|
87 | */ |
---|
88 | void Campaign::loadWorldListParams(const TiXmlElement* root) |
---|
89 | { |
---|
90 | if (root == NULL) |
---|
91 | return; |
---|
92 | |
---|
93 | LOAD_PARAM_START_CYCLE(root, element); |
---|
94 | { |
---|
95 | PRINTF(5)("Campaign: Constructor: adding a world\n"); |
---|
96 | StoryEntity* created = (StoryEntity*) GameLoader::getInstance()->fabricate(element); |
---|
97 | if( created != NULL) |
---|
98 | { |
---|
99 | this->addEntity( created); |
---|
100 | } |
---|
101 | } |
---|
102 | LOAD_PARAM_END_CYCLE(element); |
---|
103 | } |
---|
104 | |
---|
105 | ErrorMessage Campaign::start() |
---|
106 | { |
---|
107 | this->start(0); |
---|
108 | } |
---|
109 | |
---|
110 | //! @todo boky -> fix it |
---|
111 | ErrorMessage 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)("Starting new StoryEntity Nr:%i\n", se->getStoryID()); |
---|
122 | se->displayLoadScreen(); |
---|
123 | se->preLoad(); |
---|
124 | se->load(); |
---|
125 | se->init(); |
---|
126 | se->releaseLoadScreen(); |
---|
127 | se->start(); |
---|
128 | se->destroy(); |
---|
129 | |
---|
130 | int nextWorldID = se->getNextStoryID(); |
---|
131 | |
---|
132 | this->entities->remove(se); |
---|
133 | delete se; |
---|
134 | |
---|
135 | //printf("Campaing::start() - got nextWorldID = %i\n", nextWorldID); |
---|
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 | tIterator<StoryEntity>* it = this->entities->getIterator(); |
---|
151 | se = it->firstElement(); |
---|
152 | while( se != NULL) |
---|
153 | { |
---|
154 | delete se; |
---|
155 | se = it->nextElement(); |
---|
156 | } |
---|
157 | delete this->entities; |
---|
158 | delete it; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | ErrorMessage Campaign::pause() |
---|
163 | { |
---|
164 | if(this->currentEntity != NULL) |
---|
165 | this->isPaused = true; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | ErrorMessage Campaign::resume() |
---|
170 | { |
---|
171 | if(this->currentEntity != NULL) |
---|
172 | this->isPaused = false; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | ErrorMessage Campaign::stop() |
---|
177 | { |
---|
178 | this->running = false; |
---|
179 | if(this->currentEntity != NULL) |
---|
180 | { |
---|
181 | this->currentEntity->stop(); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | ErrorMessage 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 | */ |
---|
206 | void Campaign::addEntity(StoryEntity* se, int storyID) |
---|
207 | { |
---|
208 | se->setStoryID(storyID); |
---|
209 | this->addEntity(se); |
---|
210 | } |
---|
211 | |
---|
212 | void Campaign::addEntity(StoryEntity* se) |
---|
213 | { |
---|
214 | this->entities->add(se); |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | void Campaign::removeEntity(int storyID) |
---|
219 | { |
---|
220 | this->removeEntity(this->getStoryEntity(storyID)); |
---|
221 | |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | void Campaign::removeEntity(StoryEntity* se) |
---|
226 | { |
---|
227 | this->entities->remove(se); |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | /* |
---|
232 | \brief this changes to the next level |
---|
233 | */ |
---|
234 | void 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 | */ |
---|
245 | void 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 | */ |
---|
254 | StoryEntity* Campaign::getStoryEntity(int storyID) |
---|
255 | { |
---|
256 | //printf("Campaing::getStoryEntity(%i) - getting next Entity\n", storyID); |
---|
257 | if( storyID == WORLD_ID_GAMEEND) |
---|
258 | return NULL; |
---|
259 | |
---|
260 | /* |
---|
261 | tList<StoryEntity>* l; |
---|
262 | StoryEntity* entity = NULL; |
---|
263 | l = this->entities->getNext(); |
---|
264 | while( l != NULL) |
---|
265 | { |
---|
266 | entity = l->getObject(); |
---|
267 | l = l->getNext(); |
---|
268 | |
---|
269 | int id = entity->getStoryID(); |
---|
270 | //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id); |
---|
271 | if(id == storyID) |
---|
272 | { |
---|
273 | //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n"); |
---|
274 | return entity; |
---|
275 | } |
---|
276 | |
---|
277 | } |
---|
278 | */ |
---|
279 | |
---|
280 | |
---|
281 | tIterator<StoryEntity>* iterator = this->entities->getIterator(); |
---|
282 | StoryEntity* entity = iterator->firstElement(); |
---|
283 | while( entity != NULL) |
---|
284 | { |
---|
285 | int id = entity->getStoryID(); |
---|
286 | //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id); |
---|
287 | if(id == storyID) |
---|
288 | { |
---|
289 | //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n"); |
---|
290 | delete iterator; |
---|
291 | return entity; |
---|
292 | } |
---|
293 | entity = iterator->nextElement(); |
---|
294 | } |
---|
295 | delete iterator; |
---|
296 | |
---|
297 | |
---|
298 | return NULL; |
---|
299 | } |
---|