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