1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
17 | |
---|
18 | #include <fstream> |
---|
19 | #include <iostream> |
---|
20 | |
---|
21 | #include "game_world_data.h" |
---|
22 | |
---|
23 | #include "state.h" |
---|
24 | #include "substring.h" |
---|
25 | |
---|
26 | #include "util/loading/game_loader.h" |
---|
27 | #include "util/loading/resource_manager.h" |
---|
28 | |
---|
29 | #include "world_entity.h" |
---|
30 | #include "player.h" |
---|
31 | #include "camera.h" |
---|
32 | #include "terrain.h" |
---|
33 | #include "skybox.h" |
---|
34 | #include "md2/md2Model.h" |
---|
35 | #include "world_entities/projectiles/projectile.h" |
---|
36 | #include "npcs/npc_test1.h" |
---|
37 | #include "playable.h" |
---|
38 | |
---|
39 | #include "light.h" |
---|
40 | |
---|
41 | #include "util/loading/factory.h" |
---|
42 | #include "loading/fast_factory.h" |
---|
43 | #include "util/loading/load_param_xml.h" |
---|
44 | |
---|
45 | #include "graphics_engine.h" |
---|
46 | #include "graphics_effect.h" |
---|
47 | #include "weather_effects/atmospheric_engine.h" |
---|
48 | #include "event_handler.h" |
---|
49 | #include "sound_engine.h" |
---|
50 | #include "cd_engine.h" |
---|
51 | #include "network_manager.h" |
---|
52 | #include "physics_engine.h" |
---|
53 | |
---|
54 | #include "glmenu_imagescreen.h" |
---|
55 | |
---|
56 | #include "game_rules.h" |
---|
57 | |
---|
58 | #include "ogg_player.h" |
---|
59 | #include "shader.h" |
---|
60 | |
---|
61 | |
---|
62 | ObjectListDefinition(GameWorldData); |
---|
63 | /** |
---|
64 | * constructor of the GameWorldData |
---|
65 | */ |
---|
66 | GameWorldData::GameWorldData() |
---|
67 | { |
---|
68 | this->registerObject(this, GameWorldData::_objectList); |
---|
69 | |
---|
70 | this->glmis = NULL; |
---|
71 | |
---|
72 | this->localCamera = NULL; |
---|
73 | this->localPlayer = NULL; |
---|
74 | this->sky = NULL; |
---|
75 | this->terrain = NULL; |
---|
76 | |
---|
77 | this->music = NULL; |
---|
78 | this->objectManager = NULL; |
---|
79 | this->gameRule = NULL; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | * destructor for the GameWorldData |
---|
85 | */ |
---|
86 | GameWorldData::~GameWorldData() |
---|
87 | {} |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | /** |
---|
92 | * initialize the GameWorldData |
---|
93 | */ |
---|
94 | ErrorMessage GameWorldData::init() |
---|
95 | { |
---|
96 | this->objectManager = new ObjectManager(); |
---|
97 | State::setObjectManager(this->objectManager); |
---|
98 | |
---|
99 | PNode::getNullParent(); |
---|
100 | this->localCamera = new Camera(); |
---|
101 | this->localCamera->setName ("GameWorld-Camera"); |
---|
102 | State::setCamera(this->localCamera, this->localCamera->getTarget()); |
---|
103 | |
---|
104 | LightManager::getInstance(); |
---|
105 | |
---|
106 | // GraphicsEngine::getInstance()->displayFPS(true); |
---|
107 | |
---|
108 | return ErrorMessage(); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | /** |
---|
113 | * loads the data from the xml file |
---|
114 | * @param root reference to the xml root element |
---|
115 | */ |
---|
116 | ErrorMessage GameWorldData::loadData(const TiXmlElement* root) |
---|
117 | { |
---|
118 | assert (root != NULL); |
---|
119 | // load the parameters |
---|
120 | // name |
---|
121 | std::string string = LoadParamBase::grabParameter( root, "name"); |
---|
122 | if( string.empty() ) |
---|
123 | { |
---|
124 | PRINTF(2)("GameWorld is missing a proper 'name'\n"); |
---|
125 | this->setName("Unknown"); |
---|
126 | } |
---|
127 | else |
---|
128 | this->setName(string.c_str()); |
---|
129 | |
---|
130 | this->loadGUI(root); |
---|
131 | this->loadWorldEntities(root); |
---|
132 | this->loadScene(root); |
---|
133 | |
---|
134 | return ErrorMessage(); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | /** |
---|
139 | * unloads the data from the xml file |
---|
140 | */ |
---|
141 | ErrorMessage GameWorldData::unloadData() |
---|
142 | { |
---|
143 | this->unloadGUI(); |
---|
144 | this->unloadWorldEntities(); |
---|
145 | this->unloadScene(); |
---|
146 | |
---|
147 | return ErrorMessage(); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | /** |
---|
152 | * @brief loads the GUI data |
---|
153 | * @param root reference to the xml root element |
---|
154 | */ |
---|
155 | ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root) |
---|
156 | { |
---|
157 | const TiXmlElement* element = root->FirstChildElement("LoadScreen"); |
---|
158 | if( element == NULL) |
---|
159 | { |
---|
160 | PRINTF(2)("no LoadScreen specified, loading default\n"); |
---|
161 | |
---|
162 | glmis->setBackgroundImage("textures/load_screens/default.jpg"); |
---|
163 | this->glmis->setMaximum(8); |
---|
164 | // this->glmis->draw(); |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | this->glmis->loadParams(element); |
---|
169 | // this->glmis->draw(); |
---|
170 | } |
---|
171 | this->glmis->draw(); |
---|
172 | |
---|
173 | return ErrorMessage(); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | /** |
---|
178 | * @brief unloads the GUI data |
---|
179 | */ |
---|
180 | ErrorMessage GameWorldData::unloadGUI() |
---|
181 | { |
---|
182 | delete this->glmis; |
---|
183 | |
---|
184 | return ErrorMessage(); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | /** |
---|
189 | * @brief loads the world entities from the xml file |
---|
190 | * @param root reference to the xml root parameter |
---|
191 | */ |
---|
192 | ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root) |
---|
193 | { |
---|
194 | const TiXmlElement* element = root->FirstChildElement("WorldEntities"); |
---|
195 | bool mouseCaptured = EventHandler::getInstance()->grabbedEvents(); |
---|
196 | EventHandler::getInstance()->grabEvents(false); |
---|
197 | |
---|
198 | if( element == NULL) |
---|
199 | { |
---|
200 | PRINTF(1)("GameWorld is missing 'WorldEntities'\n"); |
---|
201 | } |
---|
202 | else |
---|
203 | { |
---|
204 | element = element->FirstChildElement(); |
---|
205 | // load Players/Objects/Whatever |
---|
206 | PRINTF(4)("Loading WorldEntities\n"); |
---|
207 | while( element != NULL) |
---|
208 | { |
---|
209 | BaseObject* created = Factory::fabricate(element); |
---|
210 | if( created != NULL ) |
---|
211 | PRINTF(4)("Created a %s: %s\n", created->getClassCName(), created->getCName()); |
---|
212 | |
---|
213 | //todo do this more elegant |
---|
214 | if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID())) |
---|
215 | { |
---|
216 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
217 | State::setSkyBox(dynamic_cast<SkyBox*>(this->sky)); |
---|
218 | } |
---|
219 | if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID())) |
---|
220 | { |
---|
221 | this->terrain = dynamic_cast<Terrain*>(created); |
---|
222 | CDEngine::getInstance()->setTerrain(terrain); |
---|
223 | } |
---|
224 | element = element->NextSiblingElement(); |
---|
225 | this->glmis->step(); //! @todo temporary |
---|
226 | } |
---|
227 | PRINTF(4)("Done loading WorldEntities\n"); |
---|
228 | } |
---|
229 | |
---|
230 | // Create a Player |
---|
231 | this->localPlayer = new Player(); |
---|
232 | State::setPlayer(this->localPlayer); |
---|
233 | |
---|
234 | if (!Playable::objectList().empty()) |
---|
235 | { |
---|
236 | /// TODO Make this also loadable |
---|
237 | this->localPlayer->setPlayable(Playable::objectList().front()); |
---|
238 | } |
---|
239 | |
---|
240 | // Fill the EntityLists. Tick then Draw: |
---|
241 | this->tickLists.push_back(OM_DEAD_TICK); |
---|
242 | this->tickLists.push_back(OM_ENVIRON); |
---|
243 | this->tickLists.push_back(OM_COMMON); |
---|
244 | this->tickLists.push_back(OM_GROUP_00); |
---|
245 | this->tickLists.push_back(OM_GROUP_00_PROJ); |
---|
246 | this->tickLists.push_back(OM_GROUP_01); |
---|
247 | this->tickLists.push_back(OM_GROUP_01_PROJ); |
---|
248 | this->tickLists.push_back(OM_GROUP_02); |
---|
249 | |
---|
250 | this->drawLists.push_back(OM_ENVIRON_NOTICK); |
---|
251 | this->drawLists.push_back(OM_ENVIRON); |
---|
252 | this->drawLists.push_back(OM_GROUP_00); |
---|
253 | this->drawLists.push_back(OM_GROUP_00_PROJ); |
---|
254 | this->drawLists.push_back(OM_GROUP_01); |
---|
255 | this->drawLists.push_back(OM_GROUP_01_PROJ); |
---|
256 | this->drawLists.push_back(OM_GROUP_02); |
---|
257 | this->drawLists.push_back(OM_COMMON); |
---|
258 | |
---|
259 | /* init the pnode tree */ |
---|
260 | PNode::getNullParent()->init(); |
---|
261 | |
---|
262 | EventHandler::getInstance()->grabEvents(mouseCaptured); |
---|
263 | |
---|
264 | return ErrorMessage(); |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | /** |
---|
269 | * unloads the world entities |
---|
270 | */ |
---|
271 | ErrorMessage GameWorldData::unloadWorldEntities() |
---|
272 | { |
---|
273 | FastFactory::flushAll(true); |
---|
274 | GraphicsEngine::getInstance()->displayFPS(false); |
---|
275 | // erease everything that is left. |
---|
276 | //secondary cleanup of PNodes; |
---|
277 | while (!PNode::objectList().empty()) |
---|
278 | delete PNode::objectList().front(); |
---|
279 | |
---|
280 | /* remove the player object */ |
---|
281 | if( this->localPlayer) |
---|
282 | delete this->localPlayer; |
---|
283 | State::setPlayer(NULL); |
---|
284 | this->localPlayer = NULL; |
---|
285 | this->localCamera = NULL; |
---|
286 | State::setCamera(NULL, NULL); |
---|
287 | this->sky = NULL; |
---|
288 | this->terrain = NULL; |
---|
289 | |
---|
290 | while (!GraphicsEffect::objectList().empty()) |
---|
291 | delete GraphicsEffect::objectList().front(); |
---|
292 | |
---|
293 | |
---|
294 | while (!Element2D::objectList().empty()) |
---|
295 | delete Element2D::objectList().front(); |
---|
296 | |
---|
297 | // At this Point all the WorldEntites should be unloaded. |
---|
298 | this->tickLists.clear(); |
---|
299 | this->drawLists.clear(); |
---|
300 | |
---|
301 | // unload the resources loaded in this Level !! |
---|
302 | /// FIXME TODO HACK!! |
---|
303 | // ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
304 | |
---|
305 | if (State::getObjectManager() == this->objectManager) |
---|
306 | { |
---|
307 | State::setObjectManager(NULL); |
---|
308 | delete this->objectManager; |
---|
309 | } |
---|
310 | this->objectManager = NULL; |
---|
311 | |
---|
312 | if(State::getSkyBox()) |
---|
313 | State::setSkyBox(NULL); |
---|
314 | |
---|
315 | this->glmis = NULL; |
---|
316 | |
---|
317 | return ErrorMessage(); |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | /** |
---|
322 | * @brief loads the scene data |
---|
323 | * @param root reference to the xml root element |
---|
324 | */ |
---|
325 | ErrorMessage GameWorldData::loadScene(const TiXmlElement* root) |
---|
326 | { |
---|
327 | LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams); |
---|
328 | LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams); |
---|
329 | LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams); |
---|
330 | |
---|
331 | LoadParam(root, "Music", this, GameWorldData, setSoundTrack); |
---|
332 | |
---|
333 | LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule); |
---|
334 | |
---|
335 | LoadParam(root, "clip-region", this->localCamera, Camera, setClipRegion); |
---|
336 | |
---|
337 | |
---|
338 | //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams); |
---|
339 | //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams); |
---|
340 | |
---|
341 | if( this->sky != NULL) |
---|
342 | this->localCamera->addChild(this->sky); |
---|
343 | OrxSound::SoundEngine::getInstance()->setListener(this->localCamera); |
---|
344 | |
---|
345 | return ErrorMessage(); |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | |
---|
350 | /** |
---|
351 | * unloads the scene data |
---|
352 | */ |
---|
353 | ErrorMessage GameWorldData::unloadScene() |
---|
354 | { |
---|
355 | /* delete some garphics and scene eingines */ |
---|
356 | delete LightManager::getInstance(); |
---|
357 | delete AtmosphericEngine::getInstance(); |
---|
358 | |
---|
359 | if (this->music != NULL) |
---|
360 | this->setSoundTrack(""); |
---|
361 | |
---|
362 | /* unload the shaders */ |
---|
363 | Shader::suspendShader(); |
---|
364 | |
---|
365 | State::setGameRules(NULL); |
---|
366 | |
---|
367 | return ErrorMessage(); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | void GameWorldData::setSoundTrack(const std::string& name) |
---|
372 | { |
---|
373 | if (this->music != NULL) |
---|
374 | delete this->music; |
---|
375 | this->music = NULL; |
---|
376 | |
---|
377 | if (!name.empty()) |
---|
378 | { |
---|
379 | PRINTF(3)("Setting Sound Track to %s\n", name.c_str()); |
---|
380 | std::string oggFile = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(name); |
---|
381 | this->music = new OrxSound::OggPlayer(oggFile); |
---|
382 | if (this->localPlayer != NULL) |
---|
383 | this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title()); |
---|
384 | |
---|
385 | //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL); |
---|
386 | //assert(this->music->isA(CL_SOUND_OGG_PLAYER)); |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | void GameWorldData::loadGameRule(const TiXmlElement* root) |
---|
392 | { |
---|
393 | const TiXmlElement* element = root->FirstChildElement(); |
---|
394 | while( element != NULL) |
---|
395 | { |
---|
396 | PRINTF(2)("============ GameRules ==\n"); |
---|
397 | PRINTF(2)("creating %s\n", element->Value()); |
---|
398 | BaseObject* created = Factory::fabricate(element); |
---|
399 | if (created != NULL && created->isA(GameRules::staticClassID())) |
---|
400 | { |
---|
401 | this->gameRule = dynamic_cast<GameRules*>(created); |
---|
402 | State::setGameRules(this->gameRule); |
---|
403 | // if there is a valid game rule loaded, return because it is not thought to load multiple game rules |
---|
404 | return; |
---|
405 | } |
---|
406 | else |
---|
407 | { |
---|
408 | PRINTF(1)("Could not create a %s\n", element->Value()); |
---|
409 | delete created; |
---|
410 | } |
---|
411 | element = element->NextSiblingElement(); |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | |
---|