Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/weapons/weapon.h @ 10140

Last change on this file since 10140 was 10140, checked in by nicolasc, 18 years ago

defined emissionPoint as array, update get/set function, kept old as fallback

File size: 13.2 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#include "sound_buffer.h"
22
23// FORWARD DECLARATION
24class Projectile;
25class WeaponManager;
26class Animation3D;
27class TiXmlElement;
28class FastFactory;
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
58typedef enum
59{
60  WS_SHOOTING0    = 0x00000001, //!< Fireing 1st Barrel
61  WS_SHOOTING1    = 0x00000002, //!< Fireing 2nd Barrel
62  WS_SHOOTING2    = 0x00000004, //!< Fireing 3rd Barrel
63  WS_SHOOTING3    = 0x00000008, //!< Fireing 4st Barrel
64  WS_SHOOTING4    = 0x00000010, //!< Fireing 5st Barrel
65  WS_SHOOTING5    = 0x00000020, //!< Fireing 6st Barrel
66  WS_SHOOTING6    = 0x00000040, //!< Fireing 7st Barrel
67  WS_SHOOTING7    = 0x00000080, //!< Fireing 8st Barrel
68
69  WS_SS_MAX       = 0x000000ff
70}ShootingStates;
71
72//! an enumerator defining capabilities of a WeaponSlot
73typedef enum
74{
75  WTYPE_DIRECTIONAL   = 0x00000001,           //!< Weapon is directional/Slot is able to carry directional weapons
76  WTYPE_TURRET        = 0x00000002,           //!< Weapon is a turret/slot is able to carry turrets
77  WTYPE_LIGHT         = 0x00000004,           //!< For light Armament.
78  WTYPE_HEAVY         = 0x00000008,           //!< The heavy Armament (Cannons).
79  WTYPE_ALLKINDS      = 0x0000000f,           //!< Weapon is all types/Slot is able to carry all kinds of weapons
80
81  WTYPE_FORWARD       = 0x00000010,           //!< Weapon fires forwards/Slot is able to carry weapons firing forwards
82  WTYPE_BACKWARD      = 0x00000020,           //!< Weapon fires backwards/Slot is able to carry weapons firing backwards
83  WTYPE_LEFT          = 0x00000040,           //!< Weapon fires to the left/Slot is able to carry weapons firing to the left
84  WTYPE_RIGHT         = 0x00000080,           //!< Weapon fires to the right/Slot is able to carry weapons firing to the right
85  WTYPE_ALLDIRS       = 0x000000f0,           //!< Weapon has no specific firing direction/Slot can fire into all directions
86
87  WTYPE_ALL           = 0x000000ff,           //!< Weapon has no limitations/Slot can handle all kinds of Weapon.
88} W_Capability;
89
90//! An abstract class, that describes weapons
91/**
92 * This is used as a container for all the different kinds of weapons that may exist
93 *
94 * Weapons have certain states, and actions, that can inflict them.
95 * ex. Action WA_SHOOT leeds to State WS_SHOOTING.
96 * each action has a sound connected to it,
97 * each state a time and an animation.
98 */
99class Weapon : public WorldEntity
100{
101  ObjectListDeclaration(Weapon);
102
103  public:
104    // INITIALISATION //
105    Weapon ();
106    virtual ~Weapon ();
107    static Weapon* createWeapon(const ClassID& weaponID);
108    static Weapon* createWeapon(const std::string& weaponName);
109
110    void init();
111    virtual void loadParams(const TiXmlElement* root);
112    ////////////////////
113
114    // INTERACTIVITY //
115    void requestAction(WeaponAction action);
116    float increaseEnergy(float energyToAdd);
117    ///////////////////
118
119    /** @returns true if the Weapon is Active  (this is used to check if the weapon must be drawn)*/
120    inline bool isActive() const { return (this->currentState == WS_INACTIVE)? false : true; };
121    /** @returns true if the weapon must be drawn */
122    inline bool isVisible() const { return (this->currentState != WS_INACTIVE || !this->hideInactive) ? true : false; };
123    /** @returns true if the Weapon is chargeable */
124    inline bool isChargeable() const { return this->chargeable; };
125
126    // FUNCTIONS TO SET THE WEAPONS PROPERTIES.
127    /** sets the Weapons Capabilities */
128    inline void setCapability(long capabilities) { this->capability = capabilities; };
129    /** @returns the Capabilities of this Weapon */
130    inline long getCapability() const { return this->capability; };
131    void setProjectileType(const ClassID& projectile);
132    void setProjectileTypeC(const std::string& projectile);
133    /** @returns The projectile's classID */
134    inline ClassID getProjectileType() { return this->projectile; };
135    /** @returns the FastFactory, that creates Projectiles of type getProjectile */
136    inline FastFactory* getProjectileFactory() { return this->projectileFactory; };
137    void prepareProjectiles(unsigned int count);
138    Projectile* getProjectile();
139
140
141    // EMISSION
142    /** keeping old functions*/
143    void setEmissionPoint(const Vector& point, int barrel);
144    void setEmissionPoint(const Vector& point);
145    /** @see void setEmissionPoint(const Vector& point); */
146    inline void setEmissionPoint(float x, float y, float z, int barrel) { this->setEmissionPoint(Vector(x, y, z), barrel); };
147    inline void setEmissionPoint(float x, float y, float z) { this->setEmissionPoint(Vector(x, y, z)); };
148    /** @returns the absolute Position of the EmissionPoint */
149    inline const Vector& getEmissionPoint(int barrel) const { return this->emissionPoint[barrel]->getAbsCoor(); };
150    inline const Vector& getEmissionPoint() const { return this->emissionPoint[0]->getAbsCoor(); };
151
152
153    inline void setDefaultTarget(PNode* defaultTarget) { this->defaultTarget = defaultTarget; };
154    inline PNode* getDefaultTarget() const { return this->defaultTarget; };
155
156    // STATE CHANGES //
157    /** @param state the State to time @param duration the duration of the State */
158    inline void setStateDuration(const std::string& state, float duration) { setStateDuration(charToState(state), duration); };
159    /** @param state the State to time @param duration the duration of the State */
160    inline void setStateDuration(WeaponState state, float duration) { /*(state < WS_STATE_COUNT)?*/this->times[state] = duration; };
161    /** @param state The state to query @returns the Time the queried State takes to complete */
162    inline float getStateDuration(WeaponState state) const { return (state < WS_STATE_COUNT)? this->times[state] : 0.0; };
163    /** @returns true if the time of the currentState is elapsed, false otherwise */
164    inline bool stateTimeElapsed() const { return (this->stateDuration > this->times[currentState])? true : false; };
165    /** @returns the current State of the Weapon */
166    inline WeaponState getCurrentState() const { return this->currentState; };
167
168    /** @param energyMax the maximum energy the Weapon can have */
169    inline void setEnergyMax(float energyMax) { this->energyMax = energyMax; };
170    inline float getEnergy() const { return this->energy; };
171    inline float getEnergyMax() const { return this->energyMax; };
172    inline void setAmmoContainer(const CountPointer<AmmoContainer>& ammoContainer) { this->ammoContainer = ammoContainer;}
173
174    void setActionSound(WeaponAction action, const std::string& soundFile);
175    /** @see void setActionSound(WeaponAction action, const std::string& soundFile); */
176    void setActionSound(const std::string& action, const std::string& soundFile) { this->setActionSound(charToAction(action), soundFile); };
177
178    Animation3D* getAnimation(WeaponState state, PNode* node = NULL);
179    Animation3D* getAnimation(ShootingStates state, PNode* node = NULL);
180    Animation3D* copyAnimation(WeaponState from, WeaponState to);
181
182    OrxGui::GLGuiWidget* getEnergyWidget();
183
184    // FLOW
185    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
186
187    virtual void tick(float dt) { tickW(dt); };
188
189    bool check() const;
190    void debug() const;
191
192   
193
194  protected:
195    //! ACTION: these functions are handled by the Weapon itself, and must be called by requestAction(WeaponAction);
196    virtual void activate() {};
197    virtual void deactivate() {};
198    virtual void charge() {};
199    virtual void fire() {};
200    virtual void reload() {};
201
202
203    // utility:
204    static WeaponAction  charToAction(const std::string& action);
205    static const char*   actionToChar(WeaponAction action);
206    static WeaponState   charToState(const std::string& state);
207    static const char*   stateToChar(WeaponState state);
208
209
210    inline int getBarrels() {return this->barrels; };
211    inline int getSeg() {return this->segs; };
212    inline void setBarrels(int barrels) { this->barrels = barrels; };
213    inline void setSegs(int segs) { this->segs = segs; };
214
215  private:
216    /** executive functions, that handle timing with actions.
217     * This is for the action-functions in derived functions to be easy
218     * The main function is execute, that calls all the other functions
219     * for being fast, the Functions are private and as such will be inlined
220     * into the execute function. (this is work of the compiler)
221     */
222    bool execute();
223    bool activateW();
224    bool deactivateW();
225    bool chargeW();
226    bool fireW();
227    bool reloadW();
228    inline void enterState(WeaponState state);
229
230    void updateWidgets();
231
232  private:
233    // type of Weapon
234    long                 capability;                       //!< what capabilities the Weapon has @see W_Capability
235
236    // it is all about energy
237    float                energy;                           //!< The energy stored in the weapons buffers
238    float                energyMax;                        //!< The maximal energy that can be stored in the secondary buffers (reserveMax)
239    CountPointer<AmmoContainer> ammoContainer;             //!< Pointer to the AmmoContainer this weapon grabs Energy from.
240    //! @todo move this to projectile
241    float                minCharge;                        //!< The minimal energy to be loaded onto one projectile if chargeable otherwise the power consumed by one projectile
242    float                maxCharge;                        //!< The maximal energy to be loaded onto one projectile (this is only availible if chargeable is enabled)
243
244    OrxGui::GLGuiEnergyWidget* energyWidget;
245
246    PNode*               defaultTarget;                    //!< A target for targeting Weapons.
247
248    ////////////
249    // PHASES //
250    ////////////
251    OrxSound::SoundSource* soundSource;                     //!< A SoundSource to play sound from (this is connected to the PNode of the Weapon)
252
253    WeaponState            currentState;                    //!< The State the weapon is in.
254    WeaponAction           requestedAction;                 //!< An action to try to Engage after the currentState ends.
255    float                  stateDuration;                   //!< how long the state has taken until now.
256    float                  times[WS_STATE_COUNT];           //!< Times to stay in the different States @see WeaponState.
257    Animation3D*           animation[WS_STATE_COUNT];       //!< Animations for all the States (you can say yourself on what part of the gun this animation acts).
258    OrxSound::SoundBuffer  soundBuffers[WA_ACTION_COUNT];   //!< SoundBuffers for all actions @see WeaponAction.
259
260    PNode**                emissionPoint;                   //!< The point, where the projectiles are emitted. (this is coppled with the Weapon by default)
261
262    bool                   hideInactive;                    //!< Hides the Weapon if it is inactive
263    bool                   chargeable;                      //!< if the Weapon is charcheable (if true, the weapon will charge before it fires.)
264
265    ClassID                projectile;                      //!< the projectile used for this weapon (since they should be generated via macro and the FastFactory, only the ClassID must be known.)
266    FastFactory*           projectileFactory;               //!< A factory, that produces and handles the projectiles.
267
268    int                    barrels;                         //!< # of barrels
269    int                    segs;                             //!< # of segments, one barrel has
270  };
271
272#endif /* _WEAPON_H */
Note: See TracBrowser for help on using the repository browser.