Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6276 was 6222, checked in by youngk, 16 years ago

Cleaned up the code a little

  • Property svn:eol-style set to native
File size: 8.7 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
[6222]57        this->defEngineSndNormal_ = new WorldSound(this);
58        this->defEngineSndBoost_ = new WorldSound(this);
59        this->defEngineSndNormal_->setLooping(true);
60        this->defEngineSndBoost_->setLooping(true);
[6202]61
[6187]62        this->lua_ = new LuaState();
[2254]63        this->state_ = 0;
64
65        this->registerVariables();
66    }
67
68    MultiStateEngine::~MultiStateEngine()
69    {
70        if (this->isInitialized() && !this->getShip())
71        {
72            // We have no ship, so the effects are not attached and won't be destroyed automatically
[6187]73            for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
74                for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
75                    (*it2)->destroy();
[6202]76            delete this->defEngineSndNormal_;
[6207]77            delete this->defEngineSndBoost_;
[6187]78            delete this->lua_;
[2254]79        }
80    }
81
82    void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
83    {
84        SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
[6187]85        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
[6202]86        XMLPortParam(MultiStateEngine, "defEngineSndNormal",  setDefEngSndNormal,  getDefEngSndNormal,  xmlelement, mode);
[6207]87        XMLPortParam(MultiStateEngine, "defEngineSndBoost",  setDefEngSndBoost,  getDefEngSndBoost,  xmlelement, mode);
[2254]88    }
89
90    void MultiStateEngine::registerVariables()
91    {
[3280]92        registerVariable(this->state_, VariableDirection::ToServer);
[2254]93    }
94
95    void MultiStateEngine::tick(float dt)
96    {
97        if (this->getShip())
98        {
99            if (this->getShip()->hasLocalController())
100            {
[5929]101                this->setSyncMode(ObjectDirection::Bidirectional);
[2254]102
103                const Vector3& direction = this->getDirection();
[2488]104                const Vector3& velocity = this->getShip()->getLocalVelocity();
[2254]105
[6202]106                float pitch = velocity.length();
[2254]107                bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
108
[6187]109                int newState = 0;
110                if (this->getShip()->getBoost() && forward)
[6202]111                {
[6187]112                    newState = Boost;
[6207]113                    pitch = pitch/MAX_VELOCITY_BOOST + 1;
[6202]114                    pitch = pitch > 2 ? 2 : pitch;
115                    pitch = pitch < 0.5 ? 0.5 : pitch;
[6207]116                    defEngineSndBoost_->setPitch(pitch);
[6202]117                }
[6187]118                else if (forward && !newState) // newState == Boost
[6202]119                {
[6187]120                    newState = Normal;
[6202]121                    pitch = pitch/MAX_VELOCITY_NORMAL + 1;
122                    pitch = pitch > 2 ? 2 : pitch;
123                    pitch = pitch < 0.5 ? 0.5 : pitch;
124                    defEngineSndNormal_->setPitch(pitch);
125                }
[6187]126                else if (direction.z > 0 && velocity.z < 0)
127                    newState = Brake;
[2254]128                else
[6187]129                    newState = Idle;
[2254]130
[6187]131                if (newState != this->state_)
132                {
133                    int changes = newState | this->state_;
134                    if (changes & Idle)
135                    {
136                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle);
137                        lua_setglobal(this->lua_->getInternalLuaState(), "idle");
138                    }
139                    if (changes & Normal)
140                    {
141                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal);
142                        lua_setglobal(this->lua_->getInternalLuaState(), "normal");
[6202]143                        if(newState & Normal)
144                        {
145                            defEngineSndNormal_->play();
146                        }
147                        else
148                        {
149                            defEngineSndNormal_->stop();
150                        }
[6187]151                    }
152                    if (changes & Brake)
153                    {
154                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake);
155                        lua_setglobal(this->lua_->getInternalLuaState(), "brake");
156                    }
157                    if (changes & Boost)
158                    {
159                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost);
160                        lua_setglobal(this->lua_->getInternalLuaState(), "boost");
[6207]161                        if(newState & Boost)
162                        {
163                            defEngineSndBoost_->play();
164                        }
165                        else
166                        {
167                            defEngineSndBoost_->stop();
168                        }
[6187]169                    }
[2254]170
[6187]171                    // Update all effect conditions
172                    for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
173                        (*it)->updateCondition();
[2254]174
[6187]175                    this->state_ = newState;
176                }
[2254]177            }
178
[2896]179            if (GameMode::isMaster())
[2254]180            {
181            }
182        }
183
[2809]184        SUPER(MultiStateEngine, tick, dt);
[2254]185    }
186
187    void MultiStateEngine::addToSpaceShip(SpaceShip* ship)
188    {
189        Engine::addToSpaceShip(ship);
190
191        if (!ship)
192            return;
193
[6202]194        this->getShip()->attach(defEngineSndNormal_);
[6207]195        this->getShip()->attach(defEngineSndBoost_);
[6202]196
[6187]197        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
198            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
199                this->getShip()->attach(*it2);
[2254]200    }
201
[6187]202    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
[2254]203    {
[6187]204        if (effect == NULL)
205            return;
206        effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size()));
207        this->effectContainers_.push_back(effect);
[2254]208        if (this->getShip())
209        {
[6187]210            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
211                this->getShip()->attach(*it);
[2254]212        }
213    }
214
[6187]215    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
[2254]216    {
217        unsigned int i = 0;
[6187]218        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
[2254]219        {
220            if (i == index)
221                return (*it);
222        }
[6187]223        return NULL;
[2254]224    }
[6202]225
226    void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound)
227    {
228        defEngineSndNormal_->setSource(engineSound);
229    }
230
231    const std::string& MultiStateEngine::getDefEngSndNormal()
232    {
233        return defEngineSndNormal_->getSource();
234    }
[6207]235
236    void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound)
237    {
238        defEngineSndBoost_->setSource(engineSound);
239    }
240
241    const std::string& MultiStateEngine::getDefEngSndBoost()
242    {
243        return defEngineSndBoost_->getSource();
244    }
[2254]245}
Note: See TracBrowser for help on using the repository browser.