Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/items/MultiStateEngine.cc @ 2274

Last change on this file since 2274 was 2254, checked in by landauf, 16 years ago

Update your media repository and delete keybindings.ini if you want to use boost (space).

  • Added new class "Engine" to control the speed of a SpaceShip (Engine is an Item, MultiStateEngine is a specialized version of Engine)
  • Added FadingBillboard, a billboard with the ability to fade in and out smoothly when activated/deactivated.
  • Several changes in Backlight, it's now a child of FadingBillboard
  • Some changes concerning local control in ControllableEntity
  • Fixed a bug in WorldEntity caused by different destruction order of attached objects on server and client
  • Added a "MainState" to BaseObject. An object has several states (activity, visibility, …) and one of it can be defined as "MainState" in the XML file. Other objects can change this state without knowing which state it really is (used by MultiStateEngine).
  • Property svn:eol-style set to native
File size: 8.4 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
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "MultiStateEngine.h"
31
32#include "core/Core.h"
33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35#include "objects/worldentities/pawns/SpaceShip.h"
36
37namespace orxonox
38{
39    static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20;
40
41    static const unsigned char STATE_ACTIVE  = 1;
42    static const unsigned char STATE_FORWARD = 2;
43    static const unsigned char STATE_BOOST   = 4;
44    static const unsigned char STATE_BRAKE   = 8;
45
46    CreateFactory(MultiStateEngine);
47
48    MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator)
49    {
50        RegisterObject(MultiStateEngine);
51
52        this->state_ = 0;
53
54        this->registerVariables();
55    }
56
57    MultiStateEngine::~MultiStateEngine()
58    {
59        if (this->isInitialized() && !this->getShip())
60        {
61            // We have no ship, so the effects are not attached and won't be destroyed automatically
62            for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
63                delete (*it);
64            for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
65                delete (*it);
66            for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
67                delete (*it);
68            for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
69                delete (*it);
70        }
71    }
72
73    void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
76
77        XMLPortObject(MultiStateEngine, WorldEntity, "active",  addActiveEffect,  getActiveEffect,  xmlelement, mode);
78        XMLPortObject(MultiStateEngine, WorldEntity, "forward", addForwardEffect, getForwardEffect, xmlelement, mode);
79        XMLPortObject(MultiStateEngine, WorldEntity, "boost",   addBoostEffect,   getBoostEffect,   xmlelement, mode);
80        XMLPortObject(MultiStateEngine, WorldEntity, "brake",   addBrakeEffect,   getBrakeEffect,   xmlelement, mode);
81    }
82
83    void MultiStateEngine::registerVariables()
84    {
85        REGISTERDATA(this->state_, direction::toserver);
86    }
87
88    void MultiStateEngine::tick(float dt)
89    {
90        if (this->getShip())
91        {
92            if (this->getShip()->hasLocalController())
93            {
94                this->setObjectMode(direction::bidirectional);
95
96                const Vector3& direction = this->getDirection();
97                const Vector3& velocity = this->getShip()->getVelocity();
98
99                bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
100                bool boost = (this->getShip()->getBoost() && forward);
101                bool brake = (direction.z > 0 && velocity.z < 0);
102                bool active = (direction != Vector3::ZERO && !brake);
103
104                if (active)
105                    this->state_ |= STATE_ACTIVE;
106                else
107                    this->state_ &= ~STATE_ACTIVE;
108
109                if (forward)
110                    this->state_ |= STATE_FORWARD;
111                else
112                    this->state_ &= ~STATE_FORWARD;
113
114                if (boost)
115                    this->state_ |= STATE_BOOST;
116                else
117                    this->state_ &= ~STATE_BOOST;
118
119                if (brake)
120                    this->state_ |= STATE_BRAKE;
121                else
122                    this->state_ &= ~STATE_BRAKE;
123            }
124
125            if (this->getShip()->hasLocalController() || Core::isMaster())
126            {
127                for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
128                    (*it)->setMainState(this->state_ & STATE_ACTIVE);
129                for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
130                    (*it)->setMainState(this->state_ & STATE_FORWARD);
131                for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
132                    (*it)->setMainState(this->state_ & STATE_BOOST);
133                for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
134                    (*it)->setMainState(this->state_ & STATE_BRAKE);
135            }
136        }
137
138        SUPER(MultiStateEngine, tick, dt);
139    }
140
141    void MultiStateEngine::addToSpaceShip(SpaceShip* ship)
142    {
143        Engine::addToSpaceShip(ship);
144
145        if (!ship)
146            return;
147
148        for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
149            ship->attach(*it);
150        for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
151            ship->attach(*it);
152        for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
153            ship->attach(*it);
154        for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
155            ship->attach(*it);
156    }
157
158    void MultiStateEngine::addActiveEffect(WorldEntity* effect)
159    {
160        this->activeEffects_.push_back(effect);
161        if (this->getShip())
162            this->getShip()->attach(effect);
163    }
164
165    void MultiStateEngine::addForwardEffect(WorldEntity* effect)
166    {
167        this->forwardEffects_.push_back(effect);
168        if (this->getShip())
169            this->getShip()->attach(effect);
170    }
171
172    void MultiStateEngine::addBoostEffect(WorldEntity* effect)
173    {
174        this->boostEffects_.push_back(effect);
175        if (this->getShip())
176            this->getShip()->attach(effect);
177    }
178
179    void MultiStateEngine::addBrakeEffect(WorldEntity* effect)
180    {
181        this->brakeEffects_.push_back(effect);
182        if (this->getShip())
183            this->getShip()->attach(effect);
184    }
185
186    WorldEntity* MultiStateEngine::getActiveEffect(unsigned int index) const
187    {
188        unsigned int i = 0;
189        for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
190        {
191            if (i == index)
192                return (*it);
193            ++i;
194        }
195        return 0;
196    }
197
198    WorldEntity* MultiStateEngine::getForwardEffect(unsigned int index) const
199    {
200        unsigned int i = 0;
201        for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
202        {
203            if (i == index)
204                return (*it);
205            ++i;
206        }
207        return 0;
208    }
209
210    WorldEntity* MultiStateEngine::getBoostEffect(unsigned int index) const
211    {
212        unsigned int i = 0;
213        for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
214        {
215            if (i == index)
216                return (*it);
217            ++i;
218        }
219        return 0;
220    }
221
222    WorldEntity* MultiStateEngine::getBrakeEffect(unsigned int index) const
223    {
224        unsigned int i = 0;
225        for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
226        {
227            if (i == index)
228                return (*it);
229            ++i;
230        }
231        return 0;
232    }
233}
Note: See TracBrowser for help on using the repository browser.