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