Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/weapons/weapon.h @ 9715

Last change on this file since 9715 was 9715, checked in by bensch, 18 years ago

renamed newclassid to classid and newobjectlist to objectlist

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