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