Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6196 was 6187, checked in by rgrieder, 15 years ago

Added better engine effects: you can still add any WorldEntity to the MultiStateEngine, but you have to enclose it in an EffectContainer that has one XMLParameter called "condition". There you can write conditions containing the following words: and, or, not, idle, normal, boost, brake. The last four words are mutually exclusive states.
For an example see spaceship_assff.oxt

  • Property svn:eol-style set to native
File size: 6.3 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
44namespace orxonox
45{
46    static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20;
47
48    CreateFactory(MultiStateEngine);
49
50    MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator)
51    {
52        RegisterObject(MultiStateEngine);
53
54        this->lua_ = new LuaState();
55        this->state_ = 0;
56
57        this->registerVariables();
58    }
59
60    MultiStateEngine::~MultiStateEngine()
61    {
62        if (this->isInitialized() && !this->getShip())
63        {
64            // We have no ship, so the effects are not attached and won't be destroyed automatically
65            for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
66                for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
67                    (*it2)->destroy();
68            delete this->lua_;
69        }
70    }
71
72    void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
73    {
74        SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
75        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
76    }
77
78    void MultiStateEngine::registerVariables()
79    {
80        registerVariable(this->state_, VariableDirection::ToServer);
81    }
82
83    void MultiStateEngine::tick(float dt)
84    {
85        if (this->getShip())
86        {
87            if (this->getShip()->hasLocalController())
88            {
89                this->setSyncMode(ObjectDirection::Bidirectional);
90
91                const Vector3& direction = this->getDirection();
92                const Vector3& velocity = this->getShip()->getLocalVelocity();
93
94                bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
95
96                int newState = 0;
97                if (this->getShip()->getBoost() && forward)
98                    newState = Boost;
99                else if (forward && !newState) // newState == Boost
100                    newState = Normal;
101                else if (direction.z > 0 && velocity.z < 0)
102                    newState = Brake;
103                else
104                    newState = Idle;
105
106                if (newState != this->state_)
107                {
108                    int changes = newState | this->state_;
109                    if (changes & Idle)
110                    {
111                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle);
112                        lua_setglobal(this->lua_->getInternalLuaState(), "idle");
113                    }
114                    if (changes & Normal)
115                    {
116                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal);
117                        lua_setglobal(this->lua_->getInternalLuaState(), "normal");
118                    }
119                    if (changes & Brake)
120                    {
121                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake);
122                        lua_setglobal(this->lua_->getInternalLuaState(), "brake");
123                    }
124                    if (changes & Boost)
125                    {
126                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost);
127                        lua_setglobal(this->lua_->getInternalLuaState(), "boost");
128                    }
129
130                    // Update all effect conditions
131                    for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
132                        (*it)->updateCondition();
133
134                    this->state_ = newState;
135                }
136            }
137
138            if (GameMode::isMaster())
139            {
140            }
141        }
142
143        SUPER(MultiStateEngine, tick, dt);
144    }
145
146    void MultiStateEngine::addToSpaceShip(SpaceShip* ship)
147    {
148        Engine::addToSpaceShip(ship);
149
150        if (!ship)
151            return;
152
153        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
154            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
155                this->getShip()->attach(*it2);
156    }
157
158    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
159    {
160        if (effect == NULL)
161            return;
162        effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size()));
163        this->effectContainers_.push_back(effect);
164        if (this->getShip())
165        {
166            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
167                this->getShip()->attach(*it);
168        }
169    }
170
171    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
172    {
173        unsigned int i = 0;
174        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
175        {
176            if (i == index)
177                return (*it);
178        }
179        return NULL;
180    }
181}
Note: See TracBrowser for help on using the repository browser.