| 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 | */ |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #include "campaign.h" |
|---|
| 19 | |
|---|
| 20 | #include "factory.h" |
|---|
| 21 | #include "load_param.h" |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | using namespace std; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * the constructor |
|---|
| 29 | * @param root the XML root element |
|---|
| 30 | * |
|---|
| 31 | * this constructor is always called in a XML context (loading procedure) |
|---|
| 32 | */ |
|---|
| 33 | Campaign::Campaign ( TiXmlElement* root) |
|---|
| 34 | { |
|---|
| 35 | this->setClassID(CL_CAMPAIGN, "Campaign"); |
|---|
| 36 | |
|---|
| 37 | PRINTF(4)("Loading Campaign...\n"); |
|---|
| 38 | assert( root != NULL); |
|---|
| 39 | |
|---|
| 40 | this->campaignData = new CampaignData(root); |
|---|
| 41 | this->loadParams(root); |
|---|
| 42 | |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * the campaign destructor |
|---|
| 48 | */ |
|---|
| 49 | Campaign::~Campaign () |
|---|
| 50 | { |
|---|
| 51 | if( this->campaignData) |
|---|
| 52 | delete this->campaignData; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * loads the Parameters of a Campaign |
|---|
| 58 | * @param root: The XML-element to load from |
|---|
| 59 | */ |
|---|
| 60 | void Campaign::loadParams(const TiXmlElement* root) |
|---|
| 61 | { |
|---|
| 62 | static_cast<StoryEntity*>(this)->loadParams(root); |
|---|
| 63 | |
|---|
| 64 | PRINTF(4)("Loaded Campaign specific stuff\n"); |
|---|
| 65 | |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * starts the campaing with a specific ID |
|---|
| 71 | * @param storyID the id of the StoryEntity |
|---|
| 72 | */ |
|---|
| 73 | bool Campaign::start() |
|---|
| 74 | { |
|---|
| 75 | PRINTF(2)("Starting Campaign nr. %i\n", this->getStoryID()); |
|---|
| 76 | |
|---|
| 77 | this->isRunning = true; |
|---|
| 78 | this->run(); |
|---|
| 79 | |
|---|
| 80 | PRINTF(2)("There is no StoryEnity left to play, quitting\n"); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * pauses the campaign |
|---|
| 86 | */ |
|---|
| 87 | bool Campaign::pause() |
|---|
| 88 | { |
|---|
| 89 | this->isPaused = true; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * resumes the campaign after a pause |
|---|
| 95 | */ |
|---|
| 96 | bool Campaign::resume() |
|---|
| 97 | { |
|---|
| 98 | PRINTF(4)("Resuming the current Campaign\n"); |
|---|
| 99 | this->isPaused = false; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * stops the campaign |
|---|
| 105 | */ |
|---|
| 106 | bool Campaign::stop() |
|---|
| 107 | { |
|---|
| 108 | PRINTF(4)("Stopping the current Campaign\n"); |
|---|
| 109 | this->isRunning = false; |
|---|
| 110 | if( this->currentEntity != NULL) |
|---|
| 111 | { |
|---|
| 112 | this->currentEntity->stop(); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * runs the campaign |
|---|
| 119 | */ |
|---|
| 120 | void Campaign::run() |
|---|
| 121 | { |
|---|
| 122 | ErrorMessage errorCode; |
|---|
| 123 | int storyID = WORLD_ID_0; |
|---|
| 124 | |
|---|
| 125 | for( this->currentEntity = this->campaignData->getFirstLevel(), this->isRunning = true; |
|---|
| 126 | this->currentEntity != NULL && this->isRunning; |
|---|
| 127 | this->currentEntity = this->campaignData->getNextLevel()) |
|---|
| 128 | { |
|---|
| 129 | PRINTF(0)("Campaign is starting StoryEntity nr:%i\n", this->currentEntity->getStoryID()); |
|---|
| 130 | |
|---|
| 131 | this->currentEntity->init(); |
|---|
| 132 | this->currentEntity->loadData(); |
|---|
| 133 | this->currentEntity->start(); |
|---|
| 134 | this->currentEntity->unloadData(); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * this changes to the next level |
|---|
| 141 | */ |
|---|
| 142 | void Campaign::switchToNextLevel() |
|---|
| 143 | { |
|---|
| 144 | PRINTF(4)("Switching to the next StoryEntity\n"); |
|---|
| 145 | this->currentEntity->stop(); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | /* ========================================================================================================== |
|---|
| 151 | CampaignData |
|---|
| 152 | ========================================================================================================== |
|---|
| 153 | */ |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * constructor for CampaignData |
|---|
| 157 | */ |
|---|
| 158 | CampaignData::CampaignData(const TiXmlElement* root) |
|---|
| 159 | { |
|---|
| 160 | this->setClassID(CL_CAMPAIGN_DATA, "CampaignData"); |
|---|
| 161 | this->currentEntity = NULL; |
|---|
| 162 | |
|---|
| 163 | this->loadParams(root); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * destructor for CampaignData |
|---|
| 169 | */ |
|---|
| 170 | CampaignData::~CampaignData() |
|---|
| 171 | { |
|---|
| 172 | PRINTF(0)("Deleting CampaignData\n"); |
|---|
| 173 | while( !this->storyEntities.empty()) |
|---|
| 174 | { |
|---|
| 175 | StoryEntity* bo = this->storyEntities.front(); |
|---|
| 176 | PRINTF(0)("CampaignData is been deleted: nr %i\n", bo->getStoryID()); |
|---|
| 177 | delete bo; |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | /** |
|---|
| 184 | * loads the Parameters of a Campaign |
|---|
| 185 | * @param root: The XML-element to load from |
|---|
| 186 | */ |
|---|
| 187 | void CampaignData::loadParams(const TiXmlElement* root) |
|---|
| 188 | { |
|---|
| 189 | LoadParamXML(root, "WorldList", this, CampaignData, loadParamsWorldList) |
|---|
| 190 | .describe("A List of Worlds to be loaded in this Campaign"); |
|---|
| 191 | |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * loads a WorldList |
|---|
| 197 | * @param root: the XML-element to load from |
|---|
| 198 | */ |
|---|
| 199 | void CampaignData::loadParamsWorldList(const TiXmlElement* root) |
|---|
| 200 | { |
|---|
| 201 | if( root == NULL) |
|---|
| 202 | return; |
|---|
| 203 | |
|---|
| 204 | LOAD_PARAM_START_CYCLE(root, element); |
|---|
| 205 | { |
|---|
| 206 | StoryEntity* created = (StoryEntity*) Factory::fabricate(element); |
|---|
| 207 | if( created != NULL) |
|---|
| 208 | this->addStoryEntity(created); |
|---|
| 209 | } |
|---|
| 210 | LOAD_PARAM_END_CYCLE(element); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | /** |
|---|
| 215 | * add the StoryEntity to the campaign data tank |
|---|
| 216 | * @param se the StoryEntity to add |
|---|
| 217 | */ |
|---|
| 218 | void CampaignData::addStoryEntity(StoryEntity* se) |
|---|
| 219 | { |
|---|
| 220 | this->storyEntities.push_back(se); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | /** |
|---|
| 225 | * switch to the next level in the list and return it |
|---|
| 226 | */ |
|---|
| 227 | StoryEntity* CampaignData::getFirstLevel() |
|---|
| 228 | { |
|---|
| 229 | int nextStoryID; |
|---|
| 230 | int storyID; |
|---|
| 231 | list<StoryEntity*>::iterator it; |
|---|
| 232 | |
|---|
| 233 | nextStoryID = 0; |
|---|
| 234 | this->currentEntity = NULL; |
|---|
| 235 | for( it = this->storyEntities.begin(); it != this->storyEntities.end(); it++) |
|---|
| 236 | { |
|---|
| 237 | storyID = (*it)->getStoryID(); |
|---|
| 238 | if( storyID == nextStoryID) |
|---|
| 239 | this->currentEntity = (*it); |
|---|
| 240 | } |
|---|
| 241 | return this->currentEntity; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * switch to the next level in the list and return it |
|---|
| 247 | */ |
|---|
| 248 | StoryEntity* CampaignData::getNextLevel() |
|---|
| 249 | { |
|---|
| 250 | int nextStoryID; |
|---|
| 251 | int storyID; |
|---|
| 252 | list<StoryEntity*>::iterator it; |
|---|
| 253 | |
|---|
| 254 | nextStoryID = this->currentEntity->getNextStoryID(); |
|---|
| 255 | this->currentEntity = NULL; |
|---|
| 256 | for( it = this->storyEntities.begin(); it != this->storyEntities.end(); it++) |
|---|
| 257 | { |
|---|
| 258 | storyID = (*it)->getStoryID(); |
|---|
| 259 | if( storyID == nextStoryID) |
|---|
| 260 | this->currentEntity = (*it); |
|---|
| 261 | } |
|---|
| 262 | return this->currentEntity; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|