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