Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup3/src/orxonox/items/Engine.cc @ 6514

Last change on this file since 6514 was 6467, checked in by dafrick, 15 years ago

Steering works now. The lesson is: Don't comment out stuff you don't understand. ;)

  • Property svn:eol-style set to native
File size: 8.7 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 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Engine.h"
30
31#include "util/Math.h"
32#include "core/CoreIncludes.h"
33#include "core/ConfigValueIncludes.h"
34#include "core/XMLPort.h"
35#include "Scene.h"
36#include "worldentities/pawns/SpaceShip.h"
37//TODO: Remove.
38//#include "pickup/ModifierType.h"
39#include "tools/Shader.h"
40
41namespace orxonox
42{
43    CreateFactory(Engine);
44
45    Engine::Engine(BaseObject* creator) : Item(creator)
46    {
47        RegisterObject(Engine);
48
49        this->ship_ = 0;
50        this->shipID_ = OBJECTID_UNKNOWN;
51
52        this->boostFactor_ = 1.5;
53        this->speedFactor_ = 1.0;
54
55        this->maxSpeedFront_ = 0.0;
56        this->maxSpeedBack_ = 0.0;
57        this->maxSpeedLeftRight_ = 0.0;
58        this->maxSpeedUpDown_ = 0.0;
59
60        this->accelerationFront_ = 0.0;
61        this->accelerationBrake_ = 0.0;
62        this->accelerationBack_ = 0.0;
63        this->accelerationLeftRight_ = 0.0;
64        this->accelerationUpDown_ = 0.0;
65
66        this->boostBlur_ = 0;
67
68        this->setConfigValues();
69        this->registerVariables();
70    }
71
72    Engine::~Engine()
73    {
74        if (this->isInitialized() && this->ship_)
75        {
76            this->ship_->setEngine(0);
77
78            if (this->boostBlur_)
79                this->boostBlur_->destroy();
80        }
81    }
82
83    void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
84    {
85        SUPER(Engine, XMLPort, xmlelement, mode);
86
87        XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode);
88
89        XMLPortParam(Engine, "speedfront",     setMaxSpeedFront,     setMaxSpeedFront,     xmlelement, mode);
90        XMLPortParam(Engine, "speedback",      setMaxSpeedBack,      setMaxSpeedBack,      xmlelement, mode);
91        XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode);
92        XMLPortParam(Engine, "speedupdown",    setMaxSpeedUpDown,    setMaxSpeedUpDown,    xmlelement, mode);
93
94        XMLPortParam(Engine, "accelerationfront",     setAccelerationFront,     setAccelerationFront,     xmlelement, mode);
95        XMLPortParam(Engine, "accelerationbrake",     setAccelerationBrake,     setAccelerationBrake,     xmlelement, mode);
96        XMLPortParam(Engine, "accelerationback",      setAccelerationBack,      setAccelerationBack,      xmlelement, mode);
97        XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode);
98        XMLPortParam(Engine, "accelerationupdown",    setAccelerationUpDown,    setAccelerationUpDown,    xmlelement, mode);
99    }
100
101    void Engine::setConfigValues()
102    {
103        SetConfigValue(blurStrength_, 3.0f);
104    }
105
106    void Engine::registerVariables()
107    {
108        registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
109
110        registerVariable(this->speedFactor_, VariableDirection::ToClient);
111        registerVariable(this->boostFactor_, VariableDirection::ToClient);
112
113        registerVariable(this->maxSpeedFront_,     VariableDirection::ToClient);
114        registerVariable(this->maxSpeedBack_,      VariableDirection::ToClient);
115        registerVariable(this->maxSpeedLeftRight_, VariableDirection::ToClient);
116        registerVariable(this->maxSpeedUpDown_,    VariableDirection::ToClient);
117
118        registerVariable(this->accelerationFront_,     VariableDirection::ToClient);
119        registerVariable(this->accelerationBrake_,     VariableDirection::ToClient);
120        registerVariable(this->accelerationBack_,      VariableDirection::ToClient);
121        registerVariable(this->accelerationLeftRight_, VariableDirection::ToClient);
122        registerVariable(this->accelerationUpDown_,    VariableDirection::ToClient);
123    }
124
125    void Engine::networkcallback_shipID()
126    {
127        this->ship_ = 0;
128
129        if (this->shipID_ != OBJECTID_UNKNOWN)
130        {
131            Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_);
132            if (object)
133                this->addToSpaceShip(orxonox_cast<SpaceShip*>(object));
134        }
135    }
136
137    void Engine::tick(float dt)
138    {
139        if (!this->ship_)
140        {
141            if (this->shipID_)
142            {
143                this->networkcallback_shipID();
144
145                if (!this->ship_)
146                    return;
147            }
148            else
149                return;
150        }
151
152        if (!this->isActive())
153            return;
154
155        SUPER(Engine, tick, dt);
156
157        const Vector3& direction = this->getDirection();
158        Vector3 velocity = this->ship_->getLocalVelocity();
159        Vector3 acceleration = Vector3::ZERO;
160
161        float factor = 1.0f / this->speedFactor_;
162        velocity *= factor;
163
164        if (direction.z < 0)
165        {
166            if (this->maxSpeedFront_ != 0)
167            {
168                float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f);
169                acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f);
170            }
171        }
172        else if (direction.z > 0)
173        {
174            if (velocity.z < 0)
175                acceleration.z = direction.z * this->accelerationBrake_;
176            else if (this->maxSpeedBack_ != 0)
177                acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);
178        }
179
180        if (this->maxSpeedLeftRight_ != 0)
181        {
182            if (direction.x < 0)
183                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
184            else if (direction.x > 0)
185                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
186        }
187
188        if (this->maxSpeedUpDown_ != 0)
189        {
190            if (direction.y < 0)
191                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
192            else if (direction.y > 0)
193                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
194        }
195
196        //TODO: Correct?
197        this->ship_->setAcceleration(this->ship_->getOrientation() * acceleration);
198
199        if (!this->ship_->getPermanentBoost())
200            this->ship_->setBoost(false);
201        this->ship_->setSteeringDirection(Vector3::ZERO);
202
203        if (!this->boostBlur_ && this->ship_->hasLocalController() && this->ship_->hasHumanController())
204        {
205            this->boostBlur_ = new Shader(this->ship_->getScene()->getSceneManager());
206            this->boostBlur_->setCompositor("Radial Blur");
207        }
208
209        if (this->boostBlur_ && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1)
210            this->boostBlur_->setParameter("Ogre/Compositor/Radial_Blur", 0, 0, "sampleStrength", this->blurStrength_ * clamp((-velocity.z - this->maxSpeedFront_) / ((this->boostFactor_ - 1) * this->maxSpeedFront_), 0.0f, 1.0f));
211    }
212
213    void Engine::changedActivity()
214    {
215        SUPER(Engine, changedActivity);
216
217        if (this->boostBlur_)
218            this->boostBlur_->setVisible(this->isVisible());
219    }
220
221    void Engine::addToSpaceShip(SpaceShip* ship)
222    {
223        this->ship_ = ship;
224
225        if (ship)
226        {
227            this->shipID_ = ship->getObjectID();
228            if (ship->getEngine() != this)
229                ship->setEngine(this);
230
231            if (this->boostBlur_)
232            {
233                this->boostBlur_->destroy();
234                this->boostBlur_ = 0;
235            }
236        }
237    }
238
239    const Vector3& Engine::getDirection() const
240    {
241        if (this->ship_)
242            return this->ship_->getSteeringDirection();
243        else
244            return Vector3::ZERO;
245    }
246}
Note: See TracBrowser for help on using the repository browser.