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