Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/weapons/weapon.h @ 4952

Last change on this file since 4952 was 4950, checked in by bensch, 19 years ago

orxonox/trunk: more loadability functionality in Weapon and FastFactory

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