Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 1, 2009, 6:48:51 PM (15 years ago)
Author:
rgrieder
Message:

Added better engine effects: you can still add any WorldEntity to the MultiStateEngine, but you have to enclose it in an EffectContainer that has one XMLParameter called "condition". There you can write conditions containing the following words: and, or, not, idle, normal, boost, brake. The last four words are mutually exclusive states.
For an example see spaceship_assff.oxt

Location:
code/branches/presentation2/src/orxonox/items
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2/src/orxonox/items/MultiStateEngine.cc

    r5929 r6187  
    2222 *   Author:
    2323 *      Fabian 'x3n' Landau
     24 *      Reto Grieder
    2425 *   Co-authors:
    2526 *      ...
     
    2930#include "MultiStateEngine.h"
    3031
     32extern "C" {
     33#include <lua.h>
     34}
     35
     36#include "util/Convert.h"
    3137#include "core/CoreIncludes.h"
    3238#include "core/GameMode.h"
     39#include "core/LuaState.h"
    3340#include "core/XMLPort.h"
     41#include "worldentities/EffectContainer.h"
    3442#include "worldentities/pawns/SpaceShip.h"
    3543
     
    3745{
    3846    static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20;
    39 
    40     static const unsigned char STATE_ACTIVE  = 1;
    41     static const unsigned char STATE_FORWARD = 2;
    42     static const unsigned char STATE_BOOST   = 4;
    43     static const unsigned char STATE_BRAKE   = 8;
    4447
    4548    CreateFactory(MultiStateEngine);
     
    4952        RegisterObject(MultiStateEngine);
    5053
     54        this->lua_ = new LuaState();
    5155        this->state_ = 0;
    5256
     
    5963        {
    6064            // We have no ship, so the effects are not attached and won't be destroyed automatically
    61             for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
    62                 (*it)->destroy();
    63             for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
    64                 (*it)->destroy();
    65             for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
    66                 (*it)->destroy();
    67             for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
    68                 (*it)->destroy();
     65            for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
     66                for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
     67                    (*it2)->destroy();
     68            delete this->lua_;
    6969        }
    7070    }
     
    7373    {
    7474        SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
    75 
    76         XMLPortObject(MultiStateEngine, WorldEntity, "active",  addActiveEffect,  getActiveEffect,  xmlelement, mode);
    77         XMLPortObject(MultiStateEngine, WorldEntity, "forward", addForwardEffect, getForwardEffect, xmlelement, mode);
    78         XMLPortObject(MultiStateEngine, WorldEntity, "boost",   addBoostEffect,   getBoostEffect,   xmlelement, mode);
    79         XMLPortObject(MultiStateEngine, WorldEntity, "brake",   addBrakeEffect,   getBrakeEffect,   xmlelement, mode);
     75        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
    8076    }
    8177
     
    9793
    9894                bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
    99                 bool boost = (this->getShip()->getBoost() && forward);
    100                 bool brake = (direction.z > 0 && velocity.z < 0);
    101                 bool active = (direction != Vector3::ZERO && !brake);
    10295
    103                 if (active)
    104                     this->state_ |= STATE_ACTIVE;
     96                int newState = 0;
     97                if (this->getShip()->getBoost() && forward)
     98                    newState = Boost;
     99                else if (forward && !newState) // newState == Boost
     100                    newState = Normal;
     101                else if (direction.z > 0 && velocity.z < 0)
     102                    newState = Brake;
    105103                else
    106                     this->state_ &= ~STATE_ACTIVE;
     104                    newState = Idle;
    107105
    108                 if (forward)
    109                     this->state_ |= STATE_FORWARD;
    110                 else
    111                     this->state_ &= ~STATE_FORWARD;
     106                if (newState != this->state_)
     107                {
     108                    int changes = newState | this->state_;
     109                    if (changes & Idle)
     110                    {
     111                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle);
     112                        lua_setglobal(this->lua_->getInternalLuaState(), "idle");
     113                    }
     114                    if (changes & Normal)
     115                    {
     116                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal);
     117                        lua_setglobal(this->lua_->getInternalLuaState(), "normal");
     118                    }
     119                    if (changes & Brake)
     120                    {
     121                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake);
     122                        lua_setglobal(this->lua_->getInternalLuaState(), "brake");
     123                    }
     124                    if (changes & Boost)
     125                    {
     126                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost);
     127                        lua_setglobal(this->lua_->getInternalLuaState(), "boost");
     128                    }
    112129
    113                 if (boost)
    114                     this->state_ |= STATE_BOOST;
    115                 else
    116                     this->state_ &= ~STATE_BOOST;
     130                    // Update all effect conditions
     131                    for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
     132                        (*it)->updateCondition();
    117133
    118                 if (brake)
    119                     this->state_ |= STATE_BRAKE;
    120                 else
    121                     this->state_ &= ~STATE_BRAKE;
     134                    this->state_ = newState;
     135                }
    122136            }
    123137
    124138            if (GameMode::isMaster())
    125139            {
    126                 for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
    127                     (*it)->setMainState(this->state_ & STATE_ACTIVE);
    128                 for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
    129                     (*it)->setMainState(this->state_ & STATE_FORWARD);
    130                 for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
    131                     (*it)->setMainState(this->state_ & STATE_BOOST);
    132                 for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
    133                     (*it)->setMainState(this->state_ & STATE_BRAKE);
    134140            }
    135141        }
     
    145151            return;
    146152
    147         for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
    148             ship->attach(*it);
    149         for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
    150             ship->attach(*it);
    151         for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
    152             ship->attach(*it);
    153         for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
    154             ship->attach(*it);
     153        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
     154            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
     155                this->getShip()->attach(*it2);
    155156    }
    156157
    157     void MultiStateEngine::addActiveEffect(WorldEntity* effect)
     158    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
    158159    {
    159         this->activeEffects_.push_back(effect);
     160        if (effect == NULL)
     161            return;
     162        effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size()));
     163        this->effectContainers_.push_back(effect);
    160164        if (this->getShip())
    161             this->getShip()->attach(effect);
     165        {
     166            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
     167                this->getShip()->attach(*it);
     168        }
    162169    }
    163170
    164     void MultiStateEngine::addForwardEffect(WorldEntity* effect)
    165     {
    166         this->forwardEffects_.push_back(effect);
    167         if (this->getShip())
    168             this->getShip()->attach(effect);
    169     }
    170 
    171     void MultiStateEngine::addBoostEffect(WorldEntity* effect)
    172     {
    173         this->boostEffects_.push_back(effect);
    174         if (this->getShip())
    175             this->getShip()->attach(effect);
    176     }
    177 
    178     void MultiStateEngine::addBrakeEffect(WorldEntity* effect)
    179     {
    180         this->brakeEffects_.push_back(effect);
    181         if (this->getShip())
    182             this->getShip()->attach(effect);
    183     }
    184 
    185     WorldEntity* MultiStateEngine::getActiveEffect(unsigned int index) const
     171    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
    186172    {
    187173        unsigned int i = 0;
    188         for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
     174        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
    189175        {
    190176            if (i == index)
    191177                return (*it);
    192             ++i;
    193178        }
    194         return 0;
    195     }
    196 
    197     WorldEntity* MultiStateEngine::getForwardEffect(unsigned int index) const
    198     {
    199         unsigned int i = 0;
    200         for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
    201         {
    202             if (i == index)
    203                 return (*it);
    204             ++i;
    205         }
    206         return 0;
    207     }
    208 
    209     WorldEntity* MultiStateEngine::getBoostEffect(unsigned int index) const
    210     {
    211         unsigned int i = 0;
    212         for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
    213         {
    214             if (i == index)
    215                 return (*it);
    216             ++i;
    217         }
    218         return 0;
    219     }
    220 
    221     WorldEntity* MultiStateEngine::getBrakeEffect(unsigned int index) const
    222     {
    223         unsigned int i = 0;
    224         for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
    225         {
    226             if (i == index)
    227                 return (*it);
    228             ++i;
    229         }
    230         return 0;
     179        return NULL;
    231180    }
    232181}
  • code/branches/presentation2/src/orxonox/items/MultiStateEngine.h

    r5781 r6187  
    2222 *   Author:
    2323 *      Fabian 'x3n' Landau
     24 *      Reto Grieder
    2425 *   Co-authors:
    2526 *      ...
     
    3233#include "OrxonoxPrereqs.h"
    3334
    34 #include <list>
     35#include <vector>
    3536#include "Engine.h"
    3637
    3738namespace orxonox
    3839{
     40    // forward declaration
     41    class EffectContainer;
     42
    3943    class _OrxonoxExport MultiStateEngine : public Engine
    4044    {
    4145        public:
     46            enum EngineState
     47            {
     48                Idle    = 1,
     49                Normal  = 2,
     50                Brake   = 4,
     51                Boost   = 8
     52            };
     53
    4254            MultiStateEngine(BaseObject* creator);
    4355            virtual ~MultiStateEngine();
     
    5062            virtual void addToSpaceShip(SpaceShip* ship);
    5163
    52             void addActiveEffect(WorldEntity* effect);
    53             void addForwardEffect(WorldEntity* effect);
    54             void addBoostEffect(WorldEntity* effect);
    55             void addBrakeEffect(WorldEntity* effect);
    56 
    57             WorldEntity* getActiveEffect(unsigned int index) const;
    58             WorldEntity* getForwardEffect(unsigned int index) const;
    59             WorldEntity* getBoostEffect(unsigned int index) const;
    60             WorldEntity* getBrakeEffect(unsigned int index) const;
     64            void addEffectContainer(EffectContainer* effect);
     65            EffectContainer* getEffectContainer(unsigned int index) const;
    6166
    6267        private:
    63             unsigned char state_;
    64             std::list<WorldEntity*> activeEffects_;
    65             std::list<WorldEntity*> forwardEffects_;
    66             std::list<WorldEntity*> boostEffects_;
    67             std::list<WorldEntity*> brakeEffects_;
     68            int state_;
     69            LuaState* lua_;
     70            std::vector<EffectContainer*> effectContainers_;
    6871    };
    6972}
Note: See TracChangeset for help on using the changeset viewer.