[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 | |
---|
[5498] | 97 | this->energyLoaded = .0; //< How much energy is loaded in the Gun. (Weapons must be charged befor usage) |
---|
| 98 | this->energyLoadedMax = 5.0; //< Each Weapon has a Maximum energy that can be charged onto it |
---|
| 99 | this->energy = .0; //< The secondary Buffer (before we have to reload) |
---|
| 100 | this->energyMax = 10.0; //< How much energy can be carried |
---|
| 101 | this->capability = WTYPE_ALL; //< The Weapon has all capabilities @see W_Capability. |
---|
[6438] | 102 | |
---|
| 103 | this->energyWidget = NULL; |
---|
| 104 | this->energyLoadedWidget = NULL; |
---|
[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 | { |
---|
| 113 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
| 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 | |
---|
[6445] | 291 | GLGuiWidget* Weapon::getLoadedEnergyWidget() |
---|
[6438] | 292 | { |
---|
| 293 | if (this->energyLoadedWidget == NULL) |
---|
| 294 | { |
---|
| 295 | this->energyLoadedWidget = new GLGuiBar; |
---|
| 296 | //this->energyLoadedWidget->setParent2D(this->bar); |
---|
| 297 | this->energyLoadedWidget->setRelCoor2D(20,0); |
---|
| 298 | this->energyLoadedWidget->setSize2D(10,50); |
---|
| 299 | this->energyLoadedWidget->setMaximum(this->getLoadedEnergyMax()); |
---|
| 300 | } |
---|
| 301 | return this->energyLoadedWidget; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | void Weapon::updateWidgets() |
---|
| 305 | { |
---|
| 306 | if (this->energyWidget != NULL) |
---|
| 307 | { |
---|
| 308 | this->energyWidget->setMaximum(this->energyMax); |
---|
| 309 | this->energyWidget->setValue(this->energy); |
---|
| 310 | } |
---|
| 311 | if (this->energyLoadedWidget != NULL) |
---|
| 312 | { |
---|
| 313 | this->energyLoadedWidget->setMaximum(this->energyLoadedMax); |
---|
| 314 | this->energyLoadedWidget->setValue(this->energyLoaded); |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | |
---|
[4892] | 318 | ///////////////// |
---|
| 319 | // EXECUTION // |
---|
| 320 | // GAME ACTION // |
---|
| 321 | ///////////////// |
---|
[4597] | 322 | /** |
---|
[4885] | 323 | * request an action that should be executed, |
---|
| 324 | * @param action the next action to take |
---|
| 325 | * |
---|
| 326 | * This function must be called instead of the actions (like fire/reload...) |
---|
| 327 | * to make all the checks needed to have a usefull WeaponSystem. |
---|
| 328 | */ |
---|
| 329 | void Weapon::requestAction(WeaponAction action) |
---|
| 330 | { |
---|
[4906] | 331 | if (likely(this->isActive())) |
---|
[4885] | 332 | { |
---|
[4906] | 333 | if (this->requestedAction != WA_NONE) |
---|
| 334 | return; |
---|
[5041] | 335 | PRINTF(5)("next action will be %s in %f seconds\n", actionToChar(action), this->stateDuration); |
---|
[4885] | 336 | this->requestedAction = action; |
---|
| 337 | } |
---|
[4906] | 338 | //else |
---|
| 339 | else if (unlikely(action == WA_ACTIVATE)) |
---|
| 340 | { |
---|
| 341 | this->currentState = WS_ACTIVATING; |
---|
[4926] | 342 | this->requestedAction = WA_ACTIVATE; |
---|
[4906] | 343 | } |
---|
[4885] | 344 | } |
---|
[3577] | 345 | |
---|
[4890] | 346 | /** |
---|
| 347 | * adds energy to the Weapon |
---|
| 348 | * @param energyToAdd The amount of energy |
---|
| 349 | * @returns the amount of energy we did not pick up, because the weapon is already full |
---|
| 350 | */ |
---|
| 351 | float Weapon::increaseEnergy(float energyToAdd) |
---|
| 352 | { |
---|
| 353 | float maxAddEnergy = this->energyMax - this->energy; |
---|
| 354 | |
---|
| 355 | if (maxAddEnergy >= energyToAdd) |
---|
| 356 | { |
---|
| 357 | this->energy += energyToAdd; |
---|
| 358 | return 0.0; |
---|
| 359 | } |
---|
| 360 | else |
---|
| 361 | { |
---|
| 362 | this->energy += maxAddEnergy; |
---|
| 363 | return energyToAdd - maxAddEnergy; |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
[5498] | 367 | //////////////////////////////////////////////////////////// |
---|
| 368 | // WEAPON INTERNALS // |
---|
| 369 | // These are functions, that no other Weapon should over- // |
---|
| 370 | // write. No class has direct Access to them, as it is // |
---|
| 371 | // quite a complicated process, handling a Weapon from // |
---|
| 372 | // the outside // |
---|
| 373 | //////////////////////////////////////////////////////////// |
---|
[4891] | 374 | /** |
---|
| 375 | * executes an action, and with it starts a new State. |
---|
| 376 | * @return true, if it worked, false otherwise |
---|
| 377 | * |
---|
| 378 | * This function checks, wheter the possibility of executing an action is valid, |
---|
| 379 | * and does all the necessary stuff, to execute them. If an action does not succeed, |
---|
| 380 | * it tries to go around it. (ex. shoot->noAmo->reload()->wait until shoot comes again) |
---|
| 381 | */ |
---|
[4885] | 382 | bool Weapon::execute() |
---|
[3583] | 383 | { |
---|
[4906] | 384 | #if DEBUG > 4 |
---|
[4885] | 385 | PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction)); |
---|
| 386 | this->debug(); |
---|
[4906] | 387 | #endif |
---|
[4885] | 388 | |
---|
[4926] | 389 | WeaponAction action = this->requestedAction; |
---|
| 390 | this->requestedAction = WA_NONE; |
---|
| 391 | |
---|
| 392 | switch (action) |
---|
[4885] | 393 | { |
---|
| 394 | case WA_SHOOT: |
---|
[4894] | 395 | return this->fireW(); |
---|
[4885] | 396 | break; |
---|
| 397 | case WA_CHARGE: |
---|
[4894] | 398 | return this->chargeW(); |
---|
[4885] | 399 | break; |
---|
| 400 | case WA_RELOAD: |
---|
[4894] | 401 | return this->reloadW(); |
---|
[4885] | 402 | break; |
---|
| 403 | case WA_DEACTIVATE: |
---|
[4894] | 404 | return this->deactivateW(); |
---|
[4885] | 405 | break; |
---|
| 406 | case WA_ACTIVATE: |
---|
[4894] | 407 | return this->activateW(); |
---|
[4885] | 408 | break; |
---|
| 409 | } |
---|
[3583] | 410 | } |
---|
[3577] | 411 | |
---|
[4597] | 412 | /** |
---|
[4894] | 413 | * checks and activates the Weapon. |
---|
| 414 | * @return true on success. |
---|
[4892] | 415 | */ |
---|
| 416 | bool Weapon::activateW() |
---|
[3583] | 417 | { |
---|
[6438] | 418 | // if (this->currentState == WS_INACTIVE) |
---|
[4892] | 419 | { |
---|
[6433] | 420 | // play Sound |
---|
[4893] | 421 | if (likely(this->soundBuffers[WA_ACTIVATE] != NULL)) |
---|
[4892] | 422 | this->soundSource->play(this->soundBuffers[WA_ACTIVATE]); |
---|
[6444] | 423 | this->updateWidgets(); |
---|
[6438] | 424 | // activate |
---|
[4895] | 425 | PRINTF(4)("Activating the Weapon %s\n", this->getName()); |
---|
[4892] | 426 | this->activate(); |
---|
[4895] | 427 | // setting up for next action |
---|
[4926] | 428 | this->enterState(WS_ACTIVATING); |
---|
[4892] | 429 | } |
---|
[3583] | 430 | } |
---|
[3577] | 431 | |
---|
[4597] | 432 | /** |
---|
[4894] | 433 | * checks and deactivates the Weapon |
---|
| 434 | * @return true on success. |
---|
[4892] | 435 | */ |
---|
| 436 | bool Weapon::deactivateW() |
---|
[3583] | 437 | { |
---|
[6438] | 438 | // if (this->currentState != WS_INACTIVE) |
---|
[4892] | 439 | { |
---|
| 440 | PRINTF(4)("Deactivating the Weapon %s\n", this->getName()); |
---|
[6438] | 441 | // play Sound |
---|
[4892] | 442 | if (this->soundBuffers[WA_DEACTIVATE] != NULL) |
---|
| 443 | this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]); |
---|
[4926] | 444 | // deactivate |
---|
[4892] | 445 | this->deactivate(); |
---|
[4926] | 446 | this->enterState(WS_DEACTIVATING); |
---|
[4892] | 447 | } |
---|
[3583] | 448 | } |
---|
[3577] | 449 | |
---|
[4892] | 450 | /** |
---|
[4894] | 451 | * checks and charges the Weapon |
---|
| 452 | * @return true on success. |
---|
[4892] | 453 | */ |
---|
| 454 | bool Weapon::chargeW() |
---|
[4885] | 455 | { |
---|
[4892] | 456 | if ( this->currentState != WS_INACTIVE && this->energyLoaded >= this->minCharge) |
---|
| 457 | { |
---|
[6438] | 458 | // playing Sound |
---|
[4892] | 459 | if (this->soundBuffers[WA_CHARGE] != NULL) |
---|
| 460 | this->soundSource->play(this->soundBuffers[WA_CHARGE]); |
---|
[4893] | 461 | |
---|
[6438] | 462 | // charge |
---|
[4892] | 463 | this->charge(); |
---|
[6438] | 464 | // setting up for the next state |
---|
[4926] | 465 | this->enterState(WS_CHARGING); |
---|
[4892] | 466 | } |
---|
| 467 | else // deactivate the Weapon if we do not have enough energy |
---|
| 468 | { |
---|
| 469 | this->requestAction(WA_RELOAD); |
---|
| 470 | } |
---|
[4885] | 471 | } |
---|
[3573] | 472 | |
---|
[4892] | 473 | /** |
---|
[4894] | 474 | * checks and fires the Weapon |
---|
| 475 | * @return true on success. |
---|
[4892] | 476 | */ |
---|
| 477 | bool Weapon::fireW() |
---|
[3575] | 478 | { |
---|
[6438] | 479 | //if (likely(this->currentState != WS_INACTIVE)) |
---|
[4892] | 480 | if (this->minCharge <= this->energyLoaded) |
---|
| 481 | { |
---|
[6438] | 482 | // playing Sound |
---|
[4892] | 483 | if (this->soundBuffers[WA_SHOOT] != NULL) |
---|
| 484 | this->soundSource->play(this->soundBuffers[WA_SHOOT]); |
---|
[6444] | 485 | this->updateWidgets(); |
---|
[6438] | 486 | // fire |
---|
[6306] | 487 | this->energyLoaded -= this->minCharge; |
---|
[4892] | 488 | this->fire(); |
---|
[6438] | 489 | // setting up for the next state |
---|
[4926] | 490 | this->enterState(WS_SHOOTING); |
---|
[4892] | 491 | } |
---|
| 492 | else // reload if we still have the charge |
---|
| 493 | { |
---|
| 494 | this->requestAction(WA_RELOAD); |
---|
[4930] | 495 | this->execute(); |
---|
[4892] | 496 | } |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | /** |
---|
[4894] | 500 | * checks and Reloads the Weapon |
---|
| 501 | * @return true on success. |
---|
[4892] | 502 | */ |
---|
| 503 | bool Weapon::reloadW() |
---|
| 504 | { |
---|
[4885] | 505 | PRINTF(4)("Reloading Weapon %s\n", this->getName()); |
---|
[4892] | 506 | if (unlikely(this->energy + this->energyLoaded < this->minCharge)) |
---|
[4885] | 507 | { |
---|
| 508 | this->requestAction(WA_DEACTIVATE); |
---|
[4930] | 509 | this->execute(); |
---|
[4892] | 510 | return false; |
---|
[4885] | 511 | } |
---|
[3573] | 512 | |
---|
[4885] | 513 | float chargeSize = this->energyLoadedMax - this->energyLoaded; //!< The energy to be charged |
---|
[3573] | 514 | |
---|
[4892] | 515 | if (this->soundBuffers[WA_RELOAD] != NULL) |
---|
| 516 | this->soundSource->play(this->soundBuffers[WA_RELOAD]); |
---|
| 517 | |
---|
[4885] | 518 | if (chargeSize > this->energy) |
---|
| 519 | { |
---|
| 520 | this->energyLoaded += this->energy; |
---|
| 521 | this->energy = 0.0; |
---|
[5041] | 522 | PRINT(5)("Energy depleted\n"); |
---|
[4885] | 523 | } |
---|
| 524 | else |
---|
| 525 | { |
---|
[5041] | 526 | PRINTF(5)("Loaded %f energy into the Guns Buffer\n", chargeSize); |
---|
[4885] | 527 | this->energyLoaded += chargeSize; |
---|
| 528 | this->energy -= chargeSize; |
---|
| 529 | } |
---|
[6444] | 530 | |
---|
| 531 | this->updateWidgets(); |
---|
[4892] | 532 | this->reload(); |
---|
[4926] | 533 | this->enterState(WS_RELOADING); |
---|
| 534 | } |
---|
[3575] | 535 | |
---|
[4926] | 536 | /** |
---|
| 537 | * enters the requested State, plays back animations updates the timing. |
---|
| 538 | * @param state the state to enter. |
---|
| 539 | */ |
---|
| 540 | inline void Weapon::enterState(WeaponState state) |
---|
| 541 | { |
---|
[5041] | 542 | PRINTF(4)("ENTERING STATE %s\n", stateToChar(state)); |
---|
[4926] | 543 | // playing animation if availiable |
---|
| 544 | if (likely(this->animation[state] != NULL)) |
---|
| 545 | this->animation[state]->replay(); |
---|
| 546 | |
---|
| 547 | this->stateDuration = this->times[state] + this->stateDuration; |
---|
| 548 | this->currentState = state; |
---|
[3575] | 549 | } |
---|
| 550 | |
---|
[4927] | 551 | /////////////////// |
---|
| 552 | // WORLD-ENTITY // |
---|
| 553 | // FUNCTIONALITY // |
---|
| 554 | /////////////////// |
---|
[3575] | 555 | /** |
---|
[4885] | 556 | * tick signal for time dependent/driven stuff |
---|
[3575] | 557 | */ |
---|
[6439] | 558 | void Weapon::tickW(float dt) |
---|
[4885] | 559 | { |
---|
[4934] | 560 | //printf("%s ", stateToChar(this->currentState)); |
---|
[4910] | 561 | |
---|
[4885] | 562 | // setting up the timing properties |
---|
| 563 | this->stateDuration -= dt; |
---|
[3575] | 564 | |
---|
[4949] | 565 | if (this->stateDuration <= 0.0) |
---|
[4885] | 566 | { |
---|
[4949] | 567 | if (unlikely (this->currentState == WS_DEACTIVATING)) |
---|
[4885] | 568 | { |
---|
[4949] | 569 | this->currentState = WS_INACTIVE; |
---|
| 570 | return; |
---|
| 571 | } |
---|
| 572 | else |
---|
| 573 | this->currentState = WS_IDLE; |
---|
[4906] | 574 | |
---|
[4949] | 575 | if (this->requestedAction != WA_NONE) |
---|
| 576 | { |
---|
| 577 | this->stateDuration = -dt; |
---|
| 578 | this->execute(); |
---|
[4885] | 579 | } |
---|
| 580 | } |
---|
[6439] | 581 | tick(dt); |
---|
[4885] | 582 | } |
---|
| 583 | |
---|
[3575] | 584 | |
---|
| 585 | |
---|
| 586 | |
---|
[4885] | 587 | ////////////////////// |
---|
| 588 | // HELPER FUNCTIONS // |
---|
| 589 | ////////////////////// |
---|
[3576] | 590 | /** |
---|
[4891] | 591 | * checks wether all the Weapons functions are valid, and if it is possible to go to action with it. |
---|
[5498] | 592 | * @todo IMPLEMENT the Weapons Check |
---|
[4891] | 593 | */ |
---|
| 594 | bool Weapon::check() const |
---|
| 595 | { |
---|
| 596 | bool retVal = true; |
---|
| 597 | |
---|
[6438] | 598 | // if (this->projectile == NULL) |
---|
[4891] | 599 | { |
---|
[5041] | 600 | PRINTF(1)("There was no projectile assigned to the Weapon.\n"); |
---|
[4891] | 601 | retVal = false; |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | |
---|
| 605 | |
---|
| 606 | |
---|
| 607 | return retVal; |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | /** |
---|
[4885] | 611 | * some nice debugging information about this Weapon |
---|
| 612 | */ |
---|
| 613 | void Weapon::debug() const |
---|
| 614 | { |
---|
[6433] | 615 | PRINT(0)("Weapon-Debug %s, state: %s (duration: %fs), nextAction: %s\n", this->getName(), Weapon::stateToChar(this->currentState), this->stateDuration, Weapon::actionToChar(requestedAction)); |
---|
| 616 | PRINT(0)("Energy: max: %f; current: %f; loadedMax: %f; loadedCurrent: %f; chargeMin: %f, chargeMax %f\n", |
---|
[6438] | 617 | this->energyMax, this->energy, this->energyLoadedMax, this->energyLoaded, this->minCharge, this->maxCharge); |
---|
[4967] | 618 | |
---|
| 619 | |
---|
[4885] | 620 | } |
---|
[3575] | 621 | |
---|
[5498] | 622 | //////////////////////////////////////////////////////// |
---|
| 623 | // static Definitions (transormators for readability) // |
---|
| 624 | //////////////////////////////////////////////////////// |
---|
[4885] | 625 | /** |
---|
| 626 | * Converts a String into an Action. |
---|
| 627 | * @param action the String input holding the Action. |
---|
| 628 | * @return The Action if known, WA_NONE otherwise. |
---|
| 629 | */ |
---|
| 630 | WeaponAction Weapon::charToAction(const char* action) |
---|
| 631 | { |
---|
| 632 | if (!strcmp(action, "none")) |
---|
| 633 | return WA_NONE; |
---|
| 634 | else if (!strcmp(action, "shoot")) |
---|
| 635 | return WA_SHOOT; |
---|
| 636 | else if (!strcmp(action, "charge")) |
---|
| 637 | return WA_CHARGE; |
---|
| 638 | else if (!strcmp(action, "reload")) |
---|
| 639 | return WA_RELOAD; |
---|
| 640 | else if (!strcmp(action, "acitvate")) |
---|
| 641 | return WA_ACTIVATE; |
---|
| 642 | else if (!strcmp(action, "deactivate")) |
---|
| 643 | return WA_DEACTIVATE; |
---|
| 644 | else if (!strcmp(action, "special1")) |
---|
| 645 | return WA_SPECIAL1; |
---|
| 646 | else |
---|
[6438] | 647 | { |
---|
| 648 | PRINTF(2)("action %s could not be identified.\n", action); |
---|
| 649 | return WA_NONE; |
---|
| 650 | } |
---|
[4885] | 651 | } |
---|
[3575] | 652 | |
---|
| 653 | /** |
---|
[4885] | 654 | * converts an action into a String |
---|
| 655 | * @param action the action to convert |
---|
| 656 | * @return a String matching the name of the action |
---|
| 657 | */ |
---|
| 658 | const char* Weapon::actionToChar(WeaponAction action) |
---|
| 659 | { |
---|
| 660 | switch (action) |
---|
| 661 | { |
---|
| 662 | case WA_SHOOT: |
---|
| 663 | return "shoot"; |
---|
| 664 | break; |
---|
| 665 | case WA_CHARGE: |
---|
| 666 | return "charge"; |
---|
| 667 | break; |
---|
| 668 | case WA_RELOAD: |
---|
| 669 | return "reload"; |
---|
| 670 | break; |
---|
| 671 | case WA_ACTIVATE: |
---|
| 672 | return "activate"; |
---|
| 673 | break; |
---|
| 674 | case WA_DEACTIVATE: |
---|
| 675 | return "deactivate"; |
---|
| 676 | break; |
---|
| 677 | case WA_SPECIAL1: |
---|
| 678 | return "special1"; |
---|
| 679 | break; |
---|
| 680 | default: |
---|
| 681 | return "none"; |
---|
| 682 | break; |
---|
| 683 | } |
---|
| 684 | } |
---|
[3577] | 685 | |
---|
| 686 | /** |
---|
[4885] | 687 | * Converts a String into a State. |
---|
| 688 | * @param state the String input holding the State. |
---|
| 689 | * @return The State if known, WS_NONE otherwise. |
---|
| 690 | */ |
---|
| 691 | WeaponState Weapon::charToState(const char* state) |
---|
| 692 | { |
---|
| 693 | if (!strcmp(state, "none")) |
---|
| 694 | return WS_NONE; |
---|
| 695 | else if (!strcmp(state, "shooting")) |
---|
| 696 | return WS_SHOOTING; |
---|
| 697 | else if (!strcmp(state, "charging")) |
---|
| 698 | return WS_CHARGING; |
---|
| 699 | else if (!strcmp(state, "reloading")) |
---|
| 700 | return WS_RELOADING; |
---|
| 701 | else if (!strcmp(state, "activating")) |
---|
| 702 | return WS_ACTIVATING; |
---|
| 703 | else if (!strcmp(state, "deactivating")) |
---|
| 704 | return WS_DEACTIVATING; |
---|
| 705 | else if (!strcmp(state, "inactive")) |
---|
| 706 | return WS_INACTIVE; |
---|
| 707 | else if (!strcmp(state, "idle")) |
---|
| 708 | return WS_IDLE; |
---|
| 709 | else |
---|
[6438] | 710 | { |
---|
| 711 | PRINTF(2)("state %s could not be identified.\n", state); |
---|
| 712 | return WS_NONE; |
---|
| 713 | } |
---|
[4885] | 714 | } |
---|
[3583] | 715 | |
---|
| 716 | /** |
---|
[4885] | 717 | * converts a State into a String |
---|
| 718 | * @param state the state to convert |
---|
| 719 | * @return a String matching the name of the state |
---|
| 720 | */ |
---|
| 721 | const char* Weapon::stateToChar(WeaponState state) |
---|
| 722 | { |
---|
| 723 | switch (state) |
---|
| 724 | { |
---|
| 725 | case WS_SHOOTING: |
---|
| 726 | return "shooting"; |
---|
| 727 | break; |
---|
| 728 | case WS_CHARGING: |
---|
| 729 | return "charging"; |
---|
| 730 | break; |
---|
| 731 | case WS_RELOADING: |
---|
| 732 | return "reloading"; |
---|
| 733 | break; |
---|
| 734 | case WS_ACTIVATING: |
---|
| 735 | return "activating"; |
---|
| 736 | break; |
---|
| 737 | case WS_DEACTIVATING: |
---|
| 738 | return "deactivating"; |
---|
| 739 | break; |
---|
| 740 | case WS_IDLE: |
---|
| 741 | return "idle"; |
---|
| 742 | break; |
---|
[4906] | 743 | case WS_INACTIVE: |
---|
| 744 | return "inactive"; |
---|
| 745 | break; |
---|
[4885] | 746 | default: |
---|
| 747 | return "none"; |
---|
| 748 | break; |
---|
| 749 | } |
---|
| 750 | } |
---|