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