Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10272 was 9939, checked in by jo, 11 years ago

presentationHS13 branch merged into trunk

  • Property svn:eol-style set to native
File size: 9.3 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        {
69            this->defEngineSndBoost_ = 0;
70            this->defEngineSndNormal_ = 0;
71            this->lua_ = 0;
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
87                for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
88                    for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
89                        (*it2)->destroy();
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)
[6417]126                    this->state_ = Boost;
127                else if (forward && !this->state_) // this->state_ == Boost
128                    this->state_ = Normal;
129                else if (direction.z > 0.0 && velocity.z < 0.0)
130                    this->state_ = Brake;
[2254]131                else
[6417]132                    this->state_ = Idle;
[6709]133
134                if (this->state_ == Idle && this->getSpeedAdd() > 0)
135                    this->state_ = 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();
143                if (this->state_ & Normal)
144                    defEngineSndNormal_->setPitch(clamp(pitch/MAX_VELOCITY_NORMAL + 1, 0.5f, 2.0f));
145                if (this->state_ & Boost)
146                    defEngineSndBoost_->setPitch(clamp(pitch/MAX_VELOCITY_BOOST + 1, 0.5f, 2.0f));
[2254]147
[6417]148                if (changes & Idle)
149                {
150                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Idle);
151                    lua_setglobal(this->lua_->getInternalLuaState(), "idle");
152                }
153                if (changes & Normal)
154                {
155                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Normal);
156                    lua_setglobal(this->lua_->getInternalLuaState(), "normal");
157                    if (this->state_ & Normal)
158                        defEngineSndNormal_->play();
159                    else
160                        defEngineSndNormal_->stop();
161                }
162                if (changes & Brake)
163                {
164                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Brake);
165                    lua_setglobal(this->lua_->getInternalLuaState(), "brake");
166                }
167                if (changes & Boost)
168                {
169                    lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Boost);
170                    lua_setglobal(this->lua_->getInternalLuaState(), "boost");
171                    if (this->state_ & Boost)
172                        defEngineSndBoost_->play();
173                    else
174                        defEngineSndBoost_->stop();
175                }
[2254]176
[6417]177                this->oldState_ = this->state_;
178
179                // Update all effect conditions
180                for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
181                    (*it)->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
[6417]200        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
201            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
202                this->getShip()->attach(*it2);
[2254]203    }
204
[6417]205    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
[2254]206    {
[6417]207        if (effect == NULL)
208            return;
209        effect->setLuaState(this->lua_, 'f' + multi_cast<std::string>(this->effectContainers_.size()));
210        this->effectContainers_.push_back(effect);
[2254]211        if (this->getShip())
[6417]212        {
213            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
214                this->getShip()->attach(*it);
215        }
[2254]216    }
217
[6417]218    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
[2254]219    {
220        unsigned int i = 0;
[6417]221        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
[2254]222        {
223            if (i == index)
224                return (*it);
225        }
[6417]226        return NULL;
[2254]227    }
228
[6417]229    void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound)
[2254]230    {
[6417]231        if( defEngineSndNormal_ )
232            defEngineSndNormal_->setSource(engineSound);
233        else
234            assert(0); // This should never happen, because soundpointer is only available on master
[2254]235    }
236
[6417]237    const std::string& MultiStateEngine::getDefEngSndNormal()
[2254]238    {
[6417]239        if( defEngineSndNormal_ )
240            return defEngineSndNormal_->getSource();
241        else
242            assert(0);
243        return BLANKSTRING;
[2254]244    }
245
[6417]246    void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound)
[2254]247    {
[6417]248        if( defEngineSndBoost_ )
249            defEngineSndBoost_->setSource(engineSound);
250        else
251            assert(0);
[2254]252    }
[6417]253
254    const std::string& MultiStateEngine::getDefEngSndBoost()
255    {
256        if( this->defEngineSndBoost_ )
257            return defEngineSndBoost_->getSource();
258        else
259            assert(0);
260        return BLANKSTRING;
261    }
[2254]262}
Note: See TracBrowser for help on using the repository browser.