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