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 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | Campaign::Campaign () |
---|
35 | { |
---|
36 | this->setClassID(CL_CAMPAIGN, "Campaign"); |
---|
37 | this->entities = new tList<StoryEntity>(); |
---|
38 | this->isInit = false; |
---|
39 | } |
---|
40 | |
---|
41 | Campaign::Campaign ( TiXmlElement* root) |
---|
42 | { |
---|
43 | this->setClassID(CL_CAMPAIGN, "Campaign"); |
---|
44 | |
---|
45 | PRINTF(3)("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 | |
---|
59 | Campaign::~Campaign () |
---|
60 | {} |
---|
61 | |
---|
62 | |
---|
63 | ErrorMessage 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 | */ |
---|
72 | void 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 | */ |
---|
87 | void 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("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 | |
---|
111 | ErrorMessage Campaign::start() |
---|
112 | { |
---|
113 | this->start(0); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | ErrorMessage 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 | this->entities->remove(se); |
---|
137 | delete se; |
---|
138 | |
---|
139 | int nextWorldID = se->getNextStoryID(); |
---|
140 | //printf("Campaing::start() - got nextWorldID = %i\n", nextWorldID); |
---|
141 | se = this->getStoryEntity(nextWorldID); |
---|
142 | this->currentEntity = se; |
---|
143 | if( ( nextWorldID == WORLD_ID_GAMEEND) ||( se == NULL) ) |
---|
144 | { |
---|
145 | PRINTF(0)("Quitting campaing story loop\n"); |
---|
146 | if(se != NULL) |
---|
147 | delete se; |
---|
148 | return errorCode; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /* clean up all world that have not been loaded and therefore are still in the world list - |
---|
153 | this probably does not belong into the start function. remove this later |
---|
154 | */ |
---|
155 | tIterator<StoryEntity>* it = this->entities->getIterator(); |
---|
156 | se = it->firstElement(); |
---|
157 | while( se != NULL) |
---|
158 | { |
---|
159 | delete se; |
---|
160 | se = it->nextElement(); |
---|
161 | } |
---|
162 | delete this->entities; |
---|
163 | delete it; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | ErrorMessage Campaign::pause() |
---|
168 | { |
---|
169 | if(this->currentEntity != NULL) |
---|
170 | this->isPaused = true; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | ErrorMessage Campaign::resume() |
---|
175 | { |
---|
176 | if(this->currentEntity != NULL) |
---|
177 | this->isPaused = false; |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | ErrorMessage Campaign::stop() |
---|
182 | { |
---|
183 | this->running = false; |
---|
184 | if(this->currentEntity != NULL) |
---|
185 | { |
---|
186 | this->currentEntity->stop(); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | ErrorMessage Campaign::destroy() |
---|
192 | { |
---|
193 | if(this->currentEntity != NULL) |
---|
194 | { |
---|
195 | this->currentEntity->destroy(); |
---|
196 | delete this->currentEntity; |
---|
197 | this->currentEntity = NULL; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | /** |
---|
203 | * adds an game stroy entity to the campaign |
---|
204 | |
---|
205 | * @param se: The entity |
---|
206 | * @param storyID: The number that identifies the entity in the campaign. Each ID only used once in a Campaign |
---|
207 | |
---|
208 | An entity can be a world (playable), a cinematic, a shop, sounds, whatever you |
---|
209 | want to queue up in the campaign. |
---|
210 | */ |
---|
211 | void Campaign::addEntity(StoryEntity* se, int storyID) |
---|
212 | { |
---|
213 | se->setStoryID(storyID); |
---|
214 | this->addEntity(se); |
---|
215 | } |
---|
216 | |
---|
217 | void Campaign::addEntity(StoryEntity* se) |
---|
218 | { |
---|
219 | this->entities->add(se); |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | void Campaign::removeEntity(int storyID) |
---|
224 | { |
---|
225 | this->removeEntity(this->getStoryEntity(storyID)); |
---|
226 | |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | void Campaign::removeEntity(StoryEntity* se) |
---|
231 | { |
---|
232 | this->entities->remove(se); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | /* |
---|
237 | \brief this changes to the next level |
---|
238 | */ |
---|
239 | void Campaign::nextLevel() |
---|
240 | { |
---|
241 | printf("Campaign:nextLevel()\n"); |
---|
242 | this->currentEntity->stop(); |
---|
243 | } |
---|
244 | |
---|
245 | /* |
---|
246 | \brief change to the previous level - not implemented |
---|
247 | |
---|
248 | this propably useless |
---|
249 | */ |
---|
250 | void Campaign::previousLevel() |
---|
251 | {} |
---|
252 | |
---|
253 | |
---|
254 | /* |
---|
255 | \brief lookup a entity with a given id |
---|
256 | * @param story id to be lookuped |
---|
257 | @returns the entity found or NULL if search ended without match |
---|
258 | */ |
---|
259 | StoryEntity* Campaign::getStoryEntity(int storyID) |
---|
260 | { |
---|
261 | //printf("Campaing::getStoryEntity(%i) - getting next Entity\n", storyID); |
---|
262 | if( storyID == WORLD_ID_GAMEEND) |
---|
263 | return NULL; |
---|
264 | |
---|
265 | /* |
---|
266 | tList<StoryEntity>* l; |
---|
267 | StoryEntity* entity = NULL; |
---|
268 | l = this->entities->getNext(); |
---|
269 | while( l != NULL) |
---|
270 | { |
---|
271 | entity = l->getObject(); |
---|
272 | l = l->getNext(); |
---|
273 | |
---|
274 | int id = entity->getStoryID(); |
---|
275 | //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id); |
---|
276 | if(id == storyID) |
---|
277 | { |
---|
278 | //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n"); |
---|
279 | return entity; |
---|
280 | } |
---|
281 | |
---|
282 | } |
---|
283 | */ |
---|
284 | |
---|
285 | |
---|
286 | tIterator<StoryEntity>* iterator = this->entities->getIterator(); |
---|
287 | StoryEntity* entity = iterator->firstElement(); |
---|
288 | while( entity != NULL) |
---|
289 | { |
---|
290 | int id = entity->getStoryID(); |
---|
291 | //printf("Campaing::getStoryEntity() - now looping, found entity nr=%i\n", id); |
---|
292 | if(id == storyID) |
---|
293 | { |
---|
294 | //printf("Campaing::getStoryEntity() - yea, this is what we where looking for: %id\n"); |
---|
295 | return entity; |
---|
296 | } |
---|
297 | entity = iterator->nextElement(); |
---|
298 | } |
---|
299 | delete iterator; |
---|
300 | |
---|
301 | |
---|
302 | return NULL; |
---|
303 | } |
---|