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