| 1 | /*! | 
|---|
| 2 |  * @file weapon.h | 
|---|
| 3 |  * | 
|---|
| 4 |  * Weapon is the mayor baseclass for all weapons. it is quite extensive, and expensive in use, | 
|---|
| 5 |  * because each weapon will know virutal functions for the WorldEntity's part, and also virtuals | 
|---|
| 6 |  * for Fireing/Reloading/..., | 
|---|
| 7 |  * quickly said: Weapon is a wrapper for weapons, that makes it easy to very quickly implement | 
|---|
| 8 |  * new Weapons, and with them make this game better, than any game before it, because still | 
|---|
| 9 |  * Weapons (GUNS) are the most important thing in life :?... no to be serious | 
|---|
| 10 |  * @see Weapon | 
|---|
| 11 |  */ | 
|---|
| 12 |  | 
|---|
| 13 |  | 
|---|
| 14 | #ifndef _WEAPON_H | 
|---|
| 15 | #define _WEAPON_H | 
|---|
| 16 |  | 
|---|
| 17 | #include "world_entity.h" | 
|---|
| 18 |  | 
|---|
| 19 | // FORWARD DECLARATION | 
|---|
| 20 | class Projectile; | 
|---|
| 21 | class WeaponManager; | 
|---|
| 22 | class Animation3D; | 
|---|
| 23 | class TiXmlElement; | 
|---|
| 24 | class FastFactory; | 
|---|
| 25 | template<class T> class tFastFactory; | 
|---|
| 26 |  | 
|---|
| 27 | //! An enumerator defining Actions a Weapon can take | 
|---|
| 28 | typedef enum { | 
|---|
| 29 |   WA_NONE          =    0,    //!< No Action taken | 
|---|
| 30 |   WA_SHOOT         =    1,    //!< emitting Shot | 
|---|
| 31 |   WA_CHARGE        =    2,    //!< charge action (one click before the shot) | 
|---|
| 32 |   WA_RELOAD        =    3,    //!< reload right after shoot is finished | 
|---|
| 33 |   WA_ACTIVATE      =    4,    //!< activate the GUN | 
|---|
| 34 |   WA_DEACTIVATE    =    5,    //!< deactivate the GUN | 
|---|
| 35 |   WA_SPECIAL1      =    6,    //!< Special Action taken | 
|---|
| 36 |  | 
|---|
| 37 |   WA_ACTION_COUNT  =    7     //!< This must match the count of enumerations-members. | 
|---|
| 38 | } WeaponAction; | 
|---|
| 39 |  | 
|---|
| 40 | //! An enumerator defining the States of a Weapon | 
|---|
| 41 | typedef enum { | 
|---|
| 42 |   WS_NONE          =    0,    //!< No State at all (if set, there is something wrong, or the weapon is not yet availiable) | 
|---|
| 43 |   WS_SHOOTING      =    1,    //!< The State of the Shooting | 
|---|
| 44 |   WS_CHARGING      =    2,    //!< The state of charging th weapon | 
|---|
| 45 |   WS_RELOADING     =    3,    //!< The State of the Reloading | 
|---|
| 46 |   WS_ACTIVATING    =    4,    //!< The State in which the weapon gets activated | 
|---|
| 47 |   WS_DEACTIVATING  =    5,    //!< The State in which the weapon gets deactivated | 
|---|
| 48 |   WS_INACTIVE      =    6,    //!< The State where the weapon is inactive (unable to shoot) | 
|---|
| 49 |   WS_IDLE          =    7,    //!< The State where the weapon is idle | 
|---|
| 50 |  | 
|---|
| 51 |   WS_STATE_COUNT  =     8     //!< This must match the count of enumerations-members. | 
|---|
| 52 | } WeaponState; | 
|---|
| 53 |  | 
|---|
| 54 | //! an enumerator defining capabilities of a WeaponSlot | 
|---|
| 55 | typedef enum | 
|---|
| 56 | { | 
|---|
| 57 |   WTYPE_DIRECTIONAL   = 0x00000001,           //!< Weapon is directional/Slot is able to carry directional weapons | 
|---|
| 58 |   WTYPE_TURRET        = 0x00000002,           //!< Weapon is a turret/slot is able to carry turrets | 
|---|
| 59 |   WTYPE_ALLKINDS      = 0x0000000f,           //!< Weapon is all types/Slot is able to carry all kinds of weapons | 
|---|
| 60 |  | 
|---|
| 61 |   WTYPE_FORWARD       = 0x00000010,           //!< Weapon fires forwards/Slot is able to carry weapons firing forwards | 
|---|
| 62 |   WTYPE_BACKWARD      = 0x00000020,           //!< Weapon fires backwards/Slot is able to carry weapons firing backwards | 
|---|
| 63 |   WTYPE_LEFT          = 0x00000040,           //!< Weapon fires to the left/Slot is able to carry weapons firing to the left | 
|---|
| 64 |   WTYPE_RIGHT         = 0x00000080,           //!< Weapon fires to the right/Slot is able to carry weapons firing to the right | 
|---|
| 65 |   WTYPE_ALLDIRS       = 0x000000f0,           //!< Weapon has no specific firing direction/Slot can fire into all directions | 
|---|
| 66 |  | 
|---|
| 67 |   WTYPE_ALL           = 0x000000ff,           //!< Weapon has no limitations/Slot can handle all kinds of Weapon. | 
|---|
| 68 | } W_Capability; | 
|---|
| 69 |  | 
|---|
| 70 | //! An abstract class, that describes weapons | 
|---|
| 71 | /** | 
|---|
| 72 |  * This is used as a container for all the different kinds of weapons that may exist | 
|---|
| 73 |  * | 
|---|
| 74 |  * Weapons have certain states, and actions, that can inflict them. | 
|---|
| 75 |  * ex. Action WA_SHOOT leeds to State WS_SHOOTING. | 
|---|
| 76 |  * each action has a sound connected to it, | 
|---|
| 77 |  * each state a time and an animation. | 
|---|
| 78 |  */ | 
|---|
| 79 | class Weapon : public WorldEntity | 
|---|
| 80 | { | 
|---|
| 81 |   public: | 
|---|
| 82 |     // INITIALISATION // | 
|---|
| 83 |     Weapon (WeaponManager* weaponManager = NULL); | 
|---|
| 84 |     virtual ~Weapon (); | 
|---|
| 85 |  | 
|---|
| 86 |     void init(); | 
|---|
| 87 |     void loadParams(const TiXmlElement* root); | 
|---|
| 88 |     //////////////////// | 
|---|
| 89 |  | 
|---|
| 90 |     // INTERACTIVITY // | 
|---|
| 91 |     void requestAction(WeaponAction action); | 
|---|
| 92 |     float increaseEnergy(float energyToAdd); | 
|---|
| 93 |     /////////////////// | 
|---|
| 94 |  | 
|---|
| 95 |     /** @param weaponManager sets the WeaponManager for this Weapon (NULL if free)) */ | 
|---|
| 96 |     inline void setWeaponManager(WeaponManager* weaponManager) { this->weaponManager = weaponManager; }; | 
|---|
| 97 |     /** @returns the WeaponManager of this Weapon (or NULL if it is free) */ | 
|---|
| 98 |     inline WeaponManager* getWeaponManager() const { return this->weaponManager; }; | 
|---|
| 99 |  | 
|---|
| 100 |     /** @returns true if the Weapon is Active  (this is used to check if the weapon must be drawn)*/ | 
|---|
| 101 |     inline bool isActive() const { return (this->currentState == WS_INACTIVE)?false:true; }; | 
|---|
| 102 |     /** @returns true if the weapon must be drawn */ | 
|---|
| 103 |     inline bool isVisible() const { return (this->currentState != WS_INACTIVE || !this->hideInactive)?true:false; }; | 
|---|
| 104 |     /** @returns true if the Weapon is chargeable */ | 
|---|
| 105 |     inline bool isChargeable() const { return this->chargeable; }; | 
|---|
| 106 |  | 
|---|
| 107 |     // FUNCTIONS TO SET THE WEAPONS PROPERTIES. | 
|---|
| 108 |     /** sets the Weapons Capabilities */ | 
|---|
| 109 |     inline void setCapability(long capabilities) { this->capability = capabilities; }; | 
|---|
| 110 |     /** @returns the Capabilities of this Weapon */ | 
|---|
| 111 |     inline long getCapability() { return this->capability; }; | 
|---|
| 112 |     void setProjectileType(ClassID projectile); | 
|---|
| 113 |     void setProjectileType(const char* projectile); | 
|---|
| 114 |     /** @returns The projectile's classID */ | 
|---|
| 115 |     inline ClassID getProjectileType() { return this->projectile; }; | 
|---|
| 116 |     /** @returns the FastFactory, that creates Projectiles of type getProjectile */ | 
|---|
| 117 |     inline FastFactory* getProjectileFactory() { return this->projectileFactory; }; | 
|---|
| 118 |     void prepareProjectiles(unsigned int count); | 
|---|
| 119 |     Projectile* getProjectile(); | 
|---|
| 120 |  | 
|---|
| 121 |  | 
|---|
| 122 |     void setEmissionPoint(const Vector& point); | 
|---|
| 123 |     /** @see void setEmissionPoint(const Vector& point); */ | 
|---|
| 124 |     inline void setEmissionPoint(float x, float y, float z) { this->setEmissionPoint(Vector(x, y, z)); }; | 
|---|
| 125 |     /** @returns the absolute Position of the EmissionPoint */ | 
|---|
| 126 |     inline const Vector& getEmissionPoint() const { return this->emissionPoint.getAbsCoor(); }; | 
|---|
| 127 |  | 
|---|
| 128 |     /** @param state the State to time @param duration the duration of the State */ | 
|---|
| 129 |     inline void setStateDuration(const char* state, float duration) { setStateDuration(charToState(state), duration); }; | 
|---|
| 130 |     /** @param state the State to time @param duration the duration of the State */ | 
|---|
| 131 |     inline void setStateDuration(WeaponState state, float duration) { /*(state < WS_STATE_COUNT)?*/this->times[state] = duration; }; | 
|---|
| 132 |     /** @param state The state to query @returns the Time the queried State takes to complete */ | 
|---|
| 133 |     inline float getStateDuration(WeaponState state) const { return (state < WS_STATE_COUNT)?this->times[state]:0.0; }; | 
|---|
| 134 |     /** @returns true if the time of the currentState is elapsed, false otherwise */ | 
|---|
| 135 |     inline bool stateTimeElapsed() const { return (this->stateDuration > this->times[currentState])?true:false; }; | 
|---|
| 136 |     /** @returns the current State of the Weapon */ | 
|---|
| 137 |     inline WeaponState getCurrentState() const { return this->currentState; }; | 
|---|
| 138 |  | 
|---|
| 139 |     /** @param energyMax the maximum energy the Weapon can have @param energyLoadedMax the maximum energy in the weapon buffers */ | 
|---|
| 140 |     inline void setMaximumEnergy(float energyMax, float energyLoadedMax) { this->energyMax = energyMax; this->energyLoadedMax = energyLoadedMax; }; | 
|---|
| 141 |  | 
|---|
| 142 |     void setActionSound(WeaponAction action, const char* soundFile); | 
|---|
| 143 |     /** @see void setActionSound(WeaponAction action, const char* soundFile); */ | 
|---|
| 144 |     void setActionSound(const char* action, const char* soundFile) { this->setActionSound(charToAction(action), soundFile); }; | 
|---|
| 145 |  | 
|---|
| 146 |     Animation3D* getAnimation(WeaponState state, PNode* node = NULL); | 
|---|
| 147 |     Animation3D* copyAnimation(WeaponState from, WeaponState to); | 
|---|
| 148 |  | 
|---|
| 149 |     // FLOW | 
|---|
| 150 |     void tickW(float dt); //!< this is a function that must be called by the weaponManager, or any other weaponHandler, all other functions are handled from within | 
|---|
| 151 |     virtual void tick(float dt) {}; | 
|---|
| 152 |     virtual void draw() const; | 
|---|
| 153 |  | 
|---|
| 154 |     bool check() const; | 
|---|
| 155 |     void debug() const; | 
|---|
| 156 |  | 
|---|
| 157 |   protected: | 
|---|
| 158 |     //! ACTION: these functions are handled by the Weapon itself, and must be called by requestAction(WeaponAction); | 
|---|
| 159 |     virtual void activate() {}; | 
|---|
| 160 |     virtual void deactivate() {}; | 
|---|
| 161 |     virtual void charge() {}; | 
|---|
| 162 |     virtual void fire() {}; | 
|---|
| 163 |     virtual void reload() {}; | 
|---|
| 164 |     virtual void destroy() {}; | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 |     // utility: | 
|---|
| 168 |     static WeaponAction  charToAction(const char* action); | 
|---|
| 169 |     static const char*   actionToChar(WeaponAction action); | 
|---|
| 170 |     static WeaponState   charToState(const char* state); | 
|---|
| 171 |     static const char*   stateToChar(WeaponState state); | 
|---|
| 172 |  | 
|---|
| 173 |   private: | 
|---|
| 174 |     /** executive functions, that handle timing with actions. | 
|---|
| 175 |      * This is for the action-functions in derived functions to be easy | 
|---|
| 176 |      * The main function is execute, that calls all the other functions | 
|---|
| 177 |      * for being fast, the Functions are private and as such will be inlined | 
|---|
| 178 |      * into the execute function. (this is work of the compiler) | 
|---|
| 179 |      */ | 
|---|
| 180 |     bool execute(); | 
|---|
| 181 |     bool activateW(); | 
|---|
| 182 |     bool deactivateW(); | 
|---|
| 183 |     bool chargeW(); | 
|---|
| 184 |     bool fireW(); | 
|---|
| 185 |     bool reloadW(); | 
|---|
| 186 |  | 
|---|
| 187 |     inline void enterState(WeaponState state); | 
|---|
| 188 |  | 
|---|
| 189 |  | 
|---|
| 190 |   private: | 
|---|
| 191 |     // type of Weapon | 
|---|
| 192 |     long                 capability;                       //!< what capabilities the Weapon has @see W_Capability | 
|---|
| 193 |  | 
|---|
| 194 |     // it is all about energy | 
|---|
| 195 |     float                energy;                           //!< The energy stored in the weapons secondary buffers (reserve) | 
|---|
| 196 |     float                energyLoaded;                     //!< The energy stored in the weapons primary buffers (firewithout reload) | 
|---|
| 197 |     float                energyMax;                        //!< The maximal energy that can be stored in the secondary buffers (reserveMax) | 
|---|
| 198 |     float                energyLoadedMax;                  //!< The maximal energy that can be stored in the primary buffers | 
|---|
| 199 |     //! @todo move this to projectile | 
|---|
| 200 |     float                minCharge;                        //!< The minimal energy to be loaded onto one projectile if chargeable otherwise the power consumed by one projectile | 
|---|
| 201 |     float                maxCharge;                        //!< The maximal energy to be loaded onto one projectile (this is only availible if chargeable is enabled) | 
|---|
| 202 |  | 
|---|
| 203 |     //////////// | 
|---|
| 204 |     // PHASES // | 
|---|
| 205 |     //////////// | 
|---|
| 206 |     SoundSource*         soundSource;                      //!< A SoundSource to play sound from (this is connected to the PNode of the Weapon) | 
|---|
| 207 |  | 
|---|
| 208 |     WeaponState          currentState;                     //!< The State the weapon is in. | 
|---|
| 209 |     WeaponAction         requestedAction;                  //!< An action to try to Engage after the currentState ends. | 
|---|
| 210 |     float                stateDuration;                    //!< how long the state has taken until now. | 
|---|
| 211 |     float                times[WS_STATE_COUNT];            //!< Times to stay in the different States @see WeaponState. | 
|---|
| 212 |     Animation3D*         animation[WS_STATE_COUNT];        //!< Animations for all the States (you can say yourself on what part of the gun this animation acts). | 
|---|
| 213 |     SoundBuffer*         soundBuffers[WA_ACTION_COUNT];    //!< SoundBuffers for all actions @see WeaponAction. | 
|---|
| 214 |  | 
|---|
| 215 |     PNode                emissionPoint;                   //!< The point, where the projectiles are emitted. (this is coppled with the Weapon by default) | 
|---|
| 216 |  | 
|---|
| 217 |     bool                 hideInactive;                    //!< Hides the Weapon if it is inactive | 
|---|
| 218 |     bool                 chargeable;                      //!< if the Weapon is charcheable (if true, the weapon will charge before it fires.) | 
|---|
| 219 |  | 
|---|
| 220 |     ClassID              projectile;                      //!< the projectile used for this weapon (since they should be generated via macro and the FastFactory, only the ClassID must be known.) | 
|---|
| 221 |     FastFactory*         projectileFactory;               //!< A factory, that produces and handles the projectiles. | 
|---|
| 222 |  | 
|---|
| 223 |     WeaponManager*       weaponManager;                   //!< The weaponManager this weapon is connected to. if NULL it is assumed, that the weapon is freely connected to an entity without a binding WeaponManager. | 
|---|
| 224 |   }; | 
|---|
| 225 |  | 
|---|
| 226 | #endif /* _WEAPON_H */ | 
|---|