Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/items/Engine.cc @ 2350

Last change on this file since 2350 was 2350, checked in by landauf, 16 years ago
  • Added support for postprocessing shaders (bloom, motion blur, …). Shaders are only visible if Plugin_CgProgramManager is included in bin/Plugins.cfg.
  • Added class GlobalShader which is visible on all clients and can be activated or deactivated by triggers or other events.
  • Added RadialBlur shader to the SpaceShip's engine. The strength of the RadialBlur depends on the SpaceShip's velocity.
  • Property svn:eol-style set to native
File size: 8.2 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 "Engine.h"
31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
[2350]34#include "objects/Scene.h"
[2254]35#include "objects/worldentities/pawns/SpaceShip.h"
[2350]36#include "tools/Shader.h"
[2254]37
38namespace orxonox
39{
40    CreateFactory(Engine);
41
42    Engine::Engine(BaseObject* creator) : Item(creator)
43    {
44        RegisterObject(Engine);
45
46        this->ship_ = 0;
47        this->shipID_ = OBJECTID_UNKNOWN;
48
49        this->boostFactor_ = 1.5;
50        this->speedFactor_ = 1.0;
51
52        this->maxSpeedFront_ = 0.0;
53        this->maxSpeedBack_ = 0.0;
54        this->maxSpeedLeftRight_ = 0.0;
55        this->maxSpeedUpDown_ = 0.0;
56
57        this->accelerationFront_ = 0.0;
58        this->accelerationBrake_ = 0.0;
59        this->accelerationBack_ = 0.0;
60        this->accelerationLeftRight_ = 0.0;
61        this->accelerationUpDown_ = 0.0;
62
[2350]63        this->boostBlur_ = 0;
64
[2254]65        this->registerVariables();
66    }
67
68    Engine::~Engine()
69    {
[2256]70        if (this->isInitialized() && this->ship_)
[2350]71        {
[2256]72            this->ship_->setEngine(0);
[2350]73
74            if (this->boostBlur_)
75                delete this->boostBlur_;
76        }
[2254]77    }
78
79    void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
80    {
81        SUPER(Engine, XMLPort, xmlelement, mode);
82
83        XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode);
84
85        XMLPortParam(Engine, "speedfront",     setMaxSpeedFront,     setMaxSpeedFront,     xmlelement, mode);
86        XMLPortParam(Engine, "speedback",      setMaxSpeedBack,      setMaxSpeedBack,      xmlelement, mode);
87        XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode);
88        XMLPortParam(Engine, "speedupdown",    setMaxSpeedUpDown,    setMaxSpeedUpDown,    xmlelement, mode);
89
90        XMLPortParam(Engine, "accelerationfront",     setAccelerationFront,     setAccelerationFront,     xmlelement, mode);
91        XMLPortParam(Engine, "accelerationbrake",     setAccelerationBrake,     setAccelerationBrake,     xmlelement, mode);
92        XMLPortParam(Engine, "accelerationback",      setAccelerationBack,      setAccelerationBack,      xmlelement, mode);
93        XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode);
94        XMLPortParam(Engine, "accelerationupdown",    setAccelerationUpDown,    setAccelerationUpDown,    xmlelement, mode);
95    }
96
97    void Engine::registerVariables()
98    {
99        REGISTERDATA(this->shipID_, direction::toclient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
100
101        REGISTERDATA(this->speedFactor_, direction::toclient);
102        REGISTERDATA(this->boostFactor_, direction::toclient);
103
104        REGISTERDATA(this->maxSpeedFront_,     direction::toclient);
105        REGISTERDATA(this->maxSpeedBack_,      direction::toclient);
106        REGISTERDATA(this->maxSpeedLeftRight_, direction::toclient);
107        REGISTERDATA(this->maxSpeedUpDown_,    direction::toclient);
108
109        REGISTERDATA(this->accelerationFront_,     direction::toclient);
110        REGISTERDATA(this->accelerationBrake_,     direction::toclient);
111        REGISTERDATA(this->accelerationBack_,      direction::toclient);
112        REGISTERDATA(this->accelerationLeftRight_, direction::toclient);
113        REGISTERDATA(this->accelerationUpDown_,    direction::toclient);
114    }
115
116    void Engine::networkcallback_shipID()
117    {
118        this->ship_ = 0;
119
120        if (this->shipID_ != OBJECTID_UNKNOWN)
121        {
122            Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_);
123            if (object)
[2256]124                this->addToSpaceShip(dynamic_cast<SpaceShip*>(object));
[2254]125        }
126    }
127
128    void Engine::tick(float dt)
129    {
130        if (!this->ship_)
131        {
132            if (this->shipID_)
133            {
134                this->networkcallback_shipID();
135
136                if (!this->ship_)
137                    return;
138            }
139            else
140                return;
141        }
142
143        if (!this->isActive())
144            return;
145
146        const Vector3& direction = this->getDirection();
147        Vector3 velocity = this->ship_->getVelocity();
148        Vector3 acceleration = Vector3::ZERO;
149
150        float factor = 1.0f / this->speedFactor_;
151        velocity *= factor;
152
153        if (direction.z < 0)
154        {
155            if (this->maxSpeedFront_ != 0)
156            {
157                float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f);
158                acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f);
159            }
160        }
161        else if (direction.z > 0)
162        {
163            if (velocity.z < 0)
164                acceleration.z = direction.z * this->accelerationBrake_;
165            else if (this->maxSpeedBack_ != 0)
166                acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);
167        }
168
169        if (this->maxSpeedLeftRight_ != 0)
170        {
171            if (direction.x < 0)
172                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
173            else if (direction.x > 0)
174                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
175        }
176
177        if (this->maxSpeedUpDown_ != 0)
178        {
179            if (direction.y < 0)
180                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
181            else if (direction.y > 0)
182                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
183        }
184
185        this->ship_->setAcceleration(acceleration);
186
187        this->ship_->setBoost(false);
188        this->ship_->setSteeringDirection(Vector3::ZERO);
[2350]189
190        if (!this->boostBlur_ && this->ship_->hasLocalController() && this->ship_->hasHumanController())
191        {
192            this->boostBlur_ = new Shader(this->ship_->getScene()->getSceneManager());
193            this->boostBlur_->setCompositor("Radial Blur");
194        }
195
196        if (this->boostBlur_ && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1)
197            this->boostBlur_->setParameter("Ogre/Compositor/Radial_Blur", 0, 0, "sampleStrength", 5.0f * clamp((-velocity.z - this->maxSpeedFront_) / ((this->boostFactor_ - 1) * this->maxSpeedFront_), 0.0f, 1.0f));
[2254]198    }
199
[2350]200    void Engine::changedActivity()
201    {
202        SUPER(Engine, changedActivity);
203
204        if (this->boostBlur_)
205            this->boostBlur_->setVisible(this->isVisible());
206    }
207
[2254]208    void Engine::addToSpaceShip(SpaceShip* ship)
209    {
210        this->ship_ = ship;
211        if (ship)
[2256]212        {
[2254]213            this->shipID_ = ship->getObjectID();
[2256]214            if (ship->getEngine() != this)
215                ship->setEngine(this);
[2350]216
217            if (this->boostBlur_)
218            {
219                delete this->boostBlur_;
220                this->boostBlur_ = 0;
221            }
[2256]222        }
[2254]223    }
224
225    const Vector3& Engine::getDirection() const
226    {
227        if (this->ship_)
228            return this->ship_->getSteeringDirection();
229        else
230            return Vector3::ZERO;
231    }
232}
Note: See TracBrowser for help on using the repository browser.