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