Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/orxonox/objects/items/Engine.cc @ 2948

Last change on this file since 2948 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

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