| 1 | /*! | 
|---|
| 2 |     \file weapon.h | 
|---|
| 3 |   *  a weapon that a  can use | 
|---|
| 4 |  | 
|---|
| 5 |  | 
|---|
| 6 |     A weapon is characterized by: | 
|---|
| 7 |      o firing-rate: the initial firing rate of a weapon (1/s = Herz) | 
|---|
| 8 |      o slowdown-factor: this is a factor d: exp(-d*x), d is element of all positive R. it determines how fast the firing-rate will slow down. if no slowdown: d=0, the bigger d is, the faster the weapon will slow down! | 
|---|
| 9 |      o energy-consumption: this determines the energy that has to be used to produce this projectile = costs per projectile | 
|---|
| 10 |  | 
|---|
| 11 |     Furthermore there are some other attributes, that will help to represent a firing | 
|---|
| 12 |     weapon in this world: | 
|---|
| 13 |      o sound file/ressource: this is a pointer to the sound-file/ressource. however it may be represented | 
|---|
| 14 |      o animations | 
|---|
| 15 | */ | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | #ifndef _WEAPON_H | 
|---|
| 19 | #define _WEAPON_H | 
|---|
| 20 |  | 
|---|
| 21 | #include "world_entity.h" | 
|---|
| 22 |  | 
|---|
| 23 | // FORWARD DECLARATION | 
|---|
| 24 | class Projectile; | 
|---|
| 25 | class Weapon; | 
|---|
| 26 | class Animation3D; | 
|---|
| 27 | class TiXmlElement; | 
|---|
| 28 |  | 
|---|
| 29 | //! An enumerator defining Actions a Weapon can take | 
|---|
| 30 | typedef enum { | 
|---|
| 31 |   WA_NONE          =    0,    //!< No Action taken | 
|---|
| 32 |   WA_SHOOT         =    1,    //!< emitting Shot | 
|---|
| 33 |   WA_CHARGE        =    2,    //!< charge action (one click before the shot) | 
|---|
| 34 |   WA_RELOAD        =    3,    //!< reload right after shoot is finished | 
|---|
| 35 |   WA_ACTIVATE      =    4,    //!< activate the GUN | 
|---|
| 36 |   WA_DEACTIVATE    =    5,    //!< deactivate the GUN | 
|---|
| 37 |   WA_SPECIAL1      =    6,    //!< Special Action taken | 
|---|
| 38 |  | 
|---|
| 39 |   WA_ACTION_COUNT  =    7     //!< This must match the count of enumerations-members. | 
|---|
| 40 | } WeaponAction; | 
|---|
| 41 |  | 
|---|
| 42 | //! An enumerator defining the States of a Weapon | 
|---|
| 43 | typedef enum { | 
|---|
| 44 |   WS_NONE          =    0,    //!< No State at all (if set, there is something wrong, or the weapon is not yet availiable) | 
|---|
| 45 |   WS_SHOOTING      =    1,    //!< The State of the Shooting | 
|---|
| 46 |   WS_CHARGING      =    2,    //!< The state of charging th weapon | 
|---|
| 47 |   WS_RELOADING     =    3,    //!< The State of the Reloading | 
|---|
| 48 |   WS_ACTIVATING    =    4,    //!< The State in which the weapon gets activated | 
|---|
| 49 |   WS_DEACTIVATING  =    5,    //!< The State in which the weapon gets deactivated | 
|---|
| 50 |   WS_INACTIVE      =    6,    //!< The State where the weapon is inactive (unable to shoot) | 
|---|
| 51 |   WS_IDLE          =    7,    //!< The State where the weapon is idle | 
|---|
| 52 |  | 
|---|
| 53 |   WS_STATE_COUNT  =     8     //!< This must match the count of enumerations-members. | 
|---|
| 54 | } WeaponState; | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 | //! a weapon can be left or right sided | 
|---|
| 58 | /** | 
|---|
| 59 |  * @todo this will be reset with mirror X/Y/Z | 
|---|
| 60 |  */ | 
|---|
| 61 | #define    W_LEFT        0 | 
|---|
| 62 | #define    W_RIGHT       1 | 
|---|
| 63 |  | 
|---|
| 64 | //! An abstract class, that describes weapons | 
|---|
| 65 | /** | 
|---|
| 66 |  * This is used as a container for all the different kinds of weapons that may exist | 
|---|
| 67 |  */ | 
|---|
| 68 | class Weapon : public WorldEntity | 
|---|
| 69 | { | 
|---|
| 70 |   public: | 
|---|
| 71 |     // INITIALISATION // | 
|---|
| 72 |     Weapon (PNode* parent, const Vector& coordinate, const Quaternion& direction); | 
|---|
| 73 |     Weapon(const TiXmlElement* root); | 
|---|
| 74 |     virtual ~Weapon (); | 
|---|
| 75 |  | 
|---|
| 76 |     void init(); | 
|---|
| 77 |     void loadParams(const TiXmlElement* root); | 
|---|
| 78 |     //////////////////// | 
|---|
| 79 |  | 
|---|
| 80 |     void requestAction(WeaponAction action); | 
|---|
| 81 |     float increaseEnergy(float energyToAdd); | 
|---|
| 82 |  | 
|---|
| 83 |     /** @returns true if the Weapon is Active */ | 
|---|
| 84 |     inline bool isActive() const { return this->active; }; | 
|---|
| 85 |  | 
|---|
| 86 |     // FUNCTIONS TO SET THE WEAPONS PROPERTIES. | 
|---|
| 87 |     /** @param projectile a projectile for this weapon */ | 
|---|
| 88 |     void setProjectile(Projectile* projectile) { this->projectile = projectile; }; | 
|---|
| 89 |     /** @returns The projectile if availiable */ | 
|---|
| 90 |     Projectile* getProjectile() { return this->projectile; }; | 
|---|
| 91 |  | 
|---|
| 92 |     void setEmissionPoint(const Vector& point); | 
|---|
| 93 |     /** @see void setEmissionPoint(const Vector& point); */ | 
|---|
| 94 |     inline void setEmissionPoint(float x, float y, float z) { this->setEmissionPoint(Vector(x,y,z)); }; | 
|---|
| 95 |  | 
|---|
| 96 |     /** @param state the State to time @param duration the duration of the State */ | 
|---|
| 97 |     inline void setStateDuration(const char* state, float duration) { setStateDuration(charToState(state), duration); }; | 
|---|
| 98 |     /** @param state the State to time @param duration the duration of the State */ | 
|---|
| 99 |     inline void setStateDuration(WeaponState state, float duration) { /*(state < WS_STATE_COUNT)?*/this->times[state] = duration; }; | 
|---|
| 100 |     /** @param state The state to query @returns the Time the queried State takes to complete */ | 
|---|
| 101 |     inline float getStateDuration(WeaponState state) const { return (state < WS_STATE_COUNT)?this->times[state]:0.0; }; | 
|---|
| 102 |     /** @returns true if the time of the currentState is elapsed, false otherwise */ | 
|---|
| 103 |     inline bool stateTimeElapsed() const { return (this->stateDuration > this->times[currentState])?true:false; }; | 
|---|
| 104 |     /** @returns the current State of the Weapon */ | 
|---|
| 105 |     inline WeaponState getCurrentState() const { return this->currentState; }; | 
|---|
| 106 |  | 
|---|
| 107 |     /** @param energyMax the maximum energy the Weapon can have @param energyLoadedMax the maximum energy in the weapon buffers */ | 
|---|
| 108 |     inline void setMaximumEnergy(float energyMax, float energyLoadedMax = 0.0) { this->energyMax = energyMax; this->energyLoadedMax = energyLoadedMax; }; | 
|---|
| 109 |  | 
|---|
| 110 |     void setActionSound(WeaponAction action, const char* soundFile); | 
|---|
| 111 |     /** @see void setActionSound(WeaponAction action, const char* soundFile); */ | 
|---|
| 112 |     void setActionSound(const char* action, const char* soundFile) { this->setActionSound(charToAction(action), soundFile); }; | 
|---|
| 113 |  | 
|---|
| 114 |     Animation3D* getAnimation(WeaponState state, PNode* node = NULL); | 
|---|
| 115 |  | 
|---|
| 116 |     // FLOW | 
|---|
| 117 |     virtual void tick(float dt); | 
|---|
| 118 |     virtual void draw(); | 
|---|
| 119 |  | 
|---|
| 120 |     bool check() const; | 
|---|
| 121 |     void debug() const; | 
|---|
| 122 |  | 
|---|
| 123 |   protected: | 
|---|
| 124 |     //! ACTION: these functions are handled by the Weapon itself, and must be called by requestAction(WeaponAction); | 
|---|
| 125 |     virtual void activate() {}; | 
|---|
| 126 |     virtual void deactivate() {}; | 
|---|
| 127 |     virtual void charge() {}; | 
|---|
| 128 |     virtual void fire() {}; | 
|---|
| 129 |     virtual void reload() {}; | 
|---|
| 130 |     virtual void destroy() {}; | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 |     // utility: | 
|---|
| 134 |     static WeaponAction  charToAction(const char* action); | 
|---|
| 135 |     static const char*   actionToChar(WeaponAction action); | 
|---|
| 136 |     static WeaponState   charToState(const char* state); | 
|---|
| 137 |     static const char*   stateToChar(WeaponState state); | 
|---|
| 138 |  | 
|---|
| 139 |   private: | 
|---|
| 140 |     /** executive functions, that handle timing with actions. | 
|---|
| 141 |      * This is for the action-functions in derived functions to be easy | 
|---|
| 142 |      * The main function is execute, that calls all the other functions | 
|---|
| 143 |      * for being fast, the Functions are private and as such will be inlined | 
|---|
| 144 |      * into the execute function. (this is work of the compiler) | 
|---|
| 145 |      */ | 
|---|
| 146 |     bool execute(); | 
|---|
| 147 |     bool activateW(); | 
|---|
| 148 |     bool deactivateW(); | 
|---|
| 149 |     bool chargeW(); | 
|---|
| 150 |     bool fireW(); | 
|---|
| 151 |     bool reloadW(); | 
|---|
| 152 |  | 
|---|
| 153 |  | 
|---|
| 154 |   protected: | 
|---|
| 155 |     SoundSource*         soundSource;                      //!< A SoundSource to play sound from (this is connected to the PNode of the Weapon) | 
|---|
| 156 |     // it is all about energy | 
|---|
| 157 |     float                energy;                           //!< The energy stored in the weapons secondary buffers (reserve) | 
|---|
| 158 |     float                energyLoaded;                     //!< The energy stored in the weapons primary buffers (firewithout reload) | 
|---|
| 159 |     float                energyMax;                        //!< The maximal energy that can be stored in the secondary buffers (reserveMax) | 
|---|
| 160 |     float                energyLoadedMax;                  //!< The maximal energy that can be stored in the primary buffers | 
|---|
| 161 |     float                minCharge;                        //!< The minimal energy to be loaded onto one projectile if chargeable otherwise the power consumed by one projectile | 
|---|
| 162 |     float                maxCharge;                        //!< The maximal energy to be loaded onto one projectile (this is only availible if chargeable is enabled) | 
|---|
| 163 |  | 
|---|
| 164 |     //////////// | 
|---|
| 165 |     // PHASES // | 
|---|
| 166 |     //////////// | 
|---|
| 167 |   private: | 
|---|
| 168 |     WeaponState          currentState;                     //!< The State the weapon is in. | 
|---|
| 169 |     WeaponAction         requestedAction;                  //!< An action to try to Engage after the currentState ends. | 
|---|
| 170 |     float                stateDuration;                    //!< how long the state has taken until now. | 
|---|
| 171 |     float                times[WS_STATE_COUNT];            //!< Times to stay in the different States @see WeaponState. | 
|---|
| 172 |     Animation3D*         animation[WS_STATE_COUNT];        //!< Animations for all the States (you can say yourself on what part of the gun this animation acts). | 
|---|
| 173 |     SoundBuffer*         soundBuffers[WA_ACTION_COUNT];    //!< SoundBuffers for all actions @see WeaponAction. | 
|---|
| 174 |  | 
|---|
| 175 |     PNode                emissionPoint;                   //!< The point, where the projectiles are emitted. (this is coppled with the Weapon by default) | 
|---|
| 176 |  | 
|---|
| 177 |     bool                 active;                          //!< states wheter the weapon is enabled or not | 
|---|
| 178 |     bool                 hideInactive;                    //!< Hides the Weapon if it is inactive | 
|---|
| 179 |     bool                 chargeable;                      //!< if the Weapon is charcheable | 
|---|
| 180 |  | 
|---|
| 181 |     Projectile*          projectile;                      //!< the projectile used for this weapon | 
|---|
| 182 |   }; | 
|---|
| 183 |  | 
|---|
| 184 | #endif /* _WEAPON_H */ | 
|---|