Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/items/MultiStateEngine.cc @ 6313

Last change on this file since 6313 was 6313, checked in by scheusso, 16 years ago

further network fixes
changed screenshot format from jpeg to tiff (lossless)

  • Property svn:eol-style set to native
File size: 9.6 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
[6187]24 *      Reto Grieder
[2254]25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "MultiStateEngine.h"
31
[6187]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"
[6187]39#include "core/LuaState.h"
[2254]40#include "core/XMLPort.h"
[6187]41#include "worldentities/EffectContainer.h"
[5735]42#include "worldentities/pawns/SpaceShip.h"
[6202]43#include "sound/WorldSound.h"
[2254]44
45namespace orxonox
46{
[6207]47    static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 0;
[6202]48    static const float MAX_VELOCITY_NORMAL = 111;
49    static const float MAX_VELOCITY_BOOST = 221;
[2254]50
51    CreateFactory(MultiStateEngine);
52
53    MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator)
54    {
55        RegisterObject(MultiStateEngine);
56
[6313]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        }
64        else
65        {
66            this->defEngineSndBoost_ = 0;
67            this->defEngineSndNormal_ = 0;
68        }
[6202]69
[6187]70        this->lua_ = new LuaState();
[2254]71        this->state_ = 0;
72
73        this->registerVariables();
74    }
75
76    MultiStateEngine::~MultiStateEngine()
77    {
78        if (this->isInitialized() && !this->getShip())
79        {
80            // We have no ship, so the effects are not attached and won't be destroyed automatically
[6187]81            for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
82                for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
83                    (*it2)->destroy();
[6313]84            if( this->defEngineSndNormal_ )
85                delete this->defEngineSndNormal_;
86            if( this->defEngineSndBoost_  )
87                delete this->defEngineSndBoost_;
[6187]88            delete this->lua_;
[2254]89        }
90    }
91
92    void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
93    {
94        SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
[6187]95        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
[6202]96        XMLPortParam(MultiStateEngine, "defEngineSndNormal",  setDefEngSndNormal,  getDefEngSndNormal,  xmlelement, mode);
[6207]97        XMLPortParam(MultiStateEngine, "defEngineSndBoost",  setDefEngSndBoost,  getDefEngSndBoost,  xmlelement, mode);
[2254]98    }
99
100    void MultiStateEngine::registerVariables()
101    {
[3280]102        registerVariable(this->state_, VariableDirection::ToServer);
[2254]103    }
104
105    void MultiStateEngine::tick(float dt)
106    {
107        if (this->getShip())
108        {
[6313]109//             if (this->getShip()->hasLocalController())
110            if (GameMode::isMaster() && this->getShip()->hasLocalController())
[2254]111            {
[5929]112                this->setSyncMode(ObjectDirection::Bidirectional);
[2254]113
114                const Vector3& direction = this->getDirection();
[2488]115                const Vector3& velocity = this->getShip()->getLocalVelocity();
[2254]116
[6202]117                float pitch = velocity.length();
[2254]118                bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
119
[6187]120                int newState = 0;
121                if (this->getShip()->getBoost() && forward)
[6202]122                {
[6187]123                    newState = Boost;
[6207]124                    pitch = pitch/MAX_VELOCITY_BOOST + 1;
[6202]125                    pitch = pitch > 2 ? 2 : pitch;
126                    pitch = pitch < 0.5 ? 0.5 : pitch;
[6207]127                    defEngineSndBoost_->setPitch(pitch);
[6202]128                }
[6187]129                else if (forward && !newState) // newState == Boost
[6202]130                {
[6187]131                    newState = Normal;
[6202]132                    pitch = pitch/MAX_VELOCITY_NORMAL + 1;
133                    pitch = pitch > 2 ? 2 : pitch;
134                    pitch = pitch < 0.5 ? 0.5 : pitch;
135                    defEngineSndNormal_->setPitch(pitch);
136                }
[6187]137                else if (direction.z > 0 && velocity.z < 0)
138                    newState = Brake;
[2254]139                else
[6187]140                    newState = Idle;
[2254]141
[6187]142                if (newState != this->state_)
143                {
144                    int changes = newState | this->state_;
145                    if (changes & Idle)
146                    {
147                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle);
148                        lua_setglobal(this->lua_->getInternalLuaState(), "idle");
149                    }
150                    if (changes & Normal)
151                    {
152                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal);
153                        lua_setglobal(this->lua_->getInternalLuaState(), "normal");
[6202]154                        if(newState & Normal)
155                        {
156                            defEngineSndNormal_->play();
157                        }
158                        else
159                        {
160                            defEngineSndNormal_->stop();
161                        }
[6187]162                    }
163                    if (changes & Brake)
164                    {
165                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake);
166                        lua_setglobal(this->lua_->getInternalLuaState(), "brake");
167                    }
168                    if (changes & Boost)
169                    {
170                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost);
171                        lua_setglobal(this->lua_->getInternalLuaState(), "boost");
[6207]172                        if(newState & Boost)
173                        {
174                            defEngineSndBoost_->play();
175                        }
176                        else
177                        {
178                            defEngineSndBoost_->stop();
179                        }
[6187]180                    }
[2254]181
[6187]182                    // Update all effect conditions
183                    for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
184                        (*it)->updateCondition();
[2254]185
[6187]186                    this->state_ = newState;
187                }
[2254]188            }
189
[2896]190            if (GameMode::isMaster())
[2254]191            {
192            }
193        }
194
[2809]195        SUPER(MultiStateEngine, tick, dt);
[2254]196    }
197
198    void MultiStateEngine::addToSpaceShip(SpaceShip* ship)
199    {
200        Engine::addToSpaceShip(ship);
201
202        if (!ship)
203            return;
204
[6313]205        if( this->defEngineSndNormal_ )
206            this->getShip()->attach(defEngineSndNormal_);
207        if( this->defEngineSndBoost_ )
208            this->getShip()->attach(defEngineSndBoost_);
[6202]209
[6187]210        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
211            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
212                this->getShip()->attach(*it2);
[2254]213    }
214
[6187]215    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
[2254]216    {
[6187]217        if (effect == NULL)
218            return;
219        effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size()));
220        this->effectContainers_.push_back(effect);
[2254]221        if (this->getShip())
222        {
[6187]223            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
224                this->getShip()->attach(*it);
[2254]225        }
226    }
227
[6187]228    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
[2254]229    {
230        unsigned int i = 0;
[6187]231        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
[2254]232        {
233            if (i == index)
234                return (*it);
235        }
[6187]236        return NULL;
[2254]237    }
[6202]238
239    void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound)
240    {
[6313]241        if( defEngineSndNormal_ )
242            defEngineSndNormal_->setSource(engineSound);
243        else
244            assert(0); // This should never happen, because soundpointer is only available on master
[6202]245    }
246
247    const std::string& MultiStateEngine::getDefEngSndNormal()
248    {
[6313]249        if( defEngineSndNormal_ )
250            return defEngineSndNormal_->getSource();
251        else
252            assert(0);
253        return std::string();
[6202]254    }
[6207]255
256    void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound)
257    {
[6313]258        if( defEngineSndBoost_ )
259            defEngineSndBoost_->setSource(engineSound);
260        else
261            assert(0);
[6207]262    }
263
264    const std::string& MultiStateEngine::getDefEngSndBoost()
265    {
[6313]266        if( this->defEngineSndBoost_ )
267            return defEngineSndBoost_->getSource();
268        else
269            assert(0);
270        return std::string();
[6207]271    }
[2254]272}
Note: See TracBrowser for help on using the repository browser.