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