[2636] | 1 | |
---|
| 2 | |
---|
[4597] | 3 | /* |
---|
[2636] | 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" |
---|
[5139] | 19 | |
---|
| 20 | #include "shell_command.h" |
---|
[2636] | 21 | #include "campaign.h" |
---|
| 22 | #include "world.h" |
---|
| 23 | #include "orxonox.h" |
---|
| 24 | #include "camera.h" |
---|
| 25 | #include "vector.h" |
---|
[4094] | 26 | #include "resource_manager.h" |
---|
[4010] | 27 | #include "factory.h" |
---|
[4410] | 28 | #include "event.h" |
---|
| 29 | #include "event_handler.h" |
---|
[2636] | 30 | #include <string.h> |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | using namespace std; |
---|
| 34 | |
---|
| 35 | |
---|
[5162] | 36 | SHELL_COMMAND(quit, GameLoader, stop); |
---|
[2636] | 37 | |
---|
[5162] | 38 | |
---|
| 39 | GameLoader* GameLoader::singletonRef = NULL; |
---|
| 40 | |
---|
| 41 | |
---|
[4445] | 42 | /** |
---|
[4836] | 43 | * simple constructor |
---|
[4487] | 44 | */ |
---|
[4597] | 45 | GameLoader::GameLoader () |
---|
[4017] | 46 | { |
---|
[4597] | 47 | this->setClassID(CL_GAME_LOADER, "GameLoader"); |
---|
| 48 | this->setName("GameLoader"); |
---|
[5139] | 49 | |
---|
[4017] | 50 | } |
---|
[2636] | 51 | |
---|
| 52 | |
---|
[4445] | 53 | /** |
---|
[4836] | 54 | * simple deconstructor |
---|
[4487] | 55 | */ |
---|
[4816] | 56 | GameLoader::~GameLoader () |
---|
| 57 | { |
---|
| 58 | if( this->currentCampaign) |
---|
| 59 | delete this->currentCampaign; |
---|
| 60 | this->currentCampaign = NULL; |
---|
| 61 | } |
---|
[2636] | 62 | |
---|
| 63 | |
---|
[3225] | 64 | /** |
---|
[4836] | 65 | * this class is a singleton class |
---|
| 66 | * @returns an instance of itself |
---|
[3225] | 67 | |
---|
| 68 | if you are unsure about singleton classes, check the theory out on the internet :) |
---|
| 69 | */ |
---|
[2636] | 70 | GameLoader* GameLoader::getInstance() |
---|
| 71 | { |
---|
| 72 | if(singletonRef == NULL) |
---|
| 73 | singletonRef = new GameLoader(); |
---|
| 74 | return singletonRef; |
---|
| 75 | } |
---|
| 76 | |
---|
[4487] | 77 | /** |
---|
[4836] | 78 | * initializes the GameLoader |
---|
[4487] | 79 | */ |
---|
[3222] | 80 | ErrorMessage GameLoader::init() |
---|
[2636] | 81 | { |
---|
| 82 | if(this->currentCampaign != NULL) |
---|
| 83 | this->currentCampaign->init(); |
---|
[4411] | 84 | |
---|
| 85 | this->eventHandler = EventHandler::getInstance(); |
---|
[5093] | 86 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PAUSE); |
---|
[4411] | 87 | this->eventHandler->subscribe(this, ES_ALL, KeyMapper::PEV_QUIT); |
---|
[5093] | 88 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_NEXT_WORLD); |
---|
| 89 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD); |
---|
[2636] | 90 | } |
---|
| 91 | |
---|
| 92 | |
---|
[3225] | 93 | /** |
---|
[4836] | 94 | * reads a campaign definition file into a campaign class |
---|
| 95 | * @param fileName to be loaded |
---|
| 96 | * @returns the loaded campaign |
---|
[3225] | 97 | |
---|
| 98 | this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 99 | */ |
---|
[4487] | 100 | ErrorMessage GameLoader::loadCampaign(const char* fileName) |
---|
[2636] | 101 | { |
---|
[3222] | 102 | ErrorMessage errorCode; |
---|
[4487] | 103 | char* campaignName = ResourceManager::getFullName(fileName); |
---|
[4216] | 104 | if (campaignName) |
---|
[4094] | 105 | { |
---|
| 106 | this->currentCampaign = this->fileToCampaign(campaignName); |
---|
| 107 | delete campaignName; |
---|
| 108 | } |
---|
[4816] | 109 | // World* world0 = new World(DEBUG_WORLD_0); |
---|
| 110 | // world0->setNextStoryID(WORLD_ID_GAMEEND); |
---|
| 111 | // this->currentCampaign->addEntity(world0, WORLD_ID_2); |
---|
[2636] | 112 | } |
---|
| 113 | |
---|
[3225] | 114 | /** |
---|
[4836] | 115 | * loads a debug campaign for test purposes only. |
---|
| 116 | * @param campaignID the identifier of the campaign. |
---|
| 117 | * @returns error message if not able to do so. |
---|
[3225] | 118 | */ |
---|
[3222] | 119 | ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID) |
---|
[2636] | 120 | { |
---|
| 121 | switch(campaignID) |
---|
| 122 | { |
---|
[4597] | 123 | /* |
---|
| 124 | Debug Level 0: Debug level used to test the base frame work. |
---|
| 125 | As you can see, all storyentity data is allocated before game |
---|
| 126 | start. the storyentity will load themselfs shortly before start |
---|
| 127 | through the StoryEntity::init() funtion. |
---|
[3629] | 128 | */ |
---|
[2636] | 129 | case DEBUG_CAMPAIGN_0: |
---|
| 130 | { |
---|
[4597] | 131 | Campaign* debugCampaign = new Campaign(); |
---|
[3220] | 132 | |
---|
[4597] | 133 | World* world0 = new World(DEBUG_WORLD_0); |
---|
| 134 | world0->setNextStoryID(WORLD_ID_1); |
---|
| 135 | debugCampaign->addEntity(world0, WORLD_ID_0); |
---|
[3220] | 136 | |
---|
[4597] | 137 | World* world1 = new World(DEBUG_WORLD_1); |
---|
| 138 | world1->setNextStoryID(WORLD_ID_2); |
---|
| 139 | debugCampaign->addEntity(world1, WORLD_ID_1); |
---|
[2636] | 140 | |
---|
[4597] | 141 | World* world2 = new World(DEBUG_WORLD_2); |
---|
| 142 | world2->setNextStoryID(WORLD_ID_GAMEEND); |
---|
| 143 | debugCampaign->addEntity(world2, WORLD_ID_2); |
---|
[3727] | 144 | |
---|
[4597] | 145 | this->currentCampaign = debugCampaign; |
---|
| 146 | break; |
---|
[2636] | 147 | } |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
[4443] | 151 | |
---|
[4597] | 152 | /** |
---|
[4836] | 153 | * starts the current entity |
---|
| 154 | * @returns error code if this action has caused a error |
---|
[4443] | 155 | */ |
---|
[3222] | 156 | ErrorMessage GameLoader::start() |
---|
[2636] | 157 | { |
---|
| 158 | if(this->currentCampaign != NULL) |
---|
| 159 | this->currentCampaign->start(); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
[4597] | 163 | /** |
---|
[4836] | 164 | * stops the current entity |
---|
| 165 | * @returns error code if this action has caused a error |
---|
[4443] | 166 | |
---|
| 167 | ATTENTION: this function shouldn't call other functions, or if so, they must return |
---|
| 168 | after finishing. If you ignore or forget to do so, the current entity is not able to |
---|
| 169 | terminate and it will run in the background or the ressources can't be freed or even |
---|
| 170 | worse: are freed and the program will end in a segmentation fault! |
---|
| 171 | hehehe, have ya seen it... :) |
---|
| 172 | */ |
---|
[5139] | 173 | void GameLoader::stop() |
---|
[2636] | 174 | { |
---|
| 175 | if(this->currentCampaign != NULL) |
---|
| 176 | this->currentCampaign->stop(); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
[4597] | 180 | /** |
---|
[4836] | 181 | * pause the current entity |
---|
| 182 | * @returns error code if this action has caused a error |
---|
[4443] | 183 | |
---|
| 184 | this pauses the current entity or passes this call forth to the running entity. |
---|
| 185 | */ |
---|
[3222] | 186 | ErrorMessage GameLoader::pause() |
---|
[2636] | 187 | { |
---|
| 188 | this->isPaused = true; |
---|
| 189 | if(this->currentCampaign != NULL) |
---|
| 190 | this->currentCampaign->pause(); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
[4597] | 194 | /** |
---|
[4836] | 195 | * resumes a pause |
---|
| 196 | * @returns error code if this action has caused a error |
---|
[4443] | 197 | |
---|
| 198 | this resumess the current entity or passes this call forth to the running entity. |
---|
| 199 | */ |
---|
[3222] | 200 | ErrorMessage GameLoader::resume() |
---|
[2636] | 201 | { |
---|
| 202 | this->isPaused = false; |
---|
| 203 | if(this->currentCampaign != NULL) |
---|
| 204 | this->currentCampaign->resume(); |
---|
| 205 | } |
---|
| 206 | |
---|
[4443] | 207 | |
---|
[3225] | 208 | /** |
---|
[4836] | 209 | * release the mem ATTENTION: not implemented |
---|
[3225] | 210 | */ |
---|
| 211 | ErrorMessage GameLoader::destroy() |
---|
[4487] | 212 | { |
---|
[2636] | 213 | |
---|
[4487] | 214 | } |
---|
[2636] | 215 | |
---|
[4487] | 216 | |
---|
[3225] | 217 | /** |
---|
[4836] | 218 | * reads a campaign definition file into a campaign class |
---|
| 219 | * @param fileName to be loaded |
---|
| 220 | * @returns the loaded campaign |
---|
[3225] | 221 | |
---|
| 222 | this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns |
---|
| 223 | */ |
---|
[4487] | 224 | Campaign* GameLoader::fileToCampaign(const char* fileName) |
---|
[2636] | 225 | { |
---|
| 226 | /* do not entirely load the campaign. just the current world |
---|
| 227 | before start of each world, it has to be initialized so it |
---|
| 228 | can load everything it needs into memory then. |
---|
| 229 | */ |
---|
[4597] | 230 | |
---|
[4487] | 231 | if( fileName == NULL) |
---|
[4010] | 232 | { |
---|
[4113] | 233 | PRINTF(2)("No filename specified for loading"); |
---|
[4010] | 234 | return NULL; |
---|
| 235 | } |
---|
[4597] | 236 | |
---|
[4487] | 237 | TiXmlDocument* XMLDoc = new TiXmlDocument( fileName); |
---|
[4010] | 238 | // load the campaign document |
---|
| 239 | if( !XMLDoc->LoadFile()) |
---|
| 240 | { |
---|
| 241 | // report an error |
---|
[4487] | 242 | PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); |
---|
[4010] | 243 | delete XMLDoc; |
---|
| 244 | return NULL; |
---|
| 245 | } |
---|
[4597] | 246 | |
---|
[4010] | 247 | // check basic validity |
---|
| 248 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
| 249 | assert( root != NULL); |
---|
[4597] | 250 | |
---|
[4010] | 251 | if( strcmp( root->Value(), "Campaign")) |
---|
| 252 | { |
---|
| 253 | // report an error |
---|
[4113] | 254 | PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); |
---|
[4010] | 255 | delete XMLDoc; |
---|
| 256 | return NULL; |
---|
| 257 | } |
---|
[4597] | 258 | |
---|
[4010] | 259 | // construct campaign |
---|
| 260 | Campaign* c = new Campaign( root); |
---|
[4597] | 261 | |
---|
[4010] | 262 | // free the XML data |
---|
| 263 | delete XMLDoc; |
---|
[4496] | 264 | |
---|
[4010] | 265 | return c; |
---|
[2636] | 266 | } |
---|
| 267 | |
---|
| 268 | |
---|
| 269 | /** |
---|
[4836] | 270 | * handle keyboard commands |
---|
| 271 | * @param event the event to handle |
---|
[2636] | 272 | */ |
---|
[4410] | 273 | void GameLoader::process(const Event& event) |
---|
| 274 | { |
---|
| 275 | if( event.type == KeyMapper::PEV_NEXT_WORLD) |
---|
| 276 | { |
---|
[4597] | 277 | if( likely(event.bPressed)) |
---|
| 278 | { |
---|
| 279 | this->nextLevel(); |
---|
| 280 | } |
---|
[4410] | 281 | } |
---|
| 282 | else if( event.type == KeyMapper::PEV_PREVIOUS_WORLD) |
---|
| 283 | { |
---|
| 284 | if( likely(event.bPressed)) |
---|
[4597] | 285 | { |
---|
| 286 | this->previousLevel(); |
---|
| 287 | } |
---|
[4410] | 288 | } |
---|
| 289 | else if( event.type == KeyMapper::PEV_PAUSE) |
---|
| 290 | { |
---|
| 291 | if( likely(event.bPressed)) |
---|
[4597] | 292 | { |
---|
| 293 | if(this->isPaused) |
---|
| 294 | this->resume(); |
---|
| 295 | else |
---|
| 296 | this->pause(); |
---|
| 297 | } |
---|
[4410] | 298 | } |
---|
| 299 | else if( event.type == KeyMapper::PEV_QUIT) |
---|
| 300 | { |
---|
| 301 | if( event.bPressed) this->stop(); |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | |
---|
[4443] | 305 | |
---|
[4487] | 306 | /** |
---|
[3225] | 307 | \brief this changes to the next level |
---|
| 308 | */ |
---|
[2636] | 309 | void GameLoader::nextLevel() |
---|
| 310 | { |
---|
| 311 | if(this->currentCampaign != NULL) |
---|
| 312 | this->currentCampaign->nextLevel(); |
---|
| 313 | } |
---|
| 314 | |
---|
[3225] | 315 | |
---|
[4487] | 316 | /** |
---|
[3225] | 317 | \brief change to the previous level - not implemented |
---|
| 318 | |
---|
| 319 | this propably useless |
---|
| 320 | */ |
---|
[2636] | 321 | void GameLoader::previousLevel() |
---|
| 322 | { |
---|
| 323 | if(this->currentCampaign != NULL) |
---|
| 324 | this->currentCampaign->previousLevel(); |
---|
| 325 | } |
---|
[4010] | 326 | |
---|
| 327 | /** |
---|
[4836] | 328 | * load a StoryEntity |
---|
| 329 | * @param element a XMLElement containing all the needed info |
---|
[4010] | 330 | */ |
---|
[4598] | 331 | BaseObject* GameLoader::fabricate(const TiXmlElement* element) |
---|
[4010] | 332 | { |
---|
[4114] | 333 | assert( element != NULL); |
---|
[4597] | 334 | |
---|
[4739] | 335 | if( Factory::getFirst() == NULL) |
---|
[4114] | 336 | { |
---|
| 337 | PRINTF(1)("GameLoader does not know any factories, fabricate() failed\n"); |
---|
| 338 | return NULL; |
---|
| 339 | } |
---|
[4597] | 340 | |
---|
[4114] | 341 | if( element->Value() != NULL) |
---|
| 342 | { |
---|
| 343 | PRINTF(4)("Attempting fabrication of a '%s'\n", element->Value()); |
---|
[4739] | 344 | BaseObject* b = Factory::getFirst()->fabricate( element); |
---|
[4597] | 345 | if( b == NULL) |
---|
| 346 | PRINTF(2)("Failed to fabricate a '%s'\n", element->Value()); |
---|
| 347 | else |
---|
| 348 | PRINTF(4)("Successfully fabricated a '%s'\n", element->Value()); |
---|
[4114] | 349 | return b; |
---|
| 350 | } |
---|
[4597] | 351 | |
---|
[4114] | 352 | PRINTF(2)("Fabricate failed, TiXmlElement did not contain a value\n"); |
---|
[4597] | 353 | |
---|
[4114] | 354 | return NULL; |
---|
[4010] | 355 | } |
---|