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