Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6394 was 6394, checked in by rgrieder, 16 years ago

std::string tweaks:

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