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 | #include "game_loader.h" |
---|
19 | #include "campaign.h" |
---|
20 | #include "world.h" |
---|
21 | #include "player.h" |
---|
22 | #include "orxonox.h" |
---|
23 | #include "camera.h" |
---|
24 | #include "command_node.h" |
---|
25 | #include "vector.h" |
---|
26 | #include "factory.h" |
---|
27 | |
---|
28 | #include <string.h> |
---|
29 | |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | GameLoader* GameLoader::singletonRef = 0; |
---|
35 | |
---|
36 | |
---|
37 | GameLoader::GameLoader () |
---|
38 | { |
---|
39 | first = NULL; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | GameLoader::~GameLoader () {} |
---|
44 | |
---|
45 | |
---|
46 | /** |
---|
47 | \brief this class is a singleton class |
---|
48 | \returns an instance of itself |
---|
49 | |
---|
50 | if you are unsure about singleton classes, check the theory out on the internet :) |
---|
51 | */ |
---|
52 | GameLoader* GameLoader::getInstance() |
---|
53 | { |
---|
54 | if(singletonRef == NULL) |
---|
55 | singletonRef = new GameLoader(); |
---|
56 | return singletonRef; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | ErrorMessage GameLoader::init() |
---|
61 | { |
---|
62 | if(this->currentCampaign != NULL) |
---|
63 | this->currentCampaign->init(); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | \brief reads a campaign definition file into a campaign class |
---|
69 | \param filename to be loaded |
---|
70 | \returns the loaded campaign |
---|
71 | |
---|
72 | this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
73 | */ |
---|
74 | ErrorMessage GameLoader::loadCampaign(char* name) |
---|
75 | { |
---|
76 | ErrorMessage errorCode; |
---|
77 | |
---|
78 | this->currentCampaign = this->fileToCampaign(name); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | \brief loads a debug campaign for test purposes only. |
---|
84 | \param the identifier of the campaign. |
---|
85 | \returns error message if not able to do so. |
---|
86 | */ |
---|
87 | ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID) |
---|
88 | { |
---|
89 | switch(campaignID) |
---|
90 | { |
---|
91 | // Debug Level 0: Debug level used to test the base frame work. |
---|
92 | case DEBUG_CAMPAIGN_0: |
---|
93 | { |
---|
94 | Campaign* debugCampaign = new Campaign(); |
---|
95 | |
---|
96 | World* world0 = new World(DEBUG_WORLD_0); |
---|
97 | world0->setNextStoryID(WORLD_ID_1); |
---|
98 | debugCampaign->addEntity(world0, WORLD_ID_0); |
---|
99 | |
---|
100 | World* world1 = new World(DEBUG_WORLD_1); |
---|
101 | world1->setNextStoryID(WORLD_ID_GAMEEND); |
---|
102 | debugCampaign->addEntity(world1, WORLD_ID_1); |
---|
103 | |
---|
104 | this->currentCampaign = debugCampaign; |
---|
105 | break; |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | ErrorMessage GameLoader::start() |
---|
111 | { |
---|
112 | if(this->currentCampaign != NULL) |
---|
113 | this->currentCampaign->start(); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | ErrorMessage GameLoader::stop() |
---|
118 | { |
---|
119 | if(this->currentCampaign != NULL) |
---|
120 | this->currentCampaign->stop(); |
---|
121 | this->currentCampaign = NULL; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | ErrorMessage GameLoader::pause() |
---|
126 | { |
---|
127 | this->isPaused = true; |
---|
128 | if(this->currentCampaign != NULL) |
---|
129 | this->currentCampaign->pause(); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | ErrorMessage GameLoader::resume() |
---|
134 | { |
---|
135 | this->isPaused = false; |
---|
136 | if(this->currentCampaign != NULL) |
---|
137 | this->currentCampaign->resume(); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | \brief release the mem |
---|
142 | */ |
---|
143 | ErrorMessage GameLoader::destroy() |
---|
144 | {} |
---|
145 | |
---|
146 | |
---|
147 | /** |
---|
148 | \brief reads a campaign definition file into a campaign class |
---|
149 | \param filename to be loaded |
---|
150 | \returns the loaded campaign |
---|
151 | |
---|
152 | this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
153 | */ |
---|
154 | Campaign* GameLoader::fileToCampaign(char *name) |
---|
155 | { |
---|
156 | /* do not entirely load the campaign. just the current world |
---|
157 | before start of each world, it has to be initialized so it |
---|
158 | can load everything it needs into memory then. |
---|
159 | */ |
---|
160 | |
---|
161 | if( name == NULL) |
---|
162 | { |
---|
163 | PRINTF(1)("No filename specified for loading"); |
---|
164 | return NULL; |
---|
165 | } |
---|
166 | |
---|
167 | TiXmlDocument* XMLDoc = new TiXmlDocument( name); |
---|
168 | // load the campaign document |
---|
169 | if( !XMLDoc->LoadFile()) |
---|
170 | { |
---|
171 | // report an error |
---|
172 | PRINTF(1)("Error loading XML File: %s @ %d:%d\n", XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
173 | delete XMLDoc; |
---|
174 | return NULL; |
---|
175 | } |
---|
176 | |
---|
177 | // check basic validity |
---|
178 | TiXmlElement* element = XMLDoc->RootElement(); |
---|
179 | assert( element != NULL); |
---|
180 | |
---|
181 | element = element->FirstChildElement( "Campaign"); |
---|
182 | |
---|
183 | if( element == NULL ) |
---|
184 | { |
---|
185 | // report an error |
---|
186 | PRINTF(1)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); |
---|
187 | delete XMLDoc; |
---|
188 | return NULL; |
---|
189 | } |
---|
190 | |
---|
191 | // construct campaign |
---|
192 | Campaign* c = new Campaign( element); |
---|
193 | |
---|
194 | // free the XML data |
---|
195 | delete XMLDoc; |
---|
196 | |
---|
197 | return c; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | /** |
---|
202 | \brief handle keyboard commands |
---|
203 | \param cmd: the command to handle |
---|
204 | \returns true if the command was handled by the system |
---|
205 | */ |
---|
206 | bool GameLoader::worldCommand (Command* cmd) |
---|
207 | { |
---|
208 | if( !strcmp( cmd->cmd, "up_world")) |
---|
209 | { |
---|
210 | if( !cmd->bUp) |
---|
211 | { |
---|
212 | this->nextLevel(); |
---|
213 | } |
---|
214 | return true; |
---|
215 | } |
---|
216 | else if( !strcmp( cmd->cmd, "down_world")) |
---|
217 | { |
---|
218 | if( !cmd->bUp) |
---|
219 | { |
---|
220 | this->previousLevel(); |
---|
221 | } |
---|
222 | return true; |
---|
223 | } |
---|
224 | else if( !strcmp( cmd->cmd, "pause")) |
---|
225 | { |
---|
226 | if( !cmd->bUp) |
---|
227 | { |
---|
228 | if(this->isPaused) |
---|
229 | this->resume(); |
---|
230 | else |
---|
231 | this->pause(); |
---|
232 | } |
---|
233 | return true; |
---|
234 | } |
---|
235 | else if( !strcmp( cmd->cmd, "quit")) |
---|
236 | { |
---|
237 | if( !cmd->bUp) this->stop(); |
---|
238 | return true; |
---|
239 | } |
---|
240 | return false; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | /* |
---|
245 | \brief this changes to the next level |
---|
246 | */ |
---|
247 | void GameLoader::nextLevel() |
---|
248 | { |
---|
249 | if(this->currentCampaign != NULL) |
---|
250 | this->currentCampaign->nextLevel(); |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | /* |
---|
255 | \brief change to the previous level - not implemented |
---|
256 | |
---|
257 | this propably useless |
---|
258 | */ |
---|
259 | void GameLoader::previousLevel() |
---|
260 | { |
---|
261 | if(this->currentCampaign != NULL) |
---|
262 | this->currentCampaign->previousLevel(); |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | /** |
---|
267 | \brief add a Factory to the Factory Q |
---|
268 | \param factory a Factory to be registered |
---|
269 | */ |
---|
270 | void GameLoader::registerFactory( Factory* factory) |
---|
271 | { |
---|
272 | assert( factory != NULL); |
---|
273 | |
---|
274 | printf("Registered factory for '%s'\n", factory->getClassname()); |
---|
275 | |
---|
276 | if( first == NULL) first = factory; |
---|
277 | else first->registerFactory( factory); |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | \brief load a StoryEntity |
---|
282 | \param element a XMLElement containing all the needed info |
---|
283 | */ |
---|
284 | BaseObject* GameLoader::fabricate( TiXmlElement* element) |
---|
285 | { |
---|
286 | if( first == NULL) |
---|
287 | { |
---|
288 | PRINTF(1)("GameLoader does not know any factories, fabricate() failed\n"); |
---|
289 | return NULL; |
---|
290 | } |
---|
291 | |
---|
292 | return first->fabricate( element); |
---|
293 | } |
---|