[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 | */ |
---|
[7102] | 167 | void Weapon::setProjectileTypeC(const char* projectile) |
---|
[4950] | 168 | { |
---|
| 169 | if (projectile == NULL) |
---|
[4972] | 170 | return; |
---|
[4950] | 171 | FastFactory* tmpFac = FastFactory::searchFastFactory(projectile); |
---|
| 172 | if (tmpFac != NULL) |
---|
| 173 | { |
---|
[5356] | 174 | this->setProjectileType(tmpFac->getStoredID()); |
---|
[4950] | 175 | } |
---|
[4972] | 176 | else |
---|
| 177 | { |
---|
[5356] | 178 | PRINTF(1)("Projectile %s does not exist for weapon %s\n", projectile, this->getName()); |
---|
[4972] | 179 | } |
---|
[4950] | 180 | } |
---|
| 181 | |
---|
[6728] | 182 | |
---|
[4950] | 183 | /** |
---|
[5356] | 184 | * prepares Projectiles of the Weapon |
---|
[5498] | 185 | * @param count how many Projectiles to create (they will be stored in the ProjectileFactory) |
---|
[5356] | 186 | */ |
---|
| 187 | void Weapon::prepareProjectiles(unsigned int count) |
---|
| 188 | { |
---|
[5357] | 189 | if (likely(this->projectileFactory != NULL)) |
---|
[5356] | 190 | projectileFactory->prepare(count); |
---|
| 191 | else |
---|
[5357] | 192 | PRINTF(2)("unable to create %d projectile for Weapon %s (%s)\n", count, this->getName(), this->getClassName()); |
---|
[5356] | 193 | } |
---|
| 194 | |
---|
[6728] | 195 | |
---|
[5356] | 196 | /** |
---|
| 197 | * resurects and returns a Projectile |
---|
[5498] | 198 | * @returns a Projectile on success, NULL on error |
---|
| 199 | * |
---|
| 200 | * errors: 1. (ProjectileFastFactory not Found) |
---|
| 201 | * 2. No more Projectiles availiable. |
---|
[5356] | 202 | */ |
---|
| 203 | Projectile* Weapon::getProjectile() |
---|
| 204 | { |
---|
[5357] | 205 | if (likely (this->projectileFactory != NULL)) |
---|
[6142] | 206 | { |
---|
| 207 | Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect()); |
---|
| 208 | pj->toList((OM_LIST)(this->getOMListNumber()+1)); |
---|
| 209 | return pj; |
---|
| 210 | } |
---|
[5356] | 211 | else |
---|
| 212 | { |
---|
[5498] | 213 | PRINTF(2)("No projectile defined for Weapon %s(%s) can't return any\n", this->getName(), this->getClassName()); |
---|
[5356] | 214 | return NULL; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | /** |
---|
[4892] | 220 | * sets the emissionPoint's relative position from the Weapon |
---|
| 221 | * @param point the Point relative to the mass-point of the Weapon |
---|
| 222 | */ |
---|
| 223 | void Weapon::setEmissionPoint(const Vector& point) |
---|
| 224 | { |
---|
| 225 | this->emissionPoint.setRelCoor(point); |
---|
| 226 | } |
---|
| 227 | |
---|
[6728] | 228 | |
---|
[4892] | 229 | /** |
---|
[4891] | 230 | * assigns a Sound-file to an action |
---|
| 231 | * @param action the action the sound should be assigned too |
---|
| 232 | * @param soundFile the soundFile's relative position to the data-directory (will be looked for by the ResourceManager) |
---|
| 233 | */ |
---|
[4885] | 234 | void Weapon::setActionSound(WeaponAction action, const char* soundFile) |
---|
| 235 | { |
---|
| 236 | if (action >= WA_ACTION_COUNT) |
---|
| 237 | return; |
---|
[4930] | 238 | if (this->soundBuffers[action] != NULL) |
---|
| 239 | ResourceManager::getInstance()->unload(this->soundBuffers[action]); |
---|
| 240 | |
---|
[4885] | 241 | else if (soundFile != NULL) |
---|
| 242 | { |
---|
| 243 | this->soundBuffers[action] = (SoundBuffer*)ResourceManager::getInstance()->load(soundFile, WAV); |
---|
| 244 | if (this->soundBuffers[action] != NULL) |
---|
| 245 | { |
---|
[4967] | 246 | PRINTF(4)("Loaded sound %s to action %s.\n", soundFile, actionToChar(action)); |
---|
[4885] | 247 | } |
---|
| 248 | else |
---|
| 249 | { |
---|
[5041] | 250 | PRINTF(2)("Failed to load sound %s to %s.\n.", soundFile, actionToChar(action)); |
---|
[4885] | 251 | } |
---|
| 252 | } |
---|
| 253 | else |
---|
| 254 | this->soundBuffers[action] = NULL; |
---|
| 255 | } |
---|
| 256 | |
---|
[6728] | 257 | |
---|
[4893] | 258 | /** |
---|
[4895] | 259 | * creates/returns an Animation3D for a certain State. |
---|
| 260 | * @param state what State should the Animation be created/returned for |
---|
| 261 | * @param node the node this Animation should apply to. (NULL is fine if the animation was already created) |
---|
| 262 | * @returns The created animation.Animation(), NULL on error (or if the animation does not yet exist). |
---|
[4893] | 263 | * |
---|
| 264 | * This function does only generate the Animation Object, and if set it will |
---|
| 265 | * automatically be executed, when a certain State is reached. |
---|
| 266 | * What this does not do, is set keyframes, you have to operate on the returned animation. |
---|
| 267 | */ |
---|
[4895] | 268 | Animation3D* Weapon::getAnimation(WeaponState state, PNode* node) |
---|
[4893] | 269 | { |
---|
[4895] | 270 | if (state >= WS_STATE_COUNT) // if the state is not known |
---|
[4893] | 271 | return NULL; |
---|
| 272 | |
---|
[4895] | 273 | if (unlikely(this->animation[state] == NULL)) // if the animation does not exist yet create it. |
---|
[4893] | 274 | { |
---|
[4895] | 275 | if (likely(node != NULL)) |
---|
| 276 | return this->animation[state] = new Animation3D(node); |
---|
| 277 | else |
---|
| 278 | { |
---|
| 279 | PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state)); |
---|
| 280 | return NULL; |
---|
| 281 | } |
---|
[4893] | 282 | } |
---|
[4895] | 283 | else |
---|
| 284 | return this->animation[state]; |
---|
[4893] | 285 | } |
---|
| 286 | |
---|
[6438] | 287 | GLGuiWidget* Weapon::getEnergyWidget() |
---|
| 288 | { |
---|
| 289 | if (this->energyWidget == NULL) |
---|
| 290 | { |
---|
| 291 | this->energyWidget = new GLGuiBar; |
---|
| 292 | this->energyWidget->setSize2D( 20, 100); |
---|
| 293 | this->energyWidget->setMaximum(this->getEnergyMax()); |
---|
| 294 | this->energyWidget->setValue(this->getEnergy()); |
---|
| 295 | } |
---|
| 296 | return this->energyWidget; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | void Weapon::updateWidgets() |
---|
| 300 | { |
---|
| 301 | if (this->energyWidget != NULL) |
---|
| 302 | { |
---|
| 303 | this->energyWidget->setMaximum(this->energyMax); |
---|
| 304 | this->energyWidget->setValue(this->energy); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
[4892] | 308 | ///////////////// |
---|
| 309 | // EXECUTION // |
---|
| 310 | // GAME ACTION // |
---|
| 311 | ///////////////// |
---|
[4597] | 312 | /** |
---|
[4885] | 313 | * request an action that should be executed, |
---|
| 314 | * @param action the next action to take |
---|
| 315 | * |
---|
| 316 | * This function must be called instead of the actions (like fire/reload...) |
---|
| 317 | * to make all the checks needed to have a usefull WeaponSystem. |
---|
| 318 | */ |
---|
| 319 | void Weapon::requestAction(WeaponAction action) |
---|
| 320 | { |
---|
[4906] | 321 | if (likely(this->isActive())) |
---|
[4885] | 322 | { |
---|
[4906] | 323 | if (this->requestedAction != WA_NONE) |
---|
| 324 | return; |
---|
[5041] | 325 | PRINTF(5)("next action will be %s in %f seconds\n", actionToChar(action), this->stateDuration); |
---|
[4885] | 326 | this->requestedAction = action; |
---|
| 327 | } |
---|
[4906] | 328 | //else |
---|
| 329 | else if (unlikely(action == WA_ACTIVATE)) |
---|
| 330 | { |
---|
| 331 | this->currentState = WS_ACTIVATING; |
---|
[4926] | 332 | this->requestedAction = WA_ACTIVATE; |
---|
[4906] | 333 | } |
---|
[4885] | 334 | } |
---|
[3577] | 335 | |
---|
[6728] | 336 | |
---|
[4890] | 337 | /** |
---|
| 338 | * adds energy to the Weapon |
---|
| 339 | * @param energyToAdd The amount of energy |
---|
| 340 | * @returns the amount of energy we did not pick up, because the weapon is already full |
---|
| 341 | */ |
---|
| 342 | float Weapon::increaseEnergy(float energyToAdd) |
---|
| 343 | { |
---|
| 344 | float maxAddEnergy = this->energyMax - this->energy; |
---|
| 345 | |
---|
| 346 | if (maxAddEnergy >= energyToAdd) |
---|
| 347 | { |
---|
| 348 | this->energy += energyToAdd; |
---|
| 349 | return 0.0; |
---|
| 350 | } |
---|
| 351 | else |
---|
| 352 | { |
---|
| 353 | this->energy += maxAddEnergy; |
---|
| 354 | return energyToAdd - maxAddEnergy; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
[6728] | 358 | |
---|
[5498] | 359 | //////////////////////////////////////////////////////////// |
---|
| 360 | // WEAPON INTERNALS // |
---|
| 361 | // These are functions, that no other Weapon should over- // |
---|
| 362 | // write. No class has direct Access to them, as it is // |
---|
| 363 | // quite a complicated process, handling a Weapon from // |
---|
| 364 | // the outside // |
---|
| 365 | //////////////////////////////////////////////////////////// |
---|
[4891] | 366 | /** |
---|
| 367 | * executes an action, and with it starts a new State. |
---|
| 368 | * @return true, if it worked, false otherwise |
---|
| 369 | * |
---|
| 370 | * This function checks, wheter the possibility of executing an action is valid, |
---|
| 371 | * and does all the necessary stuff, to execute them. If an action does not succeed, |
---|
| 372 | * it tries to go around it. (ex. shoot->noAmo->reload()->wait until shoot comes again) |
---|
| 373 | */ |
---|
[4885] | 374 | bool Weapon::execute() |
---|
[3583] | 375 | { |
---|
[4906] | 376 | #if DEBUG > 4 |
---|
[4885] | 377 | PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction)); |
---|
| 378 | this->debug(); |
---|
[4906] | 379 | #endif |
---|
[4885] | 380 | |
---|
[4926] | 381 | WeaponAction action = this->requestedAction; |
---|
| 382 | this->requestedAction = WA_NONE; |
---|
| 383 | |
---|
| 384 | switch (action) |
---|
[4885] | 385 | { |
---|
[6671] | 386 | case WA_SHOOT: |
---|
| 387 | return this->fireW(); |
---|
| 388 | break; |
---|
| 389 | case WA_CHARGE: |
---|
| 390 | return this->chargeW(); |
---|
| 391 | break; |
---|
| 392 | case WA_RELOAD: |
---|
| 393 | return this->reloadW(); |
---|
| 394 | break; |
---|
| 395 | case WA_DEACTIVATE: |
---|
| 396 | return this->deactivateW(); |
---|
| 397 | break; |
---|
| 398 | case WA_ACTIVATE: |
---|
| 399 | return this->activateW(); |
---|
| 400 | break; |
---|
[4885] | 401 | } |
---|
[3583] | 402 | } |
---|
[3577] | 403 | |
---|
[4597] | 404 | /** |
---|
[4894] | 405 | * checks and activates the Weapon. |
---|
| 406 | * @return true on success. |
---|
[4892] | 407 | */ |
---|
| 408 | bool Weapon::activateW() |
---|
[3583] | 409 | { |
---|
[6438] | 410 | // if (this->currentState == WS_INACTIVE) |
---|
[4892] | 411 | { |
---|
[6433] | 412 | // play Sound |
---|
[4893] | 413 | if (likely(this->soundBuffers[WA_ACTIVATE] != NULL)) |
---|
[4892] | 414 | this->soundSource->play(this->soundBuffers[WA_ACTIVATE]); |
---|
[6444] | 415 | this->updateWidgets(); |
---|
[6438] | 416 | // activate |
---|
[4895] | 417 | PRINTF(4)("Activating the Weapon %s\n", this->getName()); |
---|
[4892] | 418 | this->activate(); |
---|
[4895] | 419 | // setting up for next action |
---|
[4926] | 420 | this->enterState(WS_ACTIVATING); |
---|
[4892] | 421 | } |
---|
[3583] | 422 | } |
---|
[3577] | 423 | |
---|
[4597] | 424 | /** |
---|
[4894] | 425 | * checks and deactivates the Weapon |
---|
| 426 | * @return true on success. |
---|
[4892] | 427 | */ |
---|
| 428 | bool Weapon::deactivateW() |
---|
[3583] | 429 | { |
---|
[6438] | 430 | // if (this->currentState != WS_INACTIVE) |
---|
[4892] | 431 | { |
---|
| 432 | PRINTF(4)("Deactivating the Weapon %s\n", this->getName()); |
---|
[6438] | 433 | // play Sound |
---|
[4892] | 434 | if (this->soundBuffers[WA_DEACTIVATE] != NULL) |
---|
| 435 | this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]); |
---|
[4926] | 436 | // deactivate |
---|
[4892] | 437 | this->deactivate(); |
---|
[4926] | 438 | this->enterState(WS_DEACTIVATING); |
---|
[4892] | 439 | } |
---|
[3583] | 440 | } |
---|
[3577] | 441 | |
---|
[4892] | 442 | /** |
---|
[4894] | 443 | * checks and charges the Weapon |
---|
| 444 | * @return true on success. |
---|
[4892] | 445 | */ |
---|
| 446 | bool Weapon::chargeW() |
---|
[4885] | 447 | { |
---|
[6671] | 448 | if ( this->currentState != WS_INACTIVE && this->energy >= this->minCharge) |
---|
[4892] | 449 | { |
---|
[6438] | 450 | // playing Sound |
---|
[4892] | 451 | if (this->soundBuffers[WA_CHARGE] != NULL) |
---|
| 452 | this->soundSource->play(this->soundBuffers[WA_CHARGE]); |
---|
[4893] | 453 | |
---|
[6438] | 454 | // charge |
---|
[4892] | 455 | this->charge(); |
---|
[6438] | 456 | // setting up for the next state |
---|
[4926] | 457 | this->enterState(WS_CHARGING); |
---|
[4892] | 458 | } |
---|
| 459 | else // deactivate the Weapon if we do not have enough energy |
---|
| 460 | { |
---|
| 461 | this->requestAction(WA_RELOAD); |
---|
| 462 | } |
---|
[4885] | 463 | } |
---|
[3573] | 464 | |
---|
[4892] | 465 | /** |
---|
[4894] | 466 | * checks and fires the Weapon |
---|
| 467 | * @return true on success. |
---|
[4892] | 468 | */ |
---|
| 469 | bool Weapon::fireW() |
---|
[3575] | 470 | { |
---|
[6438] | 471 | //if (likely(this->currentState != WS_INACTIVE)) |
---|
[6671] | 472 | if (this->minCharge <= this->energy) |
---|
[4892] | 473 | { |
---|
[6438] | 474 | // playing Sound |
---|
[4892] | 475 | if (this->soundBuffers[WA_SHOOT] != NULL) |
---|
| 476 | this->soundSource->play(this->soundBuffers[WA_SHOOT]); |
---|
[6444] | 477 | this->updateWidgets(); |
---|
[6438] | 478 | // fire |
---|
[6671] | 479 | this->energy -= this->minCharge; |
---|
[4892] | 480 | this->fire(); |
---|
[6438] | 481 | // setting up for the next state |
---|
[4926] | 482 | this->enterState(WS_SHOOTING); |
---|
[4892] | 483 | } |
---|
| 484 | else // reload if we still have the charge |
---|
| 485 | { |
---|
| 486 | this->requestAction(WA_RELOAD); |
---|
[4930] | 487 | this->execute(); |
---|
[4892] | 488 | } |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | /** |
---|
[4894] | 492 | * checks and Reloads the Weapon |
---|
| 493 | * @return true on success. |
---|
[4892] | 494 | */ |
---|
| 495 | bool Weapon::reloadW() |
---|
| 496 | { |
---|
[4885] | 497 | PRINTF(4)("Reloading Weapon %s\n", this->getName()); |
---|
[6671] | 498 | if (this->ammoContainer.get() != NULL && |
---|
| 499 | unlikely(this->energy + this->ammoContainer->getStoredEnergy() < this->minCharge)) |
---|
[4885] | 500 | { |
---|
| 501 | this->requestAction(WA_DEACTIVATE); |
---|
[4930] | 502 | this->execute(); |
---|
[4892] | 503 | return false; |
---|
[4885] | 504 | } |
---|
[3573] | 505 | |
---|
| 506 | |
---|
[4892] | 507 | if (this->soundBuffers[WA_RELOAD] != NULL) |
---|
| 508 | this->soundSource->play(this->soundBuffers[WA_RELOAD]); |
---|
| 509 | |
---|
[6671] | 510 | if (this->ammoContainer.get() != NULL) |
---|
| 511 | this->ammoContainer->fillWeapon(this); |
---|
[4885] | 512 | else |
---|
| 513 | { |
---|
[6671] | 514 | this->energy = this->energyMax; |
---|
[4885] | 515 | } |
---|
[6444] | 516 | this->updateWidgets(); |
---|
[4892] | 517 | this->reload(); |
---|
[4926] | 518 | this->enterState(WS_RELOADING); |
---|
| 519 | } |
---|
[3575] | 520 | |
---|
[4926] | 521 | /** |
---|
| 522 | * enters the requested State, plays back animations updates the timing. |
---|
| 523 | * @param state the state to enter. |
---|
| 524 | */ |
---|
| 525 | inline void Weapon::enterState(WeaponState state) |
---|
| 526 | { |
---|
[5041] | 527 | PRINTF(4)("ENTERING STATE %s\n", stateToChar(state)); |
---|
[4926] | 528 | // playing animation if availiable |
---|
| 529 | if (likely(this->animation[state] != NULL)) |
---|
| 530 | this->animation[state]->replay(); |
---|
| 531 | |
---|
[6728] | 532 | this->stateDuration += this->times[state]; |
---|
[4926] | 533 | this->currentState = state; |
---|
[3575] | 534 | } |
---|
| 535 | |
---|
[4927] | 536 | /////////////////// |
---|
| 537 | // WORLD-ENTITY // |
---|
| 538 | // FUNCTIONALITY // |
---|
| 539 | /////////////////// |
---|
[3575] | 540 | /** |
---|
[4885] | 541 | * tick signal for time dependent/driven stuff |
---|
[3575] | 542 | */ |
---|
[6736] | 543 | bool Weapon::tickW(float dt) |
---|
[4885] | 544 | { |
---|
[4934] | 545 | //printf("%s ", stateToChar(this->currentState)); |
---|
[4910] | 546 | |
---|
[4885] | 547 | // setting up the timing properties |
---|
| 548 | this->stateDuration -= dt; |
---|
[3575] | 549 | |
---|
[4949] | 550 | if (this->stateDuration <= 0.0) |
---|
[4885] | 551 | { |
---|
[4949] | 552 | if (unlikely (this->currentState == WS_DEACTIVATING)) |
---|
[4885] | 553 | { |
---|
[4949] | 554 | this->currentState = WS_INACTIVE; |
---|
[6736] | 555 | return false; |
---|
[4949] | 556 | } |
---|
| 557 | else |
---|
| 558 | this->currentState = WS_IDLE; |
---|
[4906] | 559 | |
---|
[4949] | 560 | if (this->requestedAction != WA_NONE) |
---|
| 561 | { |
---|
| 562 | this->stateDuration = -dt; |
---|
| 563 | this->execute(); |
---|
[4885] | 564 | } |
---|
| 565 | } |
---|
[6736] | 566 | return true; |
---|
[4885] | 567 | } |
---|
| 568 | |
---|
[3575] | 569 | |
---|
| 570 | |
---|
| 571 | |
---|
[4885] | 572 | ////////////////////// |
---|
| 573 | // HELPER FUNCTIONS // |
---|
| 574 | ////////////////////// |
---|
[3576] | 575 | /** |
---|
[4891] | 576 | * checks wether all the Weapons functions are valid, and if it is possible to go to action with it. |
---|
[5498] | 577 | * @todo IMPLEMENT the Weapons Check |
---|
[4891] | 578 | */ |
---|
| 579 | bool Weapon::check() const |
---|
| 580 | { |
---|
| 581 | bool retVal = true; |
---|
| 582 | |
---|
[6438] | 583 | // if (this->projectile == NULL) |
---|
[4891] | 584 | { |
---|
[5041] | 585 | PRINTF(1)("There was no projectile assigned to the Weapon.\n"); |
---|
[4891] | 586 | retVal = false; |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | |
---|
| 590 | |
---|
| 591 | |
---|
| 592 | return retVal; |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | /** |
---|
[4885] | 596 | * some nice debugging information about this Weapon |
---|
| 597 | */ |
---|
| 598 | void Weapon::debug() const |
---|
| 599 | { |
---|
[6433] | 600 | PRINT(0)("Weapon-Debug %s, state: %s (duration: %fs), nextAction: %s\n", this->getName(), Weapon::stateToChar(this->currentState), this->stateDuration, Weapon::actionToChar(requestedAction)); |
---|
[6671] | 601 | PRINT(0)("Energy: max: %f; current: %f; chargeMin: %f, chargeMax %f\n", |
---|
| 602 | this->energyMax, this->energy, this->minCharge, this->maxCharge); |
---|
[4967] | 603 | |
---|
| 604 | |
---|
[4885] | 605 | } |
---|
[3575] | 606 | |
---|
[5498] | 607 | //////////////////////////////////////////////////////// |
---|
| 608 | // static Definitions (transormators for readability) // |
---|
| 609 | //////////////////////////////////////////////////////// |
---|
[4885] | 610 | /** |
---|
| 611 | * Converts a String into an Action. |
---|
| 612 | * @param action the String input holding the Action. |
---|
| 613 | * @return The Action if known, WA_NONE otherwise. |
---|
| 614 | */ |
---|
| 615 | WeaponAction Weapon::charToAction(const char* action) |
---|
| 616 | { |
---|
| 617 | if (!strcmp(action, "none")) |
---|
| 618 | return WA_NONE; |
---|
| 619 | else if (!strcmp(action, "shoot")) |
---|
| 620 | return WA_SHOOT; |
---|
| 621 | else if (!strcmp(action, "charge")) |
---|
| 622 | return WA_CHARGE; |
---|
| 623 | else if (!strcmp(action, "reload")) |
---|
| 624 | return WA_RELOAD; |
---|
| 625 | else if (!strcmp(action, "acitvate")) |
---|
| 626 | return WA_ACTIVATE; |
---|
| 627 | else if (!strcmp(action, "deactivate")) |
---|
| 628 | return WA_DEACTIVATE; |
---|
| 629 | else if (!strcmp(action, "special1")) |
---|
| 630 | return WA_SPECIAL1; |
---|
| 631 | else |
---|
[6438] | 632 | { |
---|
| 633 | PRINTF(2)("action %s could not be identified.\n", action); |
---|
| 634 | return WA_NONE; |
---|
| 635 | } |
---|
[4885] | 636 | } |
---|
[3575] | 637 | |
---|
| 638 | /** |
---|
[4885] | 639 | * converts an action into a String |
---|
| 640 | * @param action the action to convert |
---|
| 641 | * @return a String matching the name of the action |
---|
| 642 | */ |
---|
| 643 | const char* Weapon::actionToChar(WeaponAction action) |
---|
| 644 | { |
---|
| 645 | switch (action) |
---|
| 646 | { |
---|
[6671] | 647 | case WA_SHOOT: |
---|
| 648 | return "shoot"; |
---|
| 649 | break; |
---|
| 650 | case WA_CHARGE: |
---|
| 651 | return "charge"; |
---|
| 652 | break; |
---|
| 653 | case WA_RELOAD: |
---|
| 654 | return "reload"; |
---|
| 655 | break; |
---|
| 656 | case WA_ACTIVATE: |
---|
| 657 | return "activate"; |
---|
| 658 | break; |
---|
| 659 | case WA_DEACTIVATE: |
---|
| 660 | return "deactivate"; |
---|
| 661 | break; |
---|
| 662 | case WA_SPECIAL1: |
---|
| 663 | return "special1"; |
---|
| 664 | break; |
---|
| 665 | default: |
---|
| 666 | return "none"; |
---|
| 667 | break; |
---|
[4885] | 668 | } |
---|
| 669 | } |
---|
[3577] | 670 | |
---|
| 671 | /** |
---|
[4885] | 672 | * Converts a String into a State. |
---|
| 673 | * @param state the String input holding the State. |
---|
| 674 | * @return The State if known, WS_NONE otherwise. |
---|
| 675 | */ |
---|
| 676 | WeaponState Weapon::charToState(const char* state) |
---|
| 677 | { |
---|
| 678 | if (!strcmp(state, "none")) |
---|
| 679 | return WS_NONE; |
---|
| 680 | else if (!strcmp(state, "shooting")) |
---|
| 681 | return WS_SHOOTING; |
---|
| 682 | else if (!strcmp(state, "charging")) |
---|
| 683 | return WS_CHARGING; |
---|
| 684 | else if (!strcmp(state, "reloading")) |
---|
| 685 | return WS_RELOADING; |
---|
| 686 | else if (!strcmp(state, "activating")) |
---|
| 687 | return WS_ACTIVATING; |
---|
| 688 | else if (!strcmp(state, "deactivating")) |
---|
| 689 | return WS_DEACTIVATING; |
---|
| 690 | else if (!strcmp(state, "inactive")) |
---|
| 691 | return WS_INACTIVE; |
---|
| 692 | else if (!strcmp(state, "idle")) |
---|
| 693 | return WS_IDLE; |
---|
| 694 | else |
---|
[6438] | 695 | { |
---|
| 696 | PRINTF(2)("state %s could not be identified.\n", state); |
---|
| 697 | return WS_NONE; |
---|
| 698 | } |
---|
[4885] | 699 | } |
---|
[3583] | 700 | |
---|
| 701 | /** |
---|
[4885] | 702 | * converts a State into a String |
---|
| 703 | * @param state the state to convert |
---|
| 704 | * @return a String matching the name of the state |
---|
| 705 | */ |
---|
| 706 | const char* Weapon::stateToChar(WeaponState state) |
---|
| 707 | { |
---|
| 708 | switch (state) |
---|
| 709 | { |
---|
[6671] | 710 | case WS_SHOOTING: |
---|
| 711 | return "shooting"; |
---|
| 712 | break; |
---|
| 713 | case WS_CHARGING: |
---|
| 714 | return "charging"; |
---|
| 715 | break; |
---|
| 716 | case WS_RELOADING: |
---|
| 717 | return "reloading"; |
---|
| 718 | break; |
---|
| 719 | case WS_ACTIVATING: |
---|
| 720 | return "activating"; |
---|
| 721 | break; |
---|
| 722 | case WS_DEACTIVATING: |
---|
| 723 | return "deactivating"; |
---|
| 724 | break; |
---|
| 725 | case WS_IDLE: |
---|
| 726 | return "idle"; |
---|
| 727 | break; |
---|
| 728 | case WS_INACTIVE: |
---|
| 729 | return "inactive"; |
---|
| 730 | break; |
---|
| 731 | default: |
---|
| 732 | return "none"; |
---|
| 733 | break; |
---|
[4885] | 734 | } |
---|
| 735 | } |
---|