Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/items/MultiStateEngine.cc @ 11652

Last change on this file since 11652 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 9.1 KB
RevLine 
[2254]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
[6417]24 *      Reto Grieder
[2254]25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "MultiStateEngine.h"
31
[6417]32extern "C" {
33#include <lua.h>
34}
35
36#include "util/Convert.h"
[3196]37#include "core/CoreIncludes.h"
[2896]38#include "core/GameMode.h"
[6417]39#include "core/LuaState.h"
[2254]40#include "core/XMLPort.h"
[6417]41#include "worldentities/EffectContainer.h"
[5735]42#include "worldentities/pawns/SpaceShip.h"
[6417]43#include "sound/WorldSound.h"
[2254]44
45namespace orxonox
46{
[6417]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;
[2254]50
[9667]51    RegisterClass(MultiStateEngine);
[2254]52
[9667]53    MultiStateEngine::MultiStateEngine(Context* context) : Engine(context)
[2254]54    {
55        RegisterObject(MultiStateEngine);
56
[6417]57        if (GameMode::isMaster())
58        {
[9667]59            this->defEngineSndNormal_ = new WorldSound(this->getContext());
60            this->defEngineSndBoost_  = new WorldSound(this->getContext());
[6417]61            this->defEngineSndNormal_->setLooping(true);
62            this->defEngineSndBoost_->setLooping(true);
[9939]63            this->defEngineSndNormal_->setVolume(0.8f);
64            this->defEngineSndBoost_->setVolume(0.5f);
[6417]65            this->lua_ = new LuaState();
66        }
67        else
68        {
[11071]69            this->defEngineSndBoost_ = nullptr;
70            this->defEngineSndNormal_ = nullptr;
71            this->lua_ = nullptr;
[6417]72        }
[2254]73        this->state_ = 0;
[6417]74        this->oldState_ = 0;
[2254]75
[6417]76        this->setSyncMode(ObjectDirection::Bidirectional);
[2254]77        this->registerVariables();
78    }
79
80    MultiStateEngine::~MultiStateEngine()
81    {
[6417]82        if (this->isInitialized())
[2254]83        {
[6417]84            if (!this->getShip())
85            {
86                // We have no ship, so the effects are not attached and won't be destroyed automatically
[11071]87                for (EffectContainer* container : this->effectContainers_)
88                    for (WorldEntity* effect : container->getEffects())
89                        effect->destroy();
[6417]90                if (this->defEngineSndNormal_)
91                    this->defEngineSndNormal_->destroy();
92                if (this->defEngineSndBoost_)
93                    this->defEngineSndBoost_->destroy();
94            }
95            if (this->lua_)
96                delete this->lua_;
[2254]97        }
98    }
99
100    void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
101    {
102        SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
[6417]103        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
104        XMLPortParam(MultiStateEngine, "defEngineSndNormal",  setDefEngSndNormal,  getDefEngSndNormal,  xmlelement, mode);
105        XMLPortParam(MultiStateEngine, "defEngineSndBoost",  setDefEngSndBoost,  getDefEngSndBoost,  xmlelement, mode);
[2254]106    }
107
108    void MultiStateEngine::registerVariables()
109    {
[3280]110        registerVariable(this->state_, VariableDirection::ToServer);
[2254]111    }
112
[8727]113    void MultiStateEngine::run(float dt)
[2254]114    {
115        if (this->getShip())
116        {
[6417]117            const Vector3& velocity = this->getShip()->getLocalVelocity();
118
[2254]119            if (this->getShip()->hasLocalController())
120            {
[8727]121                const Vector3& direction = this->getSteering();
[6417]122                bool forward = (direction.z < 0.0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
[2254]123
[6417]124                this->state_ = 0;
[8727]125                if (this->getShip()->isBoosting() && forward)
[11071]126                    this->state_ = EngineState::Boost;
[6417]127                else if (forward && !this->state_) // this->state_ == Boost
[11071]128                    this->state_ = EngineState::Normal;
[6417]129                else if (direction.z > 0.0 && velocity.z < 0.0)
[11071]130                    this->state_ = EngineState::Brake;
[2254]131                else
[11071]132                    this->state_ = EngineState::Idle;
[6709]133
[11071]134                if (this->state_ == EngineState::Idle && this->getSpeedAdd() > 0)
135                    this->state_ = EngineState::Normal;
[6417]136            }
[2254]137
[6417]138            if (GameMode::isMaster())
139            {
140                int changes = this->state_ | this->oldState_;
[2254]141
[6417]142                float pitch = velocity.length();
[11071]143                if (this->state_ & EngineState::Normal)
[6417]144                    defEngineSndNormal_->setPitch(clamp(pitch/MAX_VELOCITY_NORMAL + 1, 0.5f, 2.0f));
[11071]145                if (this->state_ & EngineState::Boost)
[6417]146                    defEngineSndBoost_->setPitch(clamp(pitch/MAX_VELOCITY_BOOST + 1, 0.5f, 2.0f));
[2254]147
[11071]148                if (changes & EngineState::Idle)
[6417]149                {
[11071]150                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & EngineState::Idle);
[6417]151                    lua_setglobal(this->lua_->getInternalLuaState(), "idle");
152                }
[11071]153                if (changes & EngineState::Normal)
[6417]154                {
[11071]155                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & EngineState::Normal);
[6417]156                    lua_setglobal(this->lua_->getInternalLuaState(), "normal");
[11071]157                    if (this->state_ & EngineState::Normal)
[6417]158                        defEngineSndNormal_->play();
159                    else
160                        defEngineSndNormal_->stop();
161                }
[11071]162                if (changes & EngineState::Brake)
[6417]163                {
[11071]164                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & EngineState::Brake);
[6417]165                    lua_setglobal(this->lua_->getInternalLuaState(), "brake");
166                }
[11071]167                if (changes & EngineState::Boost)
[6417]168                {
[11071]169                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & EngineState::Boost);
[6417]170                    lua_setglobal(this->lua_->getInternalLuaState(), "boost");
[11071]171                    if (this->state_ & EngineState::Boost)
[6417]172                        defEngineSndBoost_->play();
173                    else
174                        defEngineSndBoost_->stop();
175                }
[2254]176
[6417]177                this->oldState_ = this->state_;
178
179                // Update all effect conditions
[11071]180                for (EffectContainer* container : this->effectContainers_)
181                    container->updateCondition();
[2254]182            }
183        }
184
[8727]185        Engine::run(dt);
[2254]186    }
187
188    void MultiStateEngine::addToSpaceShip(SpaceShip* ship)
189    {
190        Engine::addToSpaceShip(ship);
191
192        if (!ship)
193            return;
194
[6417]195        if( this->defEngineSndNormal_ )
196            this->getShip()->attach(defEngineSndNormal_);
197        if( this->defEngineSndBoost_ )
198            this->getShip()->attach(defEngineSndBoost_);
[2254]199
[11071]200        for (EffectContainer* container : this->effectContainers_)
201            for (WorldEntity* effect : container->getEffects())
202                this->getShip()->attach(effect);
[2254]203    }
204
[11071]205    void MultiStateEngine::addEffectContainer(EffectContainer* container)
[2254]206    {
[11071]207        if (container == nullptr)
[6417]208            return;
[11071]209        container->setLuaState(this->lua_, 'f' + multi_cast<std::string>(this->effectContainers_.size()));
210        this->effectContainers_.push_back(container);
[2254]211        if (this->getShip())
[6417]212        {
[11071]213            for (WorldEntity* effect : container->getEffects())
214                this->getShip()->attach(effect);
[6417]215        }
[2254]216    }
217
[6417]218    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
[2254]219    {
220        unsigned int i = 0;
[11071]221        for (EffectContainer* effectContainer : this->effectContainers_)
[2254]222        {
223            if (i == index)
[11071]224                return effectContainer;
225            i++;
[2254]226        }
[11071]227        return nullptr;
[2254]228    }
229
[6417]230    void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound)
[2254]231    {
[6417]232        if( defEngineSndNormal_ )
233            defEngineSndNormal_->setSource(engineSound);
234        else
235            assert(0); // This should never happen, because soundpointer is only available on master
[2254]236    }
237
[6417]238    const std::string& MultiStateEngine::getDefEngSndNormal()
[2254]239    {
[6417]240        if( defEngineSndNormal_ )
241            return defEngineSndNormal_->getSource();
242        else
243            assert(0);
244        return BLANKSTRING;
[2254]245    }
246
[6417]247    void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound)
[2254]248    {
[6417]249        if( defEngineSndBoost_ )
250            defEngineSndBoost_->setSource(engineSound);
251        else
252            assert(0);
[2254]253    }
[6417]254
255    const std::string& MultiStateEngine::getDefEngSndBoost()
256    {
257        if( this->defEngineSndBoost_ )
258            return defEngineSndBoost_->getSource();
259        else
260            assert(0);
261        return BLANKSTRING;
262    }
[2254]263}
Note: See TracBrowser for help on using the repository browser.