Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/items/Engine.cc @ 6251

Last change on this file since 6251 was 5929, checked in by rgrieder, 15 years ago

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

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