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 "util/loading/resource_manager.h" |
---|
24 | #include "state.h" |
---|
25 | #include "class_list.h" |
---|
26 | #include "substring.h" |
---|
27 | |
---|
28 | #include "util/loading/game_loader.h" |
---|
29 | |
---|
30 | #include "world_entity.h" |
---|
31 | #include "player.h" |
---|
32 | #include "camera.h" |
---|
33 | #include "terrain.h" |
---|
34 | #include "skybox.h" |
---|
35 | #include "md2Model.h" |
---|
36 | #include "world_entities/projectiles/projectile.h" |
---|
37 | #include "npcs/npc_test1.h" |
---|
38 | #include "playable.h" |
---|
39 | |
---|
40 | #include "light.h" |
---|
41 | |
---|
42 | #include "util/loading/factory.h" |
---|
43 | #include "fast_factory.h" |
---|
44 | #include "util/loading/load_param.h" |
---|
45 | |
---|
46 | #include "graphics_engine.h" |
---|
47 | #include "event_handler.h" |
---|
48 | #include "sound_engine.h" |
---|
49 | #include "cd_engine.h" |
---|
50 | #include "network_manager.h" |
---|
51 | #include "physics_engine.h" |
---|
52 | #include "fields.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 | using namespace std; |
---|
63 | |
---|
64 | |
---|
65 | /** |
---|
66 | * constructor of the GameWorldData |
---|
67 | */ |
---|
68 | GameWorldData::GameWorldData() |
---|
69 | { |
---|
70 | this->setClassID(CL_GAME_WORLD_DATA, "GameWorldData"); |
---|
71 | |
---|
72 | this->glmis = NULL; |
---|
73 | |
---|
74 | this->localCamera = NULL; |
---|
75 | this->localPlayer = NULL; |
---|
76 | this->sky = NULL; |
---|
77 | this->terrain = NULL; |
---|
78 | |
---|
79 | this->music = NULL; |
---|
80 | this->objectManager = NULL; |
---|
81 | this->gameRule = NULL; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /** |
---|
86 | * destructor for the GameWorldData |
---|
87 | */ |
---|
88 | GameWorldData::~GameWorldData() |
---|
89 | {} |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | /** |
---|
94 | * initialize the GameWorldData |
---|
95 | */ |
---|
96 | ErrorMessage GameWorldData::init() |
---|
97 | { |
---|
98 | this->objectManager = new ObjectManager(); |
---|
99 | State::setObjectManager(this->objectManager); |
---|
100 | |
---|
101 | PNode::getNullParent(); |
---|
102 | this->localCamera = new Camera(); |
---|
103 | this->localCamera->setName ("GameWorld-Camera"); |
---|
104 | State::setCamera(this->localCamera, this->localCamera->getTarget()); |
---|
105 | |
---|
106 | LightManager::getInstance(); |
---|
107 | |
---|
108 | GraphicsEngine::getInstance()->displayFPS(true); |
---|
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 | // load the parameters |
---|
119 | // name |
---|
120 | std::string string = grabParameter( root, "name"); |
---|
121 | if( string.empty() ) |
---|
122 | { |
---|
123 | PRINTF(2)("GameWorld is missing a proper 'name'\n"); |
---|
124 | this->setName("Unknown"); |
---|
125 | } |
---|
126 | else |
---|
127 | this->setName(string.c_str()); |
---|
128 | |
---|
129 | this->loadGUI(root); |
---|
130 | this->loadWorldEntities(root); |
---|
131 | this->loadScene(root); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
136 | * unloads the data from the xml file |
---|
137 | */ |
---|
138 | ErrorMessage GameWorldData::unloadData() |
---|
139 | { |
---|
140 | this->unloadGUI(); |
---|
141 | this->unloadWorldEntities(); |
---|
142 | this->unloadScene(); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | /** |
---|
147 | * @brief loads the GUI data |
---|
148 | * @param root reference to the xml root element |
---|
149 | */ |
---|
150 | ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root) |
---|
151 | { |
---|
152 | const TiXmlElement* element = root->FirstChildElement("LoadScreen"); |
---|
153 | if( element == NULL) |
---|
154 | { |
---|
155 | PRINTF(2)("no LoadScreen specified, loading default\n"); |
---|
156 | |
---|
157 | glmis->setBackgroundImage("pictures/load_screen.jpg"); |
---|
158 | this->glmis->setMaximum(8); |
---|
159 | // this->glmis->draw(); |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | this->glmis->loadParams(element); |
---|
164 | // this->glmis->draw(); |
---|
165 | } |
---|
166 | this->glmis->draw(); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | /** |
---|
171 | * @brief unloads the GUI data |
---|
172 | */ |
---|
173 | ErrorMessage GameWorldData::unloadGUI() |
---|
174 | { |
---|
175 | delete this->glmis; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | /** |
---|
180 | * @brief loads the world entities from the xml file |
---|
181 | * @param root reference to the xml root parameter |
---|
182 | */ |
---|
183 | ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root) |
---|
184 | { |
---|
185 | const TiXmlElement* element = root->FirstChildElement("WorldEntities"); |
---|
186 | |
---|
187 | if( element == NULL) |
---|
188 | { |
---|
189 | PRINTF(1)("GameWorld is missing 'WorldEntities'\n"); |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | element = element->FirstChildElement(); |
---|
194 | // load Players/Objects/Whatever |
---|
195 | PRINTF(4)("Loading WorldEntities\n"); |
---|
196 | while( element != NULL) |
---|
197 | { |
---|
198 | BaseObject* created = Factory::fabricate(element); |
---|
199 | if( created != NULL ) |
---|
200 | PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName()); |
---|
201 | |
---|
202 | //todo do this more elegant |
---|
203 | if( element->Value() == "SkyBox" && created->isA(CL_SKYBOX)) |
---|
204 | { |
---|
205 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
206 | State::setSkyBox(dynamic_cast<SkyBox*>(this->sky)); |
---|
207 | } |
---|
208 | if( element->Value() == "Terrain" && created->isA(CL_TERRAIN)) |
---|
209 | { |
---|
210 | this->terrain = dynamic_cast<Terrain*>(created); |
---|
211 | CDEngine::getInstance()->setTerrain(terrain); |
---|
212 | } |
---|
213 | element = element->NextSiblingElement(); |
---|
214 | this->glmis->step(); //! @todo temporary |
---|
215 | } |
---|
216 | PRINTF(4)("Done loading WorldEntities\n"); |
---|
217 | } |
---|
218 | |
---|
219 | // Create a Player |
---|
220 | this->localPlayer = new Player(); |
---|
221 | State::setPlayer(this->localPlayer); |
---|
222 | |
---|
223 | Playable* playable; |
---|
224 | const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE); |
---|
225 | if (playableList != NULL && !playableList->empty()) |
---|
226 | { |
---|
227 | /// TODO Make this also loadable |
---|
228 | playable = dynamic_cast<Playable*>(playableList->front()); |
---|
229 | this->localPlayer->setPlayable(playable); |
---|
230 | } |
---|
231 | |
---|
232 | // Fill the EntityLists. Tick then Draw: |
---|
233 | this->tickLists.push_back(OM_DEAD_TICK); |
---|
234 | this->tickLists.push_back(OM_ENVIRON); |
---|
235 | this->tickLists.push_back(OM_COMMON); |
---|
236 | this->tickLists.push_back(OM_GROUP_00); |
---|
237 | this->tickLists.push_back(OM_GROUP_00_PROJ); |
---|
238 | this->tickLists.push_back(OM_GROUP_01); |
---|
239 | this->tickLists.push_back(OM_GROUP_01_PROJ); |
---|
240 | |
---|
241 | this->drawLists.push_back(OM_ENVIRON_NOTICK); |
---|
242 | this->drawLists.push_back(OM_ENVIRON); |
---|
243 | this->drawLists.push_back(OM_COMMON); |
---|
244 | this->drawLists.push_back(OM_GROUP_00); |
---|
245 | this->drawLists.push_back(OM_GROUP_00_PROJ); |
---|
246 | this->drawLists.push_back(OM_GROUP_01); |
---|
247 | this->drawLists.push_back(OM_GROUP_01_PROJ); |
---|
248 | |
---|
249 | /* init the pnode tree */ |
---|
250 | PNode::getNullParent()->init(); |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | /** |
---|
255 | * unloads the world entities |
---|
256 | */ |
---|
257 | ErrorMessage GameWorldData::unloadWorldEntities() |
---|
258 | { |
---|
259 | FastFactory::flushAll(true); |
---|
260 | GraphicsEngine::getInstance()->displayFPS(false); |
---|
261 | // erease everything that is left. |
---|
262 | // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner) |
---|
263 | const std::list<BaseObject*>* nodeList; |
---|
264 | //secondary cleanup of PNodes; |
---|
265 | nodeList = ClassList::getList(CL_PARENT_NODE); |
---|
266 | if (nodeList != NULL) |
---|
267 | while (!nodeList->empty()) |
---|
268 | { |
---|
269 | // ClassList::debug( 3, CL_PARENT_NODE); |
---|
270 | // PNode::getNullParent()->debugNode(0); |
---|
271 | // printf("%s::%s\n", nodeList->front()->getClassName(), nodeList->front()->getName()); |
---|
272 | delete nodeList->front(); |
---|
273 | } |
---|
274 | /* remove the player object */ |
---|
275 | if( this->localPlayer) |
---|
276 | delete this->localPlayer; |
---|
277 | State::setPlayer(NULL); |
---|
278 | this->localPlayer = NULL; |
---|
279 | this->localCamera = NULL; |
---|
280 | State::setCamera(NULL, NULL); |
---|
281 | this->sky = NULL; |
---|
282 | this->terrain = NULL; |
---|
283 | |
---|
284 | nodeList = ClassList::getList(CL_GRAPHICS_EFFECT); |
---|
285 | if (nodeList != NULL) |
---|
286 | while (!nodeList->empty()) |
---|
287 | delete nodeList->front(); |
---|
288 | |
---|
289 | |
---|
290 | nodeList = ClassList::getList(CL_ELEMENT_2D); |
---|
291 | if (nodeList != NULL) |
---|
292 | while (!nodeList->empty()) |
---|
293 | delete nodeList->front(); |
---|
294 | |
---|
295 | // At this Point all the WorldEntites should be unloaded. |
---|
296 | this->tickLists.clear(); |
---|
297 | this->drawLists.clear(); |
---|
298 | |
---|
299 | // unload the resources loaded in this Level !! |
---|
300 | ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
301 | |
---|
302 | if (State::getObjectManager() == this->objectManager) |
---|
303 | { |
---|
304 | State::setObjectManager(NULL); |
---|
305 | delete this->objectManager; |
---|
306 | } |
---|
307 | this->objectManager = NULL; |
---|
308 | |
---|
309 | if(State::getSkyBox()) |
---|
310 | State::setSkyBox(NULL); |
---|
311 | |
---|
312 | this->glmis = NULL; |
---|
313 | } |
---|
314 | |
---|
315 | |
---|
316 | /** |
---|
317 | * @brief loads the scene data |
---|
318 | * @param root reference to the xml root element |
---|
319 | */ |
---|
320 | ErrorMessage GameWorldData::loadScene(const TiXmlElement* root) |
---|
321 | { |
---|
322 | LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams); |
---|
323 | LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams); |
---|
324 | |
---|
325 | LoadParam(root, "Music", this, GameWorldData, setSoundTrack); |
---|
326 | |
---|
327 | |
---|
328 | LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule); |
---|
329 | |
---|
330 | |
---|
331 | //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams); |
---|
332 | //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams); |
---|
333 | |
---|
334 | this->localCamera->setClipRegion(1, 10000.0); |
---|
335 | if( this->sky != NULL) |
---|
336 | this->localCamera->addChild(this->sky); |
---|
337 | SoundEngine::getInstance()->setListener(this->localCamera); |
---|
338 | } |
---|
339 | |
---|
340 | |
---|
341 | |
---|
342 | /** |
---|
343 | * unloads the scene data |
---|
344 | */ |
---|
345 | ErrorMessage GameWorldData::unloadScene() |
---|
346 | { |
---|
347 | /* delete some garphics and scene eingines */ |
---|
348 | delete LightManager::getInstance(); |
---|
349 | |
---|
350 | if (this->music != NULL) |
---|
351 | this->setSoundTrack(""); |
---|
352 | |
---|
353 | /* unload the shaders */ |
---|
354 | Shader::suspendShader(); |
---|
355 | } |
---|
356 | |
---|
357 | |
---|
358 | void GameWorldData::setSoundTrack(const std::string& name) |
---|
359 | { |
---|
360 | if (this->music != NULL) |
---|
361 | delete this->music; |
---|
362 | this->music = NULL; |
---|
363 | |
---|
364 | if (!name.empty()) |
---|
365 | { |
---|
366 | PRINTF(3)("Setting Sound Track to %s\n", name.c_str()); |
---|
367 | std::string oggFile = ResourceManager::getFullName(name); |
---|
368 | this->music = new OggPlayer(oggFile); |
---|
369 | |
---|
370 | //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL); |
---|
371 | //assert(this->music->isA(CL_SOUND_OGG_PLAYER)); |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | void GameWorldData::loadGameRule(const TiXmlElement* root) |
---|
377 | { |
---|
378 | const TiXmlElement* element = root->FirstChildElement(); |
---|
379 | while( element != NULL) |
---|
380 | { |
---|
381 | PRINTF(4)("creating %s\n", element->Value()); |
---|
382 | BaseObject* created = Factory::fabricate(element); |
---|
383 | if (created != NULL /*&& created->isA(CL_GAME_RULES)*/) |
---|
384 | { |
---|
385 | this->gameRule = dynamic_cast<GameRules*>(created); |
---|
386 | State::setGameRules(this->gameRule); |
---|
387 | } |
---|
388 | else |
---|
389 | { |
---|
390 | PRINTF(1)("Could not create a %s\n", element->Value()); |
---|
391 | delete created; |
---|
392 | } |
---|
393 | element = element->NextSiblingElement(); |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | |
---|
398 | |
---|