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 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
19 | |
---|
20 | #include "game_loader.h" |
---|
21 | #include "util/loading/load_param.h" |
---|
22 | |
---|
23 | #include "shell_command.h" |
---|
24 | #include "campaign.h" |
---|
25 | |
---|
26 | #include "util/loading/resource_manager.h" |
---|
27 | |
---|
28 | #include "key_mapper.h" |
---|
29 | |
---|
30 | |
---|
31 | |
---|
32 | SHELL_COMMAND(quit, GameLoader, stop) |
---|
33 | ->describe("quits the game") |
---|
34 | ->setAlias("orxoquit"); |
---|
35 | |
---|
36 | |
---|
37 | GameLoader* GameLoader::singletonRef = NULL; |
---|
38 | |
---|
39 | |
---|
40 | /** |
---|
41 | * simple constructor |
---|
42 | */ |
---|
43 | GameLoader::GameLoader () |
---|
44 | { |
---|
45 | this->setClassID(CL_GAME_LOADER, "GameLoader"); |
---|
46 | this->setName("GameLoader"); |
---|
47 | this->bRun = true; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | /** |
---|
52 | * simple deconstructor |
---|
53 | */ |
---|
54 | GameLoader::~GameLoader () |
---|
55 | { |
---|
56 | if( this->currentCampaign) |
---|
57 | delete this->currentCampaign; |
---|
58 | this->currentCampaign = NULL; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | * initializes the GameLoader |
---|
64 | */ |
---|
65 | ErrorMessage GameLoader::init() |
---|
66 | { |
---|
67 | if(this->currentCampaign != NULL) |
---|
68 | this->currentCampaign->init(); |
---|
69 | |
---|
70 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_PAUSE); |
---|
71 | this->subscribeEvent(ES_ALL, EV_MAIN_QUIT); //< External Quit Event |
---|
72 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_QUIT); |
---|
73 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_NEXT_WORLD); |
---|
74 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD); |
---|
75 | |
---|
76 | return ErrorMessage(); |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * reads a campaign definition file into a campaign class |
---|
82 | * @param fileName to be loaded |
---|
83 | * @returns the loaded campaign |
---|
84 | * |
---|
85 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
86 | */ |
---|
87 | ErrorMessage GameLoader::loadCampaign(const std::string& fileName) |
---|
88 | { |
---|
89 | ErrorMessage errorCode; |
---|
90 | std::string campaignName = ResourceManager::getFullName(fileName); |
---|
91 | if (!campaignName.empty()) |
---|
92 | { |
---|
93 | this->currentCampaign = this->fileToCampaign(campaignName); |
---|
94 | } |
---|
95 | |
---|
96 | return ErrorMessage(); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | * reads a campaign definition file into a campaign class |
---|
102 | * @param fileName to be loaded |
---|
103 | * @returns the loaded campaign |
---|
104 | * |
---|
105 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
106 | */ |
---|
107 | ErrorMessage GameLoader::loadNetworkCampaign(const std::string& fileName) |
---|
108 | { |
---|
109 | ErrorMessage errorCode; |
---|
110 | std::string campaignName = ResourceManager::getFullName(fileName); |
---|
111 | if (!campaignName.empty()) |
---|
112 | { |
---|
113 | this->currentCampaign = this->fileToCampaign(campaignName); |
---|
114 | } |
---|
115 | |
---|
116 | return ErrorMessage(); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | /** |
---|
121 | * loads a debug campaign for test purposes only. |
---|
122 | * @param campaignID the identifier of the campaign. |
---|
123 | * @returns error message if not able to do so. |
---|
124 | */ |
---|
125 | ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID) |
---|
126 | { |
---|
127 | switch(campaignID) |
---|
128 | { |
---|
129 | /* |
---|
130 | Debug Level 0: Debug level used to test the base frame work. |
---|
131 | As you can see, all storyentity data is allocated before game |
---|
132 | start. the storyentity will load themselfs shortly before start |
---|
133 | through the StoryEntity::init() funtion. |
---|
134 | */ |
---|
135 | case DEBUG_CAMPAIGN_0: |
---|
136 | { |
---|
137 | /* Campaign* debugCampaign = new Campaign(); |
---|
138 | |
---|
139 | World* world0 = new World(DEBUG_WORLD_0); |
---|
140 | world0->setNextStoryID(WORLD_ID_1); |
---|
141 | debugCampaign->addEntity(world0, WORLD_ID_0); |
---|
142 | |
---|
143 | World* world1 = new World(DEBUG_WORLD_1); |
---|
144 | world1->setNextStoryID(WORLD_ID_2); |
---|
145 | debugCampaign->addEntity(world1, WORLD_ID_1); |
---|
146 | |
---|
147 | World* world2 = new World(DEBUG_WORLD_2); |
---|
148 | world2->setNextStoryID(WORLD_ID_GAMEEND); |
---|
149 | debugCampaign->addEntity(world2, WORLD_ID_2); |
---|
150 | |
---|
151 | this->currentCampaign = debugCampaign; |
---|
152 | break;*/ |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | return ErrorMessage(); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * starts the current entity |
---|
162 | * @returns error code if this action has caused a error |
---|
163 | */ |
---|
164 | ErrorMessage GameLoader::start() |
---|
165 | { |
---|
166 | if(this->currentCampaign != NULL) |
---|
167 | { |
---|
168 | this->currentCampaign->start(); |
---|
169 | } |
---|
170 | |
---|
171 | return ErrorMessage(); |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | /** |
---|
176 | * stops the current entity |
---|
177 | * @returns error code if this action has caused a error |
---|
178 | * |
---|
179 | * ATTENTION: this function shouldn't call other functions, or if so, they must return |
---|
180 | * after finishing. If you ignore or forget to do so, the current entity is not able to |
---|
181 | * terminate and it will run in the background or the ressources can't be freed or even |
---|
182 | * worse: are freed and the program will end in a segmentation fault! |
---|
183 | * hehehe, have ya seen it... :) |
---|
184 | */ |
---|
185 | void GameLoader::stop() |
---|
186 | { |
---|
187 | if(this->currentCampaign != NULL) |
---|
188 | this->currentCampaign->stop(); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | /** |
---|
193 | * pause the current entity |
---|
194 | * @returns error code if this action has caused a error |
---|
195 | * |
---|
196 | * this pauses the current entity or passes this call forth to the running entity. |
---|
197 | */ |
---|
198 | ErrorMessage GameLoader::pause() |
---|
199 | { |
---|
200 | this->isPaused = true; |
---|
201 | if(this->currentCampaign != NULL) |
---|
202 | this->currentCampaign->pause(); |
---|
203 | |
---|
204 | return ErrorMessage(); |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | /** |
---|
209 | * resumes a pause |
---|
210 | * @returns error code if this action has caused a error |
---|
211 | * |
---|
212 | * this resumess the current entity or passes this call forth to the running entity. |
---|
213 | */ |
---|
214 | ErrorMessage GameLoader::resume() |
---|
215 | { |
---|
216 | this->isPaused = false; |
---|
217 | if(this->currentCampaign != NULL) |
---|
218 | this->currentCampaign->resume(); |
---|
219 | |
---|
220 | return ErrorMessage(); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | /** |
---|
225 | * reads a campaign definition file into a campaign class |
---|
226 | * @param fileName to be loaded |
---|
227 | * @returns the loaded campaign |
---|
228 | * |
---|
229 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
230 | */ |
---|
231 | Campaign* GameLoader::fileToCampaign(const std::string& fileName) |
---|
232 | { |
---|
233 | /* do not entirely load the campaign. just the current world |
---|
234 | before start of each world, it has to be initialized so it |
---|
235 | can load everything it needs into memory then. |
---|
236 | */ |
---|
237 | |
---|
238 | if( fileName.empty()) |
---|
239 | { |
---|
240 | PRINTF(2)("No filename specified for loading"); |
---|
241 | return NULL; |
---|
242 | } |
---|
243 | |
---|
244 | TiXmlDocument XMLDoc(fileName); |
---|
245 | // load the campaign document |
---|
246 | if( !XMLDoc.LoadFile(fileName)) |
---|
247 | { |
---|
248 | // report an error |
---|
249 | PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName.c_str(), XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol()); |
---|
250 | return NULL; |
---|
251 | } |
---|
252 | |
---|
253 | // check basic validity |
---|
254 | TiXmlElement* root = XMLDoc.RootElement(); |
---|
255 | assert( root != NULL); |
---|
256 | |
---|
257 | if( strcmp( root->Value(), "Campaign")) |
---|
258 | { |
---|
259 | // report an error |
---|
260 | PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); |
---|
261 | return NULL; |
---|
262 | } |
---|
263 | |
---|
264 | // construct campaign |
---|
265 | return new Campaign( root); |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | /** |
---|
271 | * handle keyboard commands |
---|
272 | * @param event the event to handle |
---|
273 | */ |
---|
274 | void GameLoader::process(const Event& event) |
---|
275 | { |
---|
276 | if( event.type == KeyMapper::PEV_NEXT_WORLD) |
---|
277 | { |
---|
278 | if( likely(event.bPressed)) |
---|
279 | { |
---|
280 | this->switchToNextLevel(); |
---|
281 | } |
---|
282 | } |
---|
283 | else if( event.type == KeyMapper::PEV_PAUSE) |
---|
284 | { |
---|
285 | if( likely(event.bPressed)) |
---|
286 | { |
---|
287 | if(this->isPaused) |
---|
288 | this->resume(); |
---|
289 | else |
---|
290 | this->pause(); |
---|
291 | } |
---|
292 | } |
---|
293 | else if( event.type == KeyMapper::PEV_QUIT) |
---|
294 | { |
---|
295 | if( event.bPressed) this->stop(); |
---|
296 | } |
---|
297 | else if (event.type == EV_MAIN_QUIT) |
---|
298 | this->stop(); |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | /** |
---|
303 | * this changes to the next level |
---|
304 | */ |
---|
305 | void GameLoader::switchToNextLevel() |
---|
306 | { |
---|
307 | if(this->currentCampaign != NULL) |
---|
308 | this->currentCampaign->switchToNextLevel(); |
---|
309 | } |
---|
310 | |
---|