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