Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 25, 2009, 1:18:03 PM (15 years ago)
Author:
dafrick
Message:

Merged presentation2 branch into pickup2 branch.

Location:
code/branches/pickup2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pickup2

  • code/branches/pickup2/src/orxonox/items/MultiStateEngine.cc

    r5929 r6412  
    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"
     43#include "sound/WorldSound.h"
    3544
    3645namespace orxonox
    3746{
    38     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;
     47    static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 0;
     48    static const float MAX_VELOCITY_NORMAL = 111;
     49    static const float MAX_VELOCITY_BOOST = 221;
    4450
    4551    CreateFactory(MultiStateEngine);
     
    4955        RegisterObject(MultiStateEngine);
    5056
     57        if (GameMode::isMaster())
     58        {
     59            this->defEngineSndNormal_ = new WorldSound(this);
     60            this->defEngineSndBoost_  = new WorldSound(this);
     61            this->defEngineSndNormal_->setLooping(true);
     62            this->defEngineSndBoost_->setLooping(true);
     63            this->lua_ = new LuaState();
     64        }
     65        else
     66        {
     67            this->defEngineSndBoost_ = 0;
     68            this->defEngineSndNormal_ = 0;
     69            this->lua_ = 0;
     70        }
    5171        this->state_ = 0;
    52 
     72        this->oldState_ = 0;
     73
     74        this->setSyncMode(ObjectDirection::Bidirectional);
    5375        this->registerVariables();
    5476    }
     
    5678    MultiStateEngine::~MultiStateEngine()
    5779    {
    58         if (this->isInitialized() && !this->getShip())
    59         {
    60             // 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();
     80        if (this->isInitialized())
     81        {
     82            if (!this->getShip())
     83            {
     84                // We have no ship, so the effects are not attached and won't be destroyed automatically
     85                for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
     86                    for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
     87                        (*it2)->destroy();
     88                if (this->defEngineSndNormal_)
     89                    this->defEngineSndNormal_->destroy();
     90                if (this->defEngineSndBoost_)
     91                    this->defEngineSndBoost_->destroy();
     92            }
     93            if (this->lua_)
     94                delete this->lua_;
    6995        }
    7096    }
     
    7399    {
    74100        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);
     101        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
     102        XMLPortParam(MultiStateEngine, "defEngineSndNormal",  setDefEngSndNormal,  getDefEngSndNormal,  xmlelement, mode);
     103        XMLPortParam(MultiStateEngine, "defEngineSndBoost",  setDefEngSndBoost,  getDefEngSndBoost,  xmlelement, mode);
    80104    }
    81105
     
    89113        if (this->getShip())
    90114        {
     115            const Vector3& velocity = this->getShip()->getLocalVelocity();
     116
    91117            if (this->getShip()->hasLocalController())
    92118            {
    93                 this->setSyncMode(ObjectDirection::Bidirectional);
    94 
    95119                const Vector3& direction = this->getDirection();
    96                 const Vector3& velocity = this->getShip()->getLocalVelocity();
    97 
    98                 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);
    102 
    103                 if (active)
    104                     this->state_ |= STATE_ACTIVE;
     120                bool forward = (direction.z < 0.0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
     121
     122                this->state_ = 0;
     123                if (this->getShip()->getBoost() && forward)
     124                    this->state_ = Boost;
     125                else if (forward && !this->state_) // this->state_ == Boost
     126                    this->state_ = Normal;
     127                else if (direction.z > 0.0 && velocity.z < 0.0)
     128                    this->state_ = Brake;
    105129                else
    106                     this->state_ &= ~STATE_ACTIVE;
    107 
    108                 if (forward)
    109                     this->state_ |= STATE_FORWARD;
    110                 else
    111                     this->state_ &= ~STATE_FORWARD;
    112 
    113                 if (boost)
    114                     this->state_ |= STATE_BOOST;
    115                 else
    116                     this->state_ &= ~STATE_BOOST;
    117 
    118                 if (brake)
    119                     this->state_ |= STATE_BRAKE;
    120                 else
    121                     this->state_ &= ~STATE_BRAKE;
     130                    this->state_ = Idle;
    122131            }
    123132
    124133            if (GameMode::isMaster())
    125134            {
    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);
     135                int changes = this->state_ | this->oldState_;
     136
     137                float pitch = velocity.length();
     138                if (this->state_ & Normal)
     139                    defEngineSndNormal_->setPitch(clamp(pitch/MAX_VELOCITY_NORMAL + 1, 0.5f, 2.0f));
     140                if (this->state_ & Boost)
     141                    defEngineSndBoost_->setPitch(clamp(pitch/MAX_VELOCITY_BOOST + 1, 0.5f, 2.0f));
     142
     143                if (changes & Idle)
     144                {
     145                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Idle);
     146                    lua_setglobal(this->lua_->getInternalLuaState(), "idle");
     147                }
     148                if (changes & Normal)
     149                {
     150                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Normal);
     151                    lua_setglobal(this->lua_->getInternalLuaState(), "normal");
     152                    if (this->state_ & Normal)
     153                        defEngineSndNormal_->play();
     154                    else
     155                        defEngineSndNormal_->stop();
     156                }
     157                if (changes & Brake)
     158                {
     159                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Brake);
     160                    lua_setglobal(this->lua_->getInternalLuaState(), "brake");
     161                }
     162                if (changes & Boost)
     163                {
     164                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Boost);
     165                    lua_setglobal(this->lua_->getInternalLuaState(), "boost");
     166                    if (this->state_ & Boost)
     167                        defEngineSndBoost_->play();
     168                    else
     169                        defEngineSndBoost_->stop();
     170                }
     171
     172                this->oldState_ = this->state_;
     173
     174                // Update all effect conditions
     175                for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
     176                    (*it)->updateCondition();
    134177            }
    135178        }
     
    145188            return;
    146189
    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);
    155     }
    156 
    157     void MultiStateEngine::addActiveEffect(WorldEntity* effect)
    158     {
    159         this->activeEffects_.push_back(effect);
     190        if( this->defEngineSndNormal_ )
     191            this->getShip()->attach(defEngineSndNormal_);
     192        if( this->defEngineSndBoost_ )
     193            this->getShip()->attach(defEngineSndBoost_);
     194
     195        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
     196            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
     197                this->getShip()->attach(*it2);
     198    }
     199
     200    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
     201    {
     202        if (effect == NULL)
     203            return;
     204        effect->setLuaState(this->lua_, 'f' + multi_cast<std::string>(this->effectContainers_.size()));
     205        this->effectContainers_.push_back(effect);
    160206        if (this->getShip())
    161             this->getShip()->attach(effect);
    162     }
    163 
    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
     207        {
     208            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
     209                this->getShip()->attach(*it);
     210        }
     211    }
     212
     213    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
    186214    {
    187215        unsigned int i = 0;
    188         for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
     216        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
    189217        {
    190218            if (i == index)
    191219                return (*it);
    192             ++i;
    193         }
    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;
     220        }
     221        return NULL;
     222    }
     223
     224    void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound)
     225    {
     226        if( defEngineSndNormal_ )
     227            defEngineSndNormal_->setSource(engineSound);
     228        else
     229            assert(0); // This should never happen, because soundpointer is only available on master
     230    }
     231
     232    const std::string& MultiStateEngine::getDefEngSndNormal()
     233    {
     234        if( defEngineSndNormal_ )
     235            return defEngineSndNormal_->getSource();
     236        else
     237            assert(0);
     238        return BLANKSTRING;
     239    }
     240
     241    void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound)
     242    {
     243        if( defEngineSndBoost_ )
     244            defEngineSndBoost_->setSource(engineSound);
     245        else
     246            assert(0);
     247    }
     248
     249    const std::string& MultiStateEngine::getDefEngSndBoost()
     250    {
     251        if( this->defEngineSndBoost_ )
     252            return defEngineSndBoost_->getSource();
     253        else
     254            assert(0);
     255        return BLANKSTRING;
    231256    }
    232257}
Note: See TracChangeset for help on using the changeset viewer.