Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4892 in orxonox.OLD


Ignore:
Timestamp:
Jul 19, 2005, 4:59:58 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: brought logic to the execution-cycle.
There are now some subfunction fireW() that sets all the properties needed for a shoot, and then the extension (derived class) can define the Fire function itself

Location:
orxonox/trunk/src/world_entities/weapons
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/world_entities/weapons/test_bullet.cc

    r4890 r4892  
    1919#include "test_bullet.h"
    2020
    21 #include "world_entity.h"
    22 #include "weapon.h"
    23 #include "null_parent.h"
    2421#include "model.h"
    2522#include "vector.h"
  • orxonox/trunk/src/world_entities/weapons/test_bullet.h

    r4890 r4892  
    1414class TestBullet : public Projectile
    1515{
    16   friend class World;
    17 
    1816 public:
    1917  TestBullet (Weapon* weapon);
  • orxonox/trunk/src/world_entities/weapons/weapon.cc

    r4891 r4892  
    3030#include "sound_engine.h"
    3131
     32////////////////////
     33// INITAILISATION //
     34// SETTING VALUES //
     35////////////////////
    3236/**
    3337 * standard constructor
     
    7478  this->requestedAction = WA_NONE;
    7579  this->soundSource = new SoundSource(this);
     80  this->emissionPoint.setParent(this);
    7681
    7782  this->active = true;
     
    8691}
    8792
     93
     94/**
     95 * sets the emissionPoint's relative position from the Weapon
     96 * @param point the Point relative to the mass-point of the Weapon
     97 */
     98void Weapon::setEmissionPoint(const Vector& point)
     99{
     100  this->emissionPoint.setRelCoor(point);
     101}
    88102
    89103/**
     
    112126}
    113127
     128/////////////////
     129//  EXECUTION  //
     130// GAME ACTION //
     131/////////////////
     132
    114133/**
    115134 * request an action that should be executed,
     
    152171}
    153172
     173//////////////////////
     174// WEAPON INTERNALS //
     175//////////////////////
    154176/**
    155177 * executes an action, and with it starts a new State.
     
    169191  {
    170192    case WA_SHOOT:
    171       //if (likely(this->currentState != WS_INACTIVE))
    172       {
    173         if (this->minCharge <= this->energyLoaded)
    174         {
     193      this->fireW();
     194      break;
     195    case WA_CHARGE:
     196      this->chargeW();
     197      break;
     198    case WA_RELOAD:
     199      this->reloadW();
     200      break;
     201    case WA_DEACTIVATE:
     202      this->deactivateW();
     203      break;
     204    case WA_ACTIVATE:
     205      this->activateW();
     206      break;
     207  }
     208}
     209
     210
     211/**
     212 *
     213 * @return
     214 */
     215bool Weapon::activateW()
     216{
     217
     218  if (this->currentState == WS_INACTIVE)
     219  {
     220        // play Sound
     221    if (this->soundBuffers[WA_ACTIVATE] != NULL)
     222      this->soundSource->play(this->soundBuffers[WA_ACTIVATE]);
     223        // activate
     224    PRINTF(5)("Activating the Weapon %s\n", this->getName());
     225    this->activate();
     226        // setting up for next action
     227    this->requestedAction = WA_NONE;
     228    this->stateDuration = this->times[WA_ACTIVATE] + this->stateDuration;
     229  }
     230}
     231
     232
     233/**
     234 *
     235 * @return
     236 */
     237bool Weapon::deactivateW()
     238{
     239  if (this->currentState != WS_INACTIVE)
     240  {
     241    PRINTF(4)("Deactivating the Weapon %s\n", this->getName());
     242        // play Sound
     243    if (this->soundBuffers[WA_DEACTIVATE] != NULL)
     244      this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]);
     245        // deactivate
     246    this->deactivate();
     247        // setting up for next action
     248    this->requestedAction = WA_NONE;
     249    this->stateDuration = this->times[WA_DEACTIVATE] + this->stateDuration;
     250  }
     251
     252}
     253
     254/**
     255 *
     256 * @return
     257 */
     258bool Weapon::chargeW()
     259{
     260  if ( this->currentState != WS_INACTIVE && this->energyLoaded >= this->minCharge)
     261  {
     262        // playing Sound
     263    if (this->soundBuffers[WA_CHARGE] != NULL)
     264      this->soundSource->play(this->soundBuffers[WA_CHARGE]);
     265        // charge
     266    this->charge();
     267        // setting up for the next state
     268    this->requestedAction = WA_NONE;
     269    this->stateDuration = this->times[WA_CHARGE] + this->stateDuration;
     270  }
     271  else // deactivate the Weapon if we do not have enough energy
     272  {
     273    this->requestedAction = WA_NONE;
     274    this->requestAction(WA_RELOAD);
     275  }
     276
     277}
     278
     279/**
     280 *
     281 * @return
     282 */
     283bool Weapon::fireW()
     284{
     285     //if (likely(this->currentState != WS_INACTIVE))
     286  if (this->minCharge <= this->energyLoaded)
     287  {
    175288          // playing Sound
    176           if (this->soundBuffers[WA_SHOOT] != NULL)
    177             this->soundSource->play(this->soundBuffers[WA_SHOOT]);
     289    if (this->soundBuffers[WA_SHOOT] != NULL)
     290      this->soundSource->play(this->soundBuffers[WA_SHOOT]);
    178291          // fire
    179           this->fire();
     292    this->fire();
     293    this->energyLoaded -= this->minCharge;
    180294          // setting up for the next state
    181           this->stateDuration = this->times[WA_SHOOT] + this->stateDuration;
    182           this->requestedAction = WA_NONE;
    183         }
    184         else  // reload if we still have the charge
    185         {
    186           this->requestedAction = WA_NONE;
    187           this->requestAction(WA_RELOAD);
    188         }
    189       }
    190       break;
    191     case WA_CHARGE:
    192       if ( this->currentState != WS_INACTIVE && this->energyLoaded >= this->minCharge)
    193       {
    194         // playing Sound
    195         if (this->soundBuffers[WA_CHARGE] != NULL)
    196          this->soundSource->play(this->soundBuffers[WA_CHARGE]);
    197         // charge
    198         this->charge();
    199         // setting up for the next state
    200         this->requestedAction = WA_NONE;
    201         this->stateDuration = this->times[WA_CHARGE] + this->stateDuration;
    202       }
    203       else // deactivate the Weapon if we do not have enough energy
    204       {
    205         this->requestedAction = WA_NONE;
    206         this->requestAction(WA_RELOAD);
    207       }
    208       break;
    209     case WA_RELOAD:
    210       //if (this->currentState != WS_INACTIVE && this->energy + this->energyLoaded >= this->minCharge)
    211       {
    212         // playing Sound
    213         if (this->soundBuffers[WA_RELOAD] != NULL)
    214           this->soundSource->play(this->soundBuffers[WA_RELOAD]);
    215         // reload
    216         this->reload();
    217         // setting up for next action
    218         this->requestedAction = WA_NONE;
    219         this->stateDuration = this->times[WA_RELOAD] + this->stateDuration;
    220       }
    221       break;
    222     case WA_DEACTIVATE:
    223       if (this->currentState != WS_INACTIVE)
    224       {
    225         // play Sound
    226         if (this->soundBuffers[WA_DEACTIVATE] != NULL)
    227           this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]);
    228         // deactivate
    229         this->deactivate();
    230         // setting up for next action
    231         this->requestedAction = WA_NONE;
    232         this->stateDuration = this->times[WA_DEACTIVATE] + this->stateDuration;
    233       }
    234       break;
    235     case WA_ACTIVATE:
    236       if (this->currentState == WS_INACTIVE)
    237       {
    238         // play Sound
    239         if (this->soundBuffers[WA_ACTIVATE] != NULL)
    240           this->soundSource->play(this->soundBuffers[WA_ACTIVATE]);
    241         // activate
    242         this->activate();
    243         // setting up for next action
    244         this->requestedAction = WA_NONE;
    245         this->stateDuration = this->times[WA_ACTIVATE] + this->stateDuration;
    246       }
    247       break;
    248   }
    249 }
    250 
    251 /**
    252  * this activates the weapon
    253 */
    254 void Weapon::activate()
    255 {
    256   PRINTF(4)("Activating the Weapon %s\n", this->getName());
    257 
    258   if (this->soundBuffers[WA_ACTIVATE] != NULL)
    259     this->soundSource->play(this->soundBuffers[WA_ACTIVATE]);
    260 }
    261 
    262 
    263 /**
    264  * this deactivates the weapon
    265 */
    266 void Weapon::deactivate()
    267 {
    268   PRINTF(4)("Deactivating the Weapon %s\n", this->getName());
    269 
    270   if (this->soundBuffers[WA_DEACTIVATE] != NULL)
    271     this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]);
    272 }
    273 
    274 void Weapon::fire()
    275 {
    276   this->energyLoaded -= this->minCharge;
    277 
    278   if (this->soundBuffers[WA_SHOOT] != NULL)
    279     this->soundSource->play(this->soundBuffers[WA_SHOOT]);
    280 }
    281 
    282 void Weapon::reload()
     295    this->stateDuration = this->times[WA_SHOOT] + this->stateDuration;
     296    this->requestedAction = WA_NONE;
     297  }
     298  else  // reload if we still have the charge
     299  {
     300    this->requestedAction = WA_NONE;
     301    this->requestAction(WA_RELOAD);
     302  }
     303}
     304
     305
     306/**
     307 *
     308 * @return
     309 */
     310bool Weapon::reloadW()
    283311{
    284312  PRINTF(4)("Reloading Weapon %s\n", this->getName());
    285   if (this->energy + this->energyLoaded < this->minCharge)
     313  if (unlikely(this->energy + this->energyLoaded < this->minCharge))
    286314  {
    287315    this->requestAction(WA_DEACTIVATE);
    288     return;
     316    return false;
    289317  }
    290318
    291319  float chargeSize = this->energyLoadedMax - this->energyLoaded;       //!< The energy to be charged
     320
     321  if (this->soundBuffers[WA_RELOAD] != NULL)
     322    this->soundSource->play(this->soundBuffers[WA_RELOAD]);
    292323
    293324  if (chargeSize > this->energy)
     
    303334    this->energy -= chargeSize;
    304335  }
    305   if (this->soundBuffers[WA_RELOAD] != NULL)
    306     this->soundSource->play(this->soundBuffers[WA_RELOAD]);
    307 
    308 }
    309 
    310 void Weapon::charge()
    311 {
    312   if (this->soundBuffers[WA_CHARGE] != NULL)
    313     this->soundSource->play(this->soundBuffers[WA_CHARGE]);
    314 
    315 }
    316 
    317 
    318 /**
    319  *  is called, when the weapon is destroyed
    320  *
    321  * this is in conjunction with the hit function, so when a weapon is able to get
    322  * hit, it can also be destoryed.
    323 */
    324 void Weapon::destroy ()
    325 {}
     336  this->reload();
     337
     338  this->requestedAction = WA_NONE;
     339  this->stateDuration = this->times[WA_RELOAD] + this->stateDuration;
     340
     341}
    326342
    327343
  • orxonox/trunk/src/world_entities/weapons/weapon.h

    r4891 r4892  
    1919#define _WEAPON_H
    2020
    21 #include "base_object.h"
    2221#include "world_entity.h"
    2322
     
    9190    Projectile* getProjectile() { return this->projectile; };
    9291
     92    void setEmissionPoint(const Vector& point);
     93    /** @see void setEmissionPoint(const Vector& point); */
     94    inline void setEmissionPoint(float x, float y, float z) { this->setEmissionPoint(Vector(x,y,z)); };
     95
    9396    /** @param state the State to time @param duration the duration of the State */
    9497    inline void setStateDuration(const char* state, float duration) { setStateDuration(charToState(state), duration); };
     
    101104    /** @returns the current State of the Weapon */
    102105    inline WeaponState getCurrentState() const { return this->currentState; };
     106
    103107    /** @param energyMax the maximum energy the Weapon can have @param energyLoadedMax the maximum energy in the weapon buffers */
    104108    inline void setMaximumEnergy(float energyMax, float energyLoadedMax = 0.0) { this->energyMax = energyMax; this->energyLoadedMax = energyLoadedMax; };
     
    108112    void setActionSound(const char* action, const char* soundFile) { this->setActionSound(charToAction(action), soundFile); };
    109113
    110     virtual void destroy();
    111114
    112115
     
    120123  protected:
    121124    //! ACTION: these functions are handled by the Weapon itself, and must be called by requestAction(WeaponAction);
    122     bool execute();
    123     virtual void activate();
    124     virtual void deactivate();
    125     virtual void fire();
    126     virtual void reload();
    127     virtual void charge();
     125    virtual void activate() {};
     126    virtual void deactivate() {};
     127    virtual void charge() {};
     128    virtual void fire() {};
     129    virtual void reload() {};
     130    virtual void destroy() {};
    128131
    129132
     
    136139
    137140  private:
     141    /** executive functions, that handle timing with actions.
     142     * This is for the action-functions in derived functions to be easy
     143     * The main function is execute, that calls all the other functions
     144     * for being fast, the Functions are private and as such will be inlined
     145     * into the execute function. (this is work of the compiler)
     146     */
     147    bool execute();
     148    bool activateW();
     149    bool deactivateW();
     150    bool chargeW();
     151    bool fireW();
     152    bool reloadW();
     153
    138154    bool nextActionValid() const;
    139155
     
    161177    SoundBuffer*         soundBuffers[WA_ACTION_COUNT];    //!< SoundBuffers for all actions @see WeaponAction.
    162178
     179    PNode                emissionPoint;                   //!< The point, where the projectiles are emitted. (this is coppled with the Weapon by default)
    163180
    164181    bool                 active;                          //!< states wheter the weapon is enabled or not
Note: See TracChangeset for help on using the changeset viewer.