Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/resources/src/world_entities/weapons/weapon.h @ 8538

Last change on this file since 8538 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

File size: 11.8 KB
Line 
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#include "count_pointer.h"
19#include "ammo_container.h"
20
21// FORWARD DECLARATION
22class Projectile;
23class WeaponManager;
24class Animation3D;
25class TiXmlElement;
26class FastFactory;
27template<class T> class tFastFactory;
28class GLGuiWidget;
29
30//! An enumerator defining Actions a Weapon can take
31typedef 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
39
40  WA_ACTION_COUNT  =    7     //!< This must match the count of enumerations-members.
41} WeaponAction;
42
43//! An enumerator defining the States of a Weapon
44typedef enum {
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
47  WS_CHARGING      =    2,    //!< The state of charging th weapon
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
53
54  WS_STATE_COUNT  =     8     //!< This must match the count of enumerations-members.
55} WeaponState;
56
57//! an enumerator defining capabilities of a WeaponSlot
58typedef 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
62  WTYPE_LIGHT         = 0x00000004,           //!< For light Armament.
63  WTYPE_HEAVY         = 0x00000008,           //!< The heavy Armament (Cannons).
64  WTYPE_ALLKINDS      = 0x0000000f,           //!< Weapon is all types/Slot is able to carry all kinds of weapons
65
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
71
72  WTYPE_ALL           = 0x000000ff,           //!< Weapon has no limitations/Slot can handle all kinds of Weapon.
73} W_Capability;
74
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
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.
83 */
84class Weapon : public WorldEntity
85{
86  public:
87    // INITIALISATION //
88    Weapon ();
89    virtual ~Weapon ();
90
91    void init();
92    virtual void loadParams(const TiXmlElement* root);
93    ////////////////////
94
95    // INTERACTIVITY //
96    void requestAction(WeaponAction action);
97    float increaseEnergy(float energyToAdd);
98    ///////////////////
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() const { return this->capability; };
112    void setProjectileType(ClassID projectile);
113    void setProjectileTypeC(const std::string& 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    // EMISSION
123    void setEmissionPoint(const Vector& point);
124    /** @see void setEmissionPoint(const Vector& point); */
125    inline void setEmissionPoint(float x, float y, float z) { this->setEmissionPoint(Vector(x, y, z)); };
126    /** @returns the absolute Position of the EmissionPoint */
127    inline const Vector& getEmissionPoint() const { return this->emissionPoint.getAbsCoor(); };
128
129    inline void setDefaultTarget(PNode* defaultTarget) { this->defaultTarget = defaultTarget; };
130    inline PNode* getDefaultTarget() const { return this->defaultTarget; };
131
132    // STATE CHANGES //
133    /** @param state the State to time @param duration the duration of the State */
134    inline void setStateDuration(const std::string& state, float duration) { setStateDuration(charToState(state), duration); };
135    /** @param state the State to time @param duration the duration of the State */
136    inline void setStateDuration(WeaponState state, float duration) { /*(state < WS_STATE_COUNT)?*/this->times[state] = duration; };
137    /** @param state The state to query @returns the Time the queried State takes to complete */
138    inline float getStateDuration(WeaponState state) const { return (state < WS_STATE_COUNT)? this->times[state] : 0.0; };
139    /** @returns true if the time of the currentState is elapsed, false otherwise */
140    inline bool stateTimeElapsed() const { return (this->stateDuration > this->times[currentState])? true : false; };
141    /** @returns the current State of the Weapon */
142    inline WeaponState getCurrentState() const { return this->currentState; };
143
144    /** @param energyMax the maximum energy the Weapon can have */
145    inline void setEnergyMax(float energyMax) { this->energyMax = energyMax; };
146    inline float getEnergy() const { return this->energy; };
147    inline float getEnergyMax() const { return this->energyMax; };
148    inline void setAmmoContainer(const CountPointer<AmmoContainer>& ammoContainer) { this->ammoContainer = ammoContainer;}
149
150    void setActionSound(WeaponAction action, const std::string& soundFile);
151    /** @see void setActionSound(WeaponAction action, const std::string& soundFile); */
152    void setActionSound(const std::string& action, const std::string& soundFile) { this->setActionSound(charToAction(action), soundFile); };
153
154    Animation3D* getAnimation(WeaponState state, PNode* node = NULL);
155    Animation3D* copyAnimation(WeaponState from, WeaponState to);
156
157    GLGuiWidget* getEnergyWidget();
158
159    // FLOW
160    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
161
162    virtual void tick(float dt) { tickW(dt); };
163
164    bool check() const;
165    void debug() const;
166
167  protected:
168    //! ACTION: these functions are handled by the Weapon itself, and must be called by requestAction(WeaponAction);
169    virtual void activate() {};
170    virtual void deactivate() {};
171    virtual void charge() {};
172    virtual void fire() {};
173    virtual void reload() {};
174    virtual void destroy() {};
175
176
177    // utility:
178    static WeaponAction  charToAction(const std::string& action);
179    static const char*   actionToChar(WeaponAction action);
180    static WeaponState   charToState(const std::string& state);
181    static const char*   stateToChar(WeaponState state);
182
183  private:
184    /** executive functions, that handle timing with actions.
185     * This is for the action-functions in derived functions to be easy
186     * The main function is execute, that calls all the other functions
187     * for being fast, the Functions are private and as such will be inlined
188     * into the execute function. (this is work of the compiler)
189     */
190    bool execute();
191    bool activateW();
192    bool deactivateW();
193    bool chargeW();
194    bool fireW();
195    bool reloadW();
196    inline void enterState(WeaponState state);
197
198    void updateWidgets();
199
200  private:
201    // type of Weapon
202    long                 capability;                       //!< what capabilities the Weapon has @see W_Capability
203
204    // it is all about energy
205    float                energy;                           //!< The energy stored in the weapons buffers
206    float                energyMax;                        //!< The maximal energy that can be stored in the secondary buffers (reserveMax)
207    CountPointer<AmmoContainer> ammoContainer;             //!< Pointer to the AmmoContainer this weapon grabs Energy from.
208    //! @todo move this to projectile
209    float                minCharge;                        //!< The minimal energy to be loaded onto one projectile if chargeable otherwise the power consumed by one projectile
210    float                maxCharge;                        //!< The maximal energy to be loaded onto one projectile (this is only availible if chargeable is enabled)
211
212    GLGuiBar*            energyWidget;
213
214    PNode*               defaultTarget;                    //!< A target for targeting Weapons.
215
216    ////////////
217    // PHASES //
218    ////////////
219    SoundSource*         soundSource;                      //!< A SoundSource to play sound from (this is connected to the PNode of the Weapon)
220
221    WeaponState          currentState;                     //!< The State the weapon is in.
222    WeaponAction         requestedAction;                  //!< An action to try to Engage after the currentState ends.
223    float                stateDuration;                    //!< how long the state has taken until now.
224    float                times[WS_STATE_COUNT];            //!< Times to stay in the different States @see WeaponState.
225    Animation3D*         animation[WS_STATE_COUNT];        //!< Animations for all the States (you can say yourself on what part of the gun this animation acts).
226    SoundBuffer*         soundBuffers[WA_ACTION_COUNT];    //!< SoundBuffers for all actions @see WeaponAction.
227
228    PNode                emissionPoint;                   //!< The point, where the projectiles are emitted. (this is coppled with the Weapon by default)
229
230    bool                 hideInactive;                    //!< Hides the Weapon if it is inactive
231    bool                 chargeable;                      //!< if the Weapon is charcheable (if true, the weapon will charge before it fires.)
232
233    ClassID              projectile;                      //!< the projectile used for this weapon (since they should be generated via macro and the FastFactory, only the ClassID must be known.)
234    FastFactory*         projectileFactory;               //!< A factory, that produces and handles the projectiles.
235  };
236
237#endif /* _WEAPON_H */
Note: See TracBrowser for help on using the repository browser.