Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6202 was 6202, checked in by youngk, 14 years ago

Implemented speed dependent audio pitch. BUG pitching doesn't currently work.

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
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
24 *      Reto Grieder
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "MultiStateEngine.h"
31
32extern "C" {
33#include <lua.h>
34}
35
36#include "util/Convert.h"
37#include "core/CoreIncludes.h"
38#include "core/GameMode.h"
39#include "core/LuaState.h"
40#include "core/XMLPort.h"
41#include "worldentities/EffectContainer.h"
42#include "worldentities/pawns/SpaceShip.h"
43#include "sound/WorldSound.h"
44
45namespace orxonox
46{
47    static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20;
48    static const float MAX_VELOCITY_NORMAL = 111;
49    static const float MAX_VELOCITY_BOOST = 221;
50
51    CreateFactory(MultiStateEngine);
52
53    MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator)
54    {
55        RegisterObject(MultiStateEngine);
56
57        defEngineSndNormal_ = new WorldSound(this);
58        defEngineSndNormal_->setLooping(true);
59
60        this->lua_ = new LuaState();
61        this->state_ = 0;
62
63        this->registerVariables();
64    }
65
66    MultiStateEngine::~MultiStateEngine()
67    {
68        if (this->isInitialized() && !this->getShip())
69        {
70            // We have no ship, so the effects are not attached and won't be destroyed automatically
71            for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
72                for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
73                    (*it2)->destroy();
74            delete this->defEngineSndNormal_;
75            delete this->lua_;
76        }
77    }
78
79    void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
80    {
81        SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
82        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
83        XMLPortParam(MultiStateEngine, "defEngineSndNormal",  setDefEngSndNormal,  getDefEngSndNormal,  xmlelement, mode);
84    }
85
86    void MultiStateEngine::registerVariables()
87    {
88        registerVariable(this->state_, VariableDirection::ToServer);
89    }
90
91    void MultiStateEngine::tick(float dt)
92    {
93        if (this->getShip())
94        {
95            if (this->getShip()->hasLocalController())
96            {
97                this->setSyncMode(ObjectDirection::Bidirectional);
98
99                const Vector3& direction = this->getDirection();
100                const Vector3& velocity = this->getShip()->getLocalVelocity();
101
102                float pitch = velocity.length();
103                bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
104
105                int newState = 0;
106                if (this->getShip()->getBoost() && forward)
107                {
108                    newState = Boost;
109                    pitch = pitch/MAX_VELOCITY_BOOST + 1;
110                    pitch = pitch > 2 ? 2 : pitch;
111                    pitch = pitch < 0.5 ? 0.5 : pitch;
112                    defEngineSndNormal_->setPitch(pitch);
113                }
114                else if (forward && !newState) // newState == Boost
115                {
116                    newState = Normal;
117                    pitch = pitch/MAX_VELOCITY_NORMAL + 1;
118                    pitch = pitch > 2 ? 2 : pitch;
119                    pitch = pitch < 0.5 ? 0.5 : pitch;
120                    defEngineSndNormal_->setPitch(pitch);
121                }
122                else if (direction.z > 0 && velocity.z < 0)
123                    newState = Brake;
124                else
125                    newState = Idle;
126
127                if (newState != this->state_)
128                {
129                    int changes = newState | this->state_;
130                    if (changes & Idle)
131                    {
132                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle);
133                        lua_setglobal(this->lua_->getInternalLuaState(), "idle");
134                    }
135                    if (changes & Normal)
136                    {
137                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal);
138                        lua_setglobal(this->lua_->getInternalLuaState(), "normal");
139                        if(newState & Normal)
140                        {
141                            defEngineSndNormal_->play();
142                        }
143                        else
144                        {
145                            defEngineSndNormal_->stop();
146                        }
147                    }
148                    if (changes & Brake)
149                    {
150                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake);
151                        lua_setglobal(this->lua_->getInternalLuaState(), "brake");
152                    }
153                    if (changes & Boost)
154                    {
155                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost);
156                        lua_setglobal(this->lua_->getInternalLuaState(), "boost");
157                    }
158
159                    // Update all effect conditions
160                    for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
161                        (*it)->updateCondition();
162
163                    this->state_ = newState;
164                }
165            }
166
167            if (GameMode::isMaster())
168            {
169            }
170        }
171
172        SUPER(MultiStateEngine, tick, dt);
173    }
174
175    void MultiStateEngine::addToSpaceShip(SpaceShip* ship)
176    {
177        Engine::addToSpaceShip(ship);
178
179        if (!ship)
180            return;
181
182        this->getShip()->attach(defEngineSndNormal_);
183
184        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
185            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
186                this->getShip()->attach(*it2);
187    }
188
189    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
190    {
191        if (effect == NULL)
192            return;
193        effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size()));
194        this->effectContainers_.push_back(effect);
195        if (this->getShip())
196        {
197            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
198                this->getShip()->attach(*it);
199        }
200    }
201
202    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
203    {
204        unsigned int i = 0;
205        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
206        {
207            if (i == index)
208                return (*it);
209        }
210        return NULL;
211    }
212
213    void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound)
214    {
215        defEngineSndNormal_->setSource(engineSound);
216    }
217
218    const std::string& MultiStateEngine::getDefEngSndNormal()
219    {
220        return defEngineSndNormal_->getSource();
221    }
222}
Note: See TracBrowser for help on using the repository browser.