Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/weapons/weapon.h @ 10771

Last change on this file since 10771 was 10771, checked in by nicolasc, 17 years ago

huge diff
cleaned the individual weapons, moved stuff to weapon.{cc,h}
and some minor fixes which popped up then and when

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