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