[3573] | 1 | |
---|
[4597] | 2 | /* |
---|
[3573] | 3 | orxonox - the future of 3D-vertical-scrollers |
---|
| 4 | |
---|
| 5 | Copyright (C) 2004 orx |
---|
| 6 | |
---|
| 7 | This program is free software; you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU General Public License as published by |
---|
| 9 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 10 | any later version. |
---|
| 11 | |
---|
[4826] | 12 | ### File Specific |
---|
[3573] | 13 | main-programmer: Patrick Boenzli |
---|
[4832] | 14 | co-programmer: Benjamin Grauer |
---|
[4885] | 15 | |
---|
| 16 | 2005-07-15: Benjamin Grauer: restructurating the entire Class |
---|
[3573] | 17 | */ |
---|
| 18 | |
---|
[4885] | 19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
| 20 | |
---|
[4828] | 21 | #include "weapon.h" |
---|
| 22 | |
---|
[9869] | 23 | #include "loading/fast_factory.h" |
---|
[6434] | 24 | #include "world_entities/projectiles/projectile.h" |
---|
[4834] | 25 | |
---|
[7350] | 26 | #include "util/loading/factory.h" |
---|
[7193] | 27 | #include "util/loading/load_param.h" |
---|
[4828] | 28 | #include "state.h" |
---|
[4885] | 29 | #include "animation3d.h" |
---|
[3573] | 30 | |
---|
[5930] | 31 | #include "sound_source.h" |
---|
| 32 | #include "sound_buffer.h" |
---|
[9869] | 33 | #include "resource_sound_buffer.h" |
---|
[5930] | 34 | |
---|
[10368] | 35 | #include "elements/glgui_energywidgetvertical.h" |
---|
[6438] | 36 | |
---|
[9869] | 37 | ObjectListDefinition(Weapon); |
---|
[7350] | 38 | |
---|
[4892] | 39 | //////////////////// |
---|
| 40 | // INITAILISATION // |
---|
| 41 | // SETTING VALUES // |
---|
| 42 | //////////////////// |
---|
[3870] | 43 | /** |
---|
[4885] | 44 | * standard constructor |
---|
| 45 | * |
---|
| 46 | * creates a new weapon |
---|
[3575] | 47 | */ |
---|
[5750] | 48 | Weapon::Weapon () |
---|
[3620] | 49 | { |
---|
[4885] | 50 | this->init(); |
---|
[3620] | 51 | } |
---|
[3573] | 52 | |
---|
[3575] | 53 | /** |
---|
[4885] | 54 | * standard deconstructor |
---|
[3575] | 55 | */ |
---|
[4597] | 56 | Weapon::~Weapon () |
---|
[3573] | 57 | { |
---|
[4885] | 58 | for (int i = 0; i < WS_STATE_COUNT; i++) |
---|
[9869] | 59 | if (this->animation[i] && Animation::objectList().exists(animation[i])) //!< @todo this should check animation3D |
---|
[4885] | 60 | delete this->animation[i]; |
---|
[4959] | 61 | |
---|
[9869] | 62 | if (OrxSound::SoundSource::objectList().exists(this->soundSource)) |
---|
[4959] | 63 | delete this->soundSource; |
---|
[4885] | 64 | } |
---|
[4597] | 65 | |
---|
[4885] | 66 | /** |
---|
[7350] | 67 | * @brief creates a new Weapon of type weaponID and returns it. |
---|
| 68 | * @param weaponID the WeaponID type to create. |
---|
| 69 | * @returns the newly created Weapon. |
---|
| 70 | */ |
---|
[9869] | 71 | Weapon* Weapon::createWeapon(const ClassID& weaponID) |
---|
[7350] | 72 | { |
---|
| 73 | BaseObject* createdObject = Factory::fabricate(weaponID); |
---|
| 74 | if (createdObject != NULL) |
---|
| 75 | { |
---|
[9869] | 76 | if (createdObject->isA(Weapon::staticClassID())) |
---|
[7350] | 77 | return dynamic_cast<Weapon*>(createdObject); |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | delete createdObject; |
---|
| 81 | return NULL; |
---|
| 82 | } |
---|
| 83 | } |
---|
[8316] | 84 | return NULL; |
---|
[7350] | 85 | } |
---|
| 86 | |
---|
[9869] | 87 | Weapon* Weapon::createWeapon(const std::string& weaponName) |
---|
| 88 | { |
---|
| 89 | BaseObject* createdObject = Factory::fabricate(weaponName); |
---|
| 90 | if (createdObject != NULL) |
---|
| 91 | { |
---|
| 92 | if (createdObject->isA(Weapon::staticClassID())) |
---|
| 93 | return dynamic_cast<Weapon*>(createdObject); |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | delete createdObject; |
---|
| 97 | return NULL; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | return NULL; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
[7350] | 104 | /** |
---|
[4885] | 105 | * initializes the Weapon with ALL default values |
---|
[5498] | 106 | * |
---|
| 107 | * This Sets the default values of the Weapon |
---|
[4885] | 108 | */ |
---|
| 109 | void Weapon::init() |
---|
| 110 | { |
---|
[9869] | 111 | this->registerObject(this, Weapon::_objectList); |
---|
[5498] | 112 | this->currentState = WS_INACTIVE; //< Normaly the Weapon is Inactive |
---|
| 113 | this->requestedAction = WA_NONE; //< No action is requested by default |
---|
| 114 | this->stateDuration = 0.0; //< All the States have zero duration |
---|
| 115 | for (int i = 0; i < WS_STATE_COUNT; i++) //< Every State has: |
---|
[6438] | 116 | { |
---|
| 117 | this->times[i] = 0.0; //< An infinitesimal duration |
---|
| 118 | this->animation[i] = NULL; //< No animation |
---|
| 119 | } |
---|
[3888] | 120 | |
---|
[10368] | 121 | this->soundSource = new OrxSound::SoundSource(this); //< Every Weapon has exacty one SoundSource |
---|
[4885] | 122 | |
---|
[10368] | 123 | this->barrels = 1; |
---|
| 124 | this->segs = 1; |
---|
| 125 | |
---|
[10443] | 126 | this->preferedSide = -1; |
---|
| 127 | this->preferedSlot = -1; |
---|
| 128 | |
---|
[10368] | 129 | this->shootAnim = new Animation3D**[this->getBarrels()]; |
---|
| 130 | for (int i = 0; i < this->getBarrels(); i++) |
---|
| 131 | this->shootAnim[i] = new Animation3D* [this->getSegs()]; |
---|
| 132 | |
---|
| 133 | this->emissionPoint = new PNode*[this->barrels]; |
---|
| 134 | for(int i = 0; i < this->barrels; i++){ |
---|
| 135 | this->emissionPoint[i] = new PNode; |
---|
[10648] | 136 | this->emissionPoint[i]->setParent(this); //Parenting emissionPoint to Weapons |
---|
[10368] | 137 | this->emissionPoint[i]->setName("EmissionPoint"); |
---|
| 138 | this->emissionPoint[i]->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 139 | } |
---|
| 140 | |
---|
[6920] | 141 | this->defaultTarget = NULL; //< Nothing is Targeted by default. |
---|
[6756] | 142 | |
---|
[9869] | 143 | this->projectile = NullClass::staticClassID(); //< No Projectile Class is Connected to this weapon |
---|
[5498] | 144 | this->projectileFactory = NULL; //< No Factory generating Projectiles is selected. |
---|
[4885] | 145 | |
---|
[5498] | 146 | this->hideInactive = true; //< The Weapon will be hidden if it is inactive (by default) |
---|
[10684] | 147 | this->currentState = WS_INACTIVE; |
---|
[4906] | 148 | |
---|
[5498] | 149 | this->minCharge = 1.0; //< The minimum charge the Weapon can hold is 1 unit. |
---|
| 150 | this->maxCharge = 1.0; //< The maximum charge is also one unit. |
---|
[4927] | 151 | |
---|
[6671] | 152 | this->energy = 10; //< The secondary Buffer (before we have to reload) |
---|
[5498] | 153 | this->energyMax = 10.0; //< How much energy can be carried |
---|
| 154 | this->capability = WTYPE_ALL; //< The Weapon has all capabilities @see W_Capability. |
---|
[6438] | 155 | |
---|
| 156 | this->energyWidget = NULL; |
---|
[10516] | 157 | isEnergyWidgetInitialized = false; |
---|
[6695] | 158 | |
---|
| 159 | // set this object to be synchronized over network |
---|
| 160 | //this->setSynchronized(true); |
---|
[3573] | 161 | } |
---|
| 162 | |
---|
[5498] | 163 | /** |
---|
| 164 | * loads the Parameters of a Weapon |
---|
| 165 | * @param root the XML-Element to load the Weapons settings from |
---|
| 166 | */ |
---|
[4972] | 167 | void Weapon::loadParams(const TiXmlElement* root) |
---|
| 168 | { |
---|
[6512] | 169 | WorldEntity::loadParams(root); |
---|
[4972] | 170 | |
---|
[7102] | 171 | LoadParam(root, "projectile", this, Weapon, setProjectileTypeC) |
---|
[6438] | 172 | .describe("Sets the name of the Projectile to load onto the Entity"); |
---|
[10443] | 173 | |
---|
| 174 | LoadParam(root, "energy", this, Weapon, setEnergyMax) |
---|
| 175 | .describe("number of shoots/ energy whatever"); |
---|
| 176 | |
---|
| 177 | LoadParam(root, "slot", this, Weapon, setPreferedSlot) |
---|
| 178 | .describe("slot this weapon will be added"); |
---|
[10368] | 179 | /* |
---|
[5671] | 180 | LoadParam(root, "emission-point", this, Weapon, setEmissionPoint) |
---|
[10368] | 181 | .describe("Sets the Point of emission of this weapon (1: EmmsionVector; 2: EmissionPoint)");*/ |
---|
[4972] | 182 | |
---|
[5671] | 183 | LoadParam(root, "state-duration", this, Weapon, setStateDuration) |
---|
[6438] | 184 | .describe("Sets the duration of a given state (1: state-Name; 2: duration in seconds)"); |
---|
[4972] | 185 | |
---|
[5671] | 186 | LoadParam(root, "action-sound", this, Weapon, setActionSound) |
---|
[6438] | 187 | .describe("Sets a given sound to an action (1: action-Name; 2: name of the sound (relative to the Data-Path))"); |
---|
[4972] | 188 | } |
---|
| 189 | |
---|
[6728] | 190 | |
---|
[4947] | 191 | /** |
---|
| 192 | * sets the Projectile to use for this weapon. |
---|
| 193 | * @param projectile The ID of the Projectile to use |
---|
[4950] | 194 | * @returns true, if it was sucessfull, false on error |
---|
[4947] | 195 | * |
---|
[5498] | 196 | * be aware, that this function does not create Factories, as this is job of Projecitle/Bullet-classes. |
---|
| 197 | * What it does, is telling the Weapon what Projectiles it can Emit. |
---|
[4947] | 198 | */ |
---|
[9869] | 199 | void Weapon::setProjectileType(const ClassID& projectile) |
---|
[4947] | 200 | { |
---|
| 201 | this->projectile = projectile; |
---|
| 202 | this->projectileFactory = FastFactory::searchFastFactory(projectile); |
---|
| 203 | if (this->projectileFactory == NULL) |
---|
[4979] | 204 | { |
---|
| 205 | PRINTF(1)("unable to find FastFactory for the Projectile.\n"); |
---|
[4972] | 206 | return; |
---|
[4979] | 207 | } |
---|
[4948] | 208 | else |
---|
| 209 | { |
---|
| 210 | // grabbing Parameters from the Projectile to have them at hand here. |
---|
| 211 | Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect()); |
---|
[6431] | 212 | this->minCharge = pj->getMinEnergy(); |
---|
[6700] | 213 | this->maxCharge = pj->getHealthMax(); |
---|
[4948] | 214 | this->chargeable = pj->isChageable(); |
---|
[4979] | 215 | this->projectileFactory->kill(pj); |
---|
[4948] | 216 | } |
---|
[4979] | 217 | } |
---|
[3573] | 218 | |
---|
[6728] | 219 | |
---|
[4891] | 220 | /** |
---|
[4950] | 221 | * @see bool Weapon::setProjectile(ClassID projectile) |
---|
| 222 | * @param projectile the Name of the Projectile. |
---|
| 223 | */ |
---|
[7221] | 224 | void Weapon::setProjectileTypeC(const std::string& projectile) |
---|
[4950] | 225 | { |
---|
| 226 | FastFactory* tmpFac = FastFactory::searchFastFactory(projectile); |
---|
| 227 | if (tmpFac != NULL) |
---|
| 228 | { |
---|
[5356] | 229 | this->setProjectileType(tmpFac->getStoredID()); |
---|
[4950] | 230 | } |
---|
[4972] | 231 | else |
---|
| 232 | { |
---|
[9406] | 233 | PRINTF(1)("Projectile %s does not exist for weapon %s\n", projectile.c_str(), this->getCName()); |
---|
[4972] | 234 | } |
---|
[4950] | 235 | } |
---|
| 236 | |
---|
[6728] | 237 | |
---|
[4950] | 238 | /** |
---|
[5356] | 239 | * prepares Projectiles of the Weapon |
---|
[5498] | 240 | * @param count how many Projectiles to create (they will be stored in the ProjectileFactory) |
---|
[5356] | 241 | */ |
---|
| 242 | void Weapon::prepareProjectiles(unsigned int count) |
---|
| 243 | { |
---|
[5357] | 244 | if (likely(this->projectileFactory != NULL)) |
---|
[5356] | 245 | projectileFactory->prepare(count); |
---|
| 246 | else |
---|
[9406] | 247 | PRINTF(2)("unable to create %d projectile for Weapon %s::%s\n", count, this->getClassCName(), this->getCName()); |
---|
[5356] | 248 | } |
---|
| 249 | |
---|
[6728] | 250 | |
---|
[5356] | 251 | /** |
---|
| 252 | * resurects and returns a Projectile |
---|
[5498] | 253 | * @returns a Projectile on success, NULL on error |
---|
| 254 | * |
---|
| 255 | * errors: 1. (ProjectileFastFactory not Found) |
---|
| 256 | * 2. No more Projectiles availiable. |
---|
[5356] | 257 | */ |
---|
| 258 | Projectile* Weapon::getProjectile() |
---|
| 259 | { |
---|
[5357] | 260 | if (likely (this->projectileFactory != NULL)) |
---|
[6142] | 261 | { |
---|
| 262 | Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect()); |
---|
| 263 | pj->toList((OM_LIST)(this->getOMListNumber()+1)); |
---|
| 264 | return pj; |
---|
| 265 | } |
---|
[5356] | 266 | else |
---|
| 267 | { |
---|
[9406] | 268 | PRINTF(2)("No projectile defined for Weapon %s(%s) can't return any\n", this->getCName(), this->getClassCName()); |
---|
[5356] | 269 | return NULL; |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | /** |
---|
[4892] | 275 | * sets the emissionPoint's relative position from the Weapon |
---|
| 276 | * @param point the Point relative to the mass-point of the Weapon |
---|
| 277 | */ |
---|
[10368] | 278 | void Weapon::setEmissionPoint(const Vector& point, int barrel) |
---|
| 279 | { |
---|
| 280 | this->emissionPoint[barrel]->setRelCoor(point); |
---|
| 281 | } |
---|
| 282 | |
---|
[4892] | 283 | void Weapon::setEmissionPoint(const Vector& point) |
---|
| 284 | { |
---|
[10368] | 285 | this->emissionPoint[0]->setRelCoor(point); |
---|
[4892] | 286 | } |
---|
| 287 | |
---|
| 288 | /** |
---|
[4891] | 289 | * assigns a Sound-file to an action |
---|
| 290 | * @param action the action the sound should be assigned too |
---|
| 291 | * @param soundFile the soundFile's relative position to the data-directory (will be looked for by the ResourceManager) |
---|
| 292 | */ |
---|
[7221] | 293 | void Weapon::setActionSound(WeaponAction action, const std::string& soundFile) |
---|
[4885] | 294 | { |
---|
| 295 | if (action >= WA_ACTION_COUNT) |
---|
| 296 | return; |
---|
[4930] | 297 | |
---|
[7221] | 298 | else if (!soundFile.empty()) |
---|
[4885] | 299 | { |
---|
[9869] | 300 | this->soundBuffers[action] = OrxSound::ResourceSoundBuffer(soundFile); |
---|
| 301 | if (this->soundBuffers[action].loaded()) |
---|
[4885] | 302 | { |
---|
[7221] | 303 | PRINTF(4)("Loaded sound %s to action %s.\n", soundFile.c_str(), actionToChar(action)); |
---|
[4885] | 304 | } |
---|
| 305 | else |
---|
| 306 | { |
---|
[7221] | 307 | PRINTF(2)("Failed to load sound %s to %s.\n.", soundFile.c_str(), actionToChar(action)); |
---|
[4885] | 308 | } |
---|
| 309 | } |
---|
| 310 | else |
---|
[9869] | 311 | this->soundBuffers[action] = OrxSound::SoundBuffer(); |
---|
[4885] | 312 | } |
---|
| 313 | |
---|
[6728] | 314 | |
---|
[4893] | 315 | /** |
---|
[4895] | 316 | * creates/returns an Animation3D for a certain State. |
---|
| 317 | * @param state what State should the Animation be created/returned for |
---|
| 318 | * @param node the node this Animation should apply to. (NULL is fine if the animation was already created) |
---|
| 319 | * @returns The created animation.Animation(), NULL on error (or if the animation does not yet exist). |
---|
[4893] | 320 | * |
---|
| 321 | * This function does only generate the Animation Object, and if set it will |
---|
| 322 | * automatically be executed, when a certain State is reached. |
---|
| 323 | * What this does not do, is set keyframes, you have to operate on the returned animation. |
---|
| 324 | */ |
---|
[4895] | 325 | Animation3D* Weapon::getAnimation(WeaponState state, PNode* node) |
---|
[4893] | 326 | { |
---|
[4895] | 327 | if (state >= WS_STATE_COUNT) // if the state is not known |
---|
[4893] | 328 | return NULL; |
---|
| 329 | |
---|
[4895] | 330 | if (unlikely(this->animation[state] == NULL)) // if the animation does not exist yet create it. |
---|
[4893] | 331 | { |
---|
[4895] | 332 | if (likely(node != NULL)) |
---|
| 333 | return this->animation[state] = new Animation3D(node); |
---|
| 334 | else |
---|
| 335 | { |
---|
| 336 | PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state)); |
---|
| 337 | return NULL; |
---|
| 338 | } |
---|
[4893] | 339 | } |
---|
[4895] | 340 | else |
---|
| 341 | return this->animation[state]; |
---|
[4893] | 342 | } |
---|
| 343 | |
---|
[10368] | 344 | Animation3D* Weapon::getAnimation(int barrel, int seg, PNode* node) |
---|
| 345 | { |
---|
| 346 | if (barrel >= this->getBarrels() || seg >= this->getSegs()) // if the state is not known |
---|
| 347 | return NULL; |
---|
| 348 | |
---|
| 349 | if (unlikely(this->shootAnim[barrel][seg] == NULL)) // if the animation does not exist yet create it. |
---|
| 350 | { |
---|
| 351 | if (likely(node != NULL)) |
---|
| 352 | return this->shootAnim[barrel][seg] = new Animation3D(node); |
---|
| 353 | else |
---|
| 354 | { |
---|
| 355 | // PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state)); |
---|
| 356 | return NULL; |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | else |
---|
| 360 | return this->shootAnim[barrel][seg]; |
---|
| 361 | } |
---|
| 362 | |
---|
[7779] | 363 | OrxGui::GLGuiWidget* Weapon::getEnergyWidget() |
---|
[6438] | 364 | { |
---|
[10368] | 365 | if ( this->energyWidget == NULL) |
---|
[6438] | 366 | { |
---|
[10368] | 367 | this->energyWidget = new OrxGui::GLGuiEnergyWidgetVertical(); |
---|
| 368 | //this->energyWidget->setDisplayedName(this->getClassCName()); |
---|
[6438] | 369 | this->energyWidget->setSize2D( 20, 100); |
---|
| 370 | this->energyWidget->setMaximum(this->getEnergyMax()); |
---|
| 371 | this->energyWidget->setValue(this->getEnergy()); |
---|
| 372 | } |
---|
| 373 | return this->energyWidget; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | void Weapon::updateWidgets() |
---|
| 377 | { |
---|
| 378 | if (this->energyWidget != NULL) |
---|
| 379 | { |
---|
| 380 | this->energyWidget->setMaximum(this->energyMax); |
---|
| 381 | this->energyWidget->setValue(this->energy); |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | |
---|
[4892] | 385 | ///////////////// |
---|
| 386 | // EXECUTION // |
---|
| 387 | // GAME ACTION // |
---|
| 388 | ///////////////// |
---|
[4597] | 389 | /** |
---|
[4885] | 390 | * request an action that should be executed, |
---|
| 391 | * @param action the next action to take |
---|
| 392 | * |
---|
| 393 | * This function must be called instead of the actions (like fire/reload...) |
---|
| 394 | * to make all the checks needed to have a usefull WeaponSystem. |
---|
| 395 | */ |
---|
| 396 | void Weapon::requestAction(WeaponAction action) |
---|
| 397 | { |
---|
[4906] | 398 | if (likely(this->isActive())) |
---|
[4885] | 399 | { |
---|
[10368] | 400 | /** Disabled for releaseFire() from WM*/ |
---|
[4906] | 401 | if (this->requestedAction != WA_NONE) |
---|
| 402 | return; |
---|
[10529] | 403 | PRINTF(5)("%s: next action will be %s in %f seconds\n", this->getCName(), actionToChar(action), this->stateDuration); |
---|
[4885] | 404 | this->requestedAction = action; |
---|
| 405 | } |
---|
[4906] | 406 | //else |
---|
| 407 | else if (unlikely(action == WA_ACTIVATE)) |
---|
| 408 | { |
---|
| 409 | this->currentState = WS_ACTIVATING; |
---|
[4926] | 410 | this->requestedAction = WA_ACTIVATE; |
---|
[4906] | 411 | } |
---|
[4885] | 412 | } |
---|
[3577] | 413 | |
---|
[6728] | 414 | |
---|
[4890] | 415 | /** |
---|
| 416 | * adds energy to the Weapon |
---|
| 417 | * @param energyToAdd The amount of energy |
---|
| 418 | * @returns the amount of energy we did not pick up, because the weapon is already full |
---|
| 419 | */ |
---|
| 420 | float Weapon::increaseEnergy(float energyToAdd) |
---|
| 421 | { |
---|
| 422 | float maxAddEnergy = this->energyMax - this->energy; |
---|
| 423 | |
---|
| 424 | if (maxAddEnergy >= energyToAdd) |
---|
| 425 | { |
---|
| 426 | this->energy += energyToAdd; |
---|
[10368] | 427 | this->updateWidgets(); |
---|
[4890] | 428 | return 0.0; |
---|
| 429 | } |
---|
| 430 | else |
---|
| 431 | { |
---|
| 432 | this->energy += maxAddEnergy; |
---|
[10368] | 433 | this->updateWidgets(); |
---|
[4890] | 434 | return energyToAdd - maxAddEnergy; |
---|
| 435 | } |
---|
[10443] | 436 | |
---|
[4890] | 437 | } |
---|
| 438 | |
---|
[6728] | 439 | |
---|
[5498] | 440 | //////////////////////////////////////////////////////////// |
---|
| 441 | // WEAPON INTERNALS // |
---|
| 442 | // These are functions, that no other Weapon should over- // |
---|
| 443 | // write. No class has direct Access to them, as it is // |
---|
| 444 | // quite a complicated process, handling a Weapon from // |
---|
| 445 | // the outside // |
---|
| 446 | //////////////////////////////////////////////////////////// |
---|
[4891] | 447 | /** |
---|
| 448 | * executes an action, and with it starts a new State. |
---|
| 449 | * @return true, if it worked, false otherwise |
---|
| 450 | * |
---|
| 451 | * This function checks, wheter the possibility of executing an action is valid, |
---|
| 452 | * and does all the necessary stuff, to execute them. If an action does not succeed, |
---|
| 453 | * it tries to go around it. (ex. shoot->noAmo->reload()->wait until shoot comes again) |
---|
| 454 | */ |
---|
[4885] | 455 | bool Weapon::execute() |
---|
[3583] | 456 | { |
---|
[7729] | 457 | #if DEBUG_LEVEL > 4 |
---|
[4885] | 458 | PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction)); |
---|
| 459 | this->debug(); |
---|
[4906] | 460 | #endif |
---|
[4885] | 461 | |
---|
[4926] | 462 | WeaponAction action = this->requestedAction; |
---|
| 463 | this->requestedAction = WA_NONE; |
---|
| 464 | |
---|
| 465 | switch (action) |
---|
[4885] | 466 | { |
---|
[7350] | 467 | case WA_SHOOT: |
---|
[9869] | 468 | return this->fireW(); |
---|
| 469 | break; |
---|
[7350] | 470 | case WA_CHARGE: |
---|
[9869] | 471 | return this->chargeW(); |
---|
| 472 | break; |
---|
[7350] | 473 | case WA_RELOAD: |
---|
[9869] | 474 | return this->reloadW(); |
---|
| 475 | break; |
---|
[7350] | 476 | case WA_DEACTIVATE: |
---|
[9869] | 477 | return this->deactivateW(); |
---|
| 478 | break; |
---|
[7350] | 479 | case WA_ACTIVATE: |
---|
[9869] | 480 | return this->activateW(); |
---|
| 481 | break; |
---|
[8316] | 482 | default: |
---|
[10368] | 483 | PRINTF(5)("Action %s Not Implemented yet \n", Weapon::actionToChar(action)); |
---|
[9869] | 484 | return false; |
---|
[4885] | 485 | } |
---|
[3583] | 486 | } |
---|
[3577] | 487 | |
---|
[4597] | 488 | /** |
---|
[4894] | 489 | * checks and activates the Weapon. |
---|
| 490 | * @return true on success. |
---|
[4892] | 491 | */ |
---|
| 492 | bool Weapon::activateW() |
---|
[3583] | 493 | { |
---|
[6438] | 494 | // if (this->currentState == WS_INACTIVE) |
---|
[4892] | 495 | { |
---|
[6433] | 496 | // play Sound |
---|
[9869] | 497 | if (likely(this->soundBuffers[WA_ACTIVATE].loaded())) |
---|
[4892] | 498 | this->soundSource->play(this->soundBuffers[WA_ACTIVATE]); |
---|
[6444] | 499 | this->updateWidgets(); |
---|
[6438] | 500 | // activate |
---|
[9406] | 501 | PRINTF(4)("Activating the Weapon %s\n", this->getCName()); |
---|
[4892] | 502 | this->activate(); |
---|
[4895] | 503 | // setting up for next action |
---|
[4926] | 504 | this->enterState(WS_ACTIVATING); |
---|
[4892] | 505 | } |
---|
[8316] | 506 | return true; |
---|
[3583] | 507 | } |
---|
[3577] | 508 | |
---|
[4597] | 509 | /** |
---|
[4894] | 510 | * checks and deactivates the Weapon |
---|
| 511 | * @return true on success. |
---|
[4892] | 512 | */ |
---|
| 513 | bool Weapon::deactivateW() |
---|
[3583] | 514 | { |
---|
[6438] | 515 | // if (this->currentState != WS_INACTIVE) |
---|
[4892] | 516 | { |
---|
[9406] | 517 | PRINTF(4)("Deactivating the Weapon %s\n", this->getCName()); |
---|
[6438] | 518 | // play Sound |
---|
[9869] | 519 | if (this->soundBuffers[WA_DEACTIVATE].loaded()) |
---|
[4892] | 520 | this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]); |
---|
[4926] | 521 | // deactivate |
---|
[4892] | 522 | this->deactivate(); |
---|
[4926] | 523 | this->enterState(WS_DEACTIVATING); |
---|
[4892] | 524 | } |
---|
[8316] | 525 | |
---|
| 526 | return true; |
---|
[3583] | 527 | } |
---|
[3577] | 528 | |
---|
[4892] | 529 | /** |
---|
[4894] | 530 | * checks and charges the Weapon |
---|
| 531 | * @return true on success. |
---|
[4892] | 532 | */ |
---|
| 533 | bool Weapon::chargeW() |
---|
[4885] | 534 | { |
---|
[6671] | 535 | if ( this->currentState != WS_INACTIVE && this->energy >= this->minCharge) |
---|
[4892] | 536 | { |
---|
[6438] | 537 | // playing Sound |
---|
[9869] | 538 | if (this->soundBuffers[WA_CHARGE].loaded()) |
---|
[4892] | 539 | this->soundSource->play(this->soundBuffers[WA_CHARGE]); |
---|
[4893] | 540 | |
---|
[6438] | 541 | // charge |
---|
[4892] | 542 | this->charge(); |
---|
[6438] | 543 | // setting up for the next state |
---|
[4926] | 544 | this->enterState(WS_CHARGING); |
---|
[4892] | 545 | } |
---|
| 546 | else // deactivate the Weapon if we do not have enough energy |
---|
| 547 | { |
---|
| 548 | this->requestAction(WA_RELOAD); |
---|
| 549 | } |
---|
[8316] | 550 | return true; |
---|
[4885] | 551 | } |
---|
[3573] | 552 | |
---|
[4892] | 553 | /** |
---|
[4894] | 554 | * checks and fires the Weapon |
---|
| 555 | * @return true on success. |
---|
[4892] | 556 | */ |
---|
| 557 | bool Weapon::fireW() |
---|
[3575] | 558 | { |
---|
[10368] | 559 | // printf("fireW Weapon\n"); |
---|
[6438] | 560 | //if (likely(this->currentState != WS_INACTIVE)) |
---|
[6671] | 561 | if (this->minCharge <= this->energy) |
---|
[4892] | 562 | { |
---|
[6438] | 563 | // playing Sound |
---|
[9869] | 564 | if (this->soundBuffers[WA_SHOOT].loaded()) |
---|
[4892] | 565 | this->soundSource->play(this->soundBuffers[WA_SHOOT]); |
---|
[6438] | 566 | // fire |
---|
[6671] | 567 | this->energy -= this->minCharge; |
---|
[4892] | 568 | this->fire(); |
---|
[6438] | 569 | // setting up for the next state |
---|
[4926] | 570 | this->enterState(WS_SHOOTING); |
---|
[10368] | 571 | this->updateWidgets(); |
---|
[4892] | 572 | } |
---|
| 573 | else // reload if we still have the charge |
---|
| 574 | { |
---|
| 575 | this->requestAction(WA_RELOAD); |
---|
[4930] | 576 | this->execute(); |
---|
[4892] | 577 | } |
---|
[8316] | 578 | return true; |
---|
[4892] | 579 | } |
---|
| 580 | |
---|
| 581 | /** |
---|
[4894] | 582 | * checks and Reloads the Weapon |
---|
| 583 | * @return true on success. |
---|
[4892] | 584 | */ |
---|
| 585 | bool Weapon::reloadW() |
---|
| 586 | { |
---|
[9406] | 587 | PRINTF(4)("Reloading Weapon %s\n", this->getCName()); |
---|
[9869] | 588 | if (!this->ammoContainer.isNull() && |
---|
[7350] | 589 | unlikely(this->energy + this->ammoContainer->getStoredEnergy() < this->minCharge)) |
---|
[4885] | 590 | { |
---|
[10368] | 591 | //this->requestAction(WA_DEACTIVATE); |
---|
[4930] | 592 | this->execute(); |
---|
[4892] | 593 | return false; |
---|
[4885] | 594 | } |
---|
[3573] | 595 | |
---|
| 596 | |
---|
[9869] | 597 | if (this->soundBuffers[WA_RELOAD].loaded()) |
---|
[4892] | 598 | this->soundSource->play(this->soundBuffers[WA_RELOAD]); |
---|
| 599 | |
---|
[9869] | 600 | if (!this->ammoContainer.isNull()) |
---|
[6671] | 601 | this->ammoContainer->fillWeapon(this); |
---|
[4885] | 602 | else |
---|
| 603 | { |
---|
[6671] | 604 | this->energy = this->energyMax; |
---|
[4885] | 605 | } |
---|
[6444] | 606 | this->updateWidgets(); |
---|
[4892] | 607 | this->reload(); |
---|
[4926] | 608 | this->enterState(WS_RELOADING); |
---|
[8316] | 609 | |
---|
| 610 | return true; |
---|
[4926] | 611 | } |
---|
[3575] | 612 | |
---|
[4926] | 613 | /** |
---|
| 614 | * enters the requested State, plays back animations updates the timing. |
---|
| 615 | * @param state the state to enter. |
---|
| 616 | */ |
---|
| 617 | inline void Weapon::enterState(WeaponState state) |
---|
| 618 | { |
---|
[5041] | 619 | PRINTF(4)("ENTERING STATE %s\n", stateToChar(state)); |
---|
[4926] | 620 | // playing animation if availiable |
---|
| 621 | if (likely(this->animation[state] != NULL)) |
---|
| 622 | this->animation[state]->replay(); |
---|
| 623 | |
---|
[6728] | 624 | this->stateDuration += this->times[state]; |
---|
[4926] | 625 | this->currentState = state; |
---|
[3575] | 626 | } |
---|
| 627 | |
---|
[4927] | 628 | /////////////////// |
---|
| 629 | // WORLD-ENTITY // |
---|
| 630 | // FUNCTIONALITY // |
---|
| 631 | /////////////////// |
---|
[3575] | 632 | /** |
---|
[4885] | 633 | * tick signal for time dependent/driven stuff |
---|
[3575] | 634 | */ |
---|
[6736] | 635 | bool Weapon::tickW(float dt) |
---|
[4885] | 636 | { |
---|
[4934] | 637 | //printf("%s ", stateToChar(this->currentState)); |
---|
[4910] | 638 | |
---|
[4885] | 639 | // setting up the timing properties |
---|
| 640 | this->stateDuration -= dt; |
---|
[3575] | 641 | |
---|
[4949] | 642 | if (this->stateDuration <= 0.0) |
---|
[4885] | 643 | { |
---|
[4949] | 644 | if (unlikely (this->currentState == WS_DEACTIVATING)) |
---|
[4885] | 645 | { |
---|
[4949] | 646 | this->currentState = WS_INACTIVE; |
---|
[6736] | 647 | return false; |
---|
[4949] | 648 | } |
---|
| 649 | else |
---|
| 650 | this->currentState = WS_IDLE; |
---|
[4906] | 651 | |
---|
[4949] | 652 | if (this->requestedAction != WA_NONE) |
---|
| 653 | { |
---|
| 654 | this->stateDuration = -dt; |
---|
| 655 | this->execute(); |
---|
[4885] | 656 | } |
---|
| 657 | } |
---|
[6736] | 658 | return true; |
---|
[4885] | 659 | } |
---|
| 660 | |
---|
[3575] | 661 | |
---|
| 662 | |
---|
| 663 | |
---|
[4885] | 664 | ////////////////////// |
---|
| 665 | // HELPER FUNCTIONS // |
---|
| 666 | ////////////////////// |
---|
[3576] | 667 | /** |
---|
[4891] | 668 | * checks wether all the Weapons functions are valid, and if it is possible to go to action with it. |
---|
[5498] | 669 | * @todo IMPLEMENT the Weapons Check |
---|
[4891] | 670 | */ |
---|
| 671 | bool Weapon::check() const |
---|
| 672 | { |
---|
| 673 | bool retVal = true; |
---|
| 674 | |
---|
[6438] | 675 | // if (this->projectile == NULL) |
---|
[4891] | 676 | { |
---|
[5041] | 677 | PRINTF(1)("There was no projectile assigned to the Weapon.\n"); |
---|
[4891] | 678 | retVal = false; |
---|
| 679 | } |
---|
| 680 | |
---|
| 681 | |
---|
| 682 | |
---|
| 683 | |
---|
| 684 | return retVal; |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | /** |
---|
[4885] | 688 | * some nice debugging information about this Weapon |
---|
| 689 | */ |
---|
| 690 | void Weapon::debug() const |
---|
| 691 | { |
---|
[9406] | 692 | PRINT(0)("Weapon-Debug %s, state: %s (duration: %fs), nextAction: %s\n", this->getCName(), Weapon::stateToChar(this->currentState), this->stateDuration, Weapon::actionToChar(requestedAction)); |
---|
[6671] | 693 | PRINT(0)("Energy: max: %f; current: %f; chargeMin: %f, chargeMax %f\n", |
---|
| 694 | this->energyMax, this->energy, this->minCharge, this->maxCharge); |
---|
[4967] | 695 | |
---|
| 696 | |
---|
[4885] | 697 | } |
---|
[3575] | 698 | |
---|
[5498] | 699 | //////////////////////////////////////////////////////// |
---|
| 700 | // static Definitions (transormators for readability) // |
---|
| 701 | //////////////////////////////////////////////////////// |
---|
[4885] | 702 | /** |
---|
| 703 | * Converts a String into an Action. |
---|
| 704 | * @param action the String input holding the Action. |
---|
| 705 | * @return The Action if known, WA_NONE otherwise. |
---|
| 706 | */ |
---|
[7221] | 707 | WeaponAction Weapon::charToAction(const std::string& action) |
---|
[4885] | 708 | { |
---|
[7221] | 709 | if (action == "none") |
---|
[4885] | 710 | return WA_NONE; |
---|
[7221] | 711 | else if (action == "shoot") |
---|
[4885] | 712 | return WA_SHOOT; |
---|
[7221] | 713 | else if (action == "charge") |
---|
[4885] | 714 | return WA_CHARGE; |
---|
[7221] | 715 | else if (action == "reload") |
---|
[4885] | 716 | return WA_RELOAD; |
---|
[7221] | 717 | else if (action == "acitvate") |
---|
[4885] | 718 | return WA_ACTIVATE; |
---|
[7221] | 719 | else if (action == "deactivate") |
---|
[4885] | 720 | return WA_DEACTIVATE; |
---|
[7221] | 721 | else if (action == "special1") |
---|
[4885] | 722 | return WA_SPECIAL1; |
---|
| 723 | else |
---|
[6438] | 724 | { |
---|
[7221] | 725 | PRINTF(2)("action %s could not be identified.\n", action.c_str()); |
---|
[6438] | 726 | return WA_NONE; |
---|
| 727 | } |
---|
[4885] | 728 | } |
---|
[3575] | 729 | |
---|
| 730 | /** |
---|
[4885] | 731 | * converts an action into a String |
---|
| 732 | * @param action the action to convert |
---|
| 733 | * @return a String matching the name of the action |
---|
| 734 | */ |
---|
| 735 | const char* Weapon::actionToChar(WeaponAction action) |
---|
| 736 | { |
---|
| 737 | switch (action) |
---|
| 738 | { |
---|
[7350] | 739 | case WA_SHOOT: |
---|
[9869] | 740 | return "shoot"; |
---|
| 741 | break; |
---|
[7350] | 742 | case WA_CHARGE: |
---|
[9869] | 743 | return "charge"; |
---|
| 744 | break; |
---|
[7350] | 745 | case WA_RELOAD: |
---|
[9869] | 746 | return "reload"; |
---|
| 747 | break; |
---|
[7350] | 748 | case WA_ACTIVATE: |
---|
[9869] | 749 | return "activate"; |
---|
| 750 | break; |
---|
[7350] | 751 | case WA_DEACTIVATE: |
---|
[9869] | 752 | return "deactivate"; |
---|
| 753 | break; |
---|
[7350] | 754 | case WA_SPECIAL1: |
---|
[9869] | 755 | return "special1"; |
---|
| 756 | break; |
---|
[7350] | 757 | default: |
---|
[9869] | 758 | return "none"; |
---|
| 759 | break; |
---|
[4885] | 760 | } |
---|
| 761 | } |
---|
[3577] | 762 | |
---|
| 763 | /** |
---|
[4885] | 764 | * Converts a String into a State. |
---|
| 765 | * @param state the String input holding the State. |
---|
| 766 | * @return The State if known, WS_NONE otherwise. |
---|
| 767 | */ |
---|
[7221] | 768 | WeaponState Weapon::charToState(const std::string& state) |
---|
[4885] | 769 | { |
---|
[7221] | 770 | if (state == "none") |
---|
[4885] | 771 | return WS_NONE; |
---|
[7221] | 772 | else if (state == "shooting") |
---|
[4885] | 773 | return WS_SHOOTING; |
---|
[7221] | 774 | else if (state == "charging") |
---|
[4885] | 775 | return WS_CHARGING; |
---|
[7221] | 776 | else if (state == "reloading") |
---|
[4885] | 777 | return WS_RELOADING; |
---|
[7221] | 778 | else if (state == "activating") |
---|
[4885] | 779 | return WS_ACTIVATING; |
---|
[7221] | 780 | else if (state == "deactivating") |
---|
[4885] | 781 | return WS_DEACTIVATING; |
---|
[7221] | 782 | else if (state == "inactive") |
---|
[4885] | 783 | return WS_INACTIVE; |
---|
[7221] | 784 | else if (state == "idle") |
---|
[4885] | 785 | return WS_IDLE; |
---|
| 786 | else |
---|
[6438] | 787 | { |
---|
[7221] | 788 | PRINTF(2)("state %s could not be identified.\n", state.c_str()); |
---|
[6438] | 789 | return WS_NONE; |
---|
| 790 | } |
---|
[4885] | 791 | } |
---|
[3583] | 792 | |
---|
| 793 | /** |
---|
[4885] | 794 | * converts a State into a String |
---|
| 795 | * @param state the state to convert |
---|
| 796 | * @return a String matching the name of the state |
---|
| 797 | */ |
---|
| 798 | const char* Weapon::stateToChar(WeaponState state) |
---|
| 799 | { |
---|
| 800 | switch (state) |
---|
| 801 | { |
---|
[7350] | 802 | case WS_SHOOTING: |
---|
[9869] | 803 | return "shooting"; |
---|
| 804 | break; |
---|
[7350] | 805 | case WS_CHARGING: |
---|
[9869] | 806 | return "charging"; |
---|
| 807 | break; |
---|
[7350] | 808 | case WS_RELOADING: |
---|
[9869] | 809 | return "reloading"; |
---|
| 810 | break; |
---|
[7350] | 811 | case WS_ACTIVATING: |
---|
[9869] | 812 | return "activating"; |
---|
| 813 | break; |
---|
[7350] | 814 | case WS_DEACTIVATING: |
---|
[9869] | 815 | return "deactivating"; |
---|
| 816 | break; |
---|
[7350] | 817 | case WS_IDLE: |
---|
[9869] | 818 | return "idle"; |
---|
| 819 | break; |
---|
[7350] | 820 | case WS_INACTIVE: |
---|
[9869] | 821 | return "inactive"; |
---|
| 822 | break; |
---|
[7350] | 823 | default: |
---|
[9869] | 824 | return "none"; |
---|
| 825 | break; |
---|
[4885] | 826 | } |
---|
| 827 | } |
---|