Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3132 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 9.3 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 "core/CoreIncludes.h"
32#include "core/ConfigValueIncludes.h"
33#include "core/XMLPort.h"
34#include "objects/Scene.h"
35#include "objects/worldentities/pawns/SpaceShip.h"
36#include "tools/Shader.h"
37#include "sound/SoundBase.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        this->sound_ = NULL;
70    }
71
72    Engine::~Engine()
73    {
74        if (this->isInitialized() && this->ship_)
75        {
76            this->ship_->setEngine(0);
77
78            if (this->boostBlur_)
79                delete this->boostBlur_;
80
81            if(this->sound_ != NULL)
82                delete this->sound_;
83        }
84    }
85
86    void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
87    {
88        SUPER(Engine, XMLPort, xmlelement, mode);
89
90        XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode);
91
92        XMLPortParam(Engine, "speedfront",     setMaxSpeedFront,     setMaxSpeedFront,     xmlelement, mode);
93        XMLPortParam(Engine, "speedback",      setMaxSpeedBack,      setMaxSpeedBack,      xmlelement, mode);
94        XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode);
95        XMLPortParam(Engine, "speedupdown",    setMaxSpeedUpDown,    setMaxSpeedUpDown,    xmlelement, mode);
96
97        XMLPortParam(Engine, "accelerationfront",     setAccelerationFront,     setAccelerationFront,     xmlelement, mode);
98        XMLPortParam(Engine, "accelerationbrake",     setAccelerationBrake,     setAccelerationBrake,     xmlelement, mode);
99        XMLPortParam(Engine, "accelerationback",      setAccelerationBack,      setAccelerationBack,      xmlelement, mode);
100        XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode);
101        XMLPortParam(Engine, "accelerationupdown",    setAccelerationUpDown,    setAccelerationUpDown,    xmlelement, mode);
102
103        XMLPortParamLoadOnly(Engine, "sound", loadSound, xmlelement, mode);
104    }
105
106    void Engine::setConfigValues()
107    {
108        SetConfigValue(blurStrength_, 3.0f);
109    }
110
111    void Engine::registerVariables()
112    {
113        registerVariable(this->shipID_, variableDirection::toclient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
114
115        registerVariable(this->speedFactor_, variableDirection::toclient);
116        registerVariable(this->boostFactor_, variableDirection::toclient);
117
118        registerVariable(this->maxSpeedFront_,     variableDirection::toclient);
119        registerVariable(this->maxSpeedBack_,      variableDirection::toclient);
120        registerVariable(this->maxSpeedLeftRight_, variableDirection::toclient);
121        registerVariable(this->maxSpeedUpDown_,    variableDirection::toclient);
122
123        registerVariable(this->accelerationFront_,     variableDirection::toclient);
124        registerVariable(this->accelerationBrake_,     variableDirection::toclient);
125        registerVariable(this->accelerationBack_,      variableDirection::toclient);
126        registerVariable(this->accelerationLeftRight_, variableDirection::toclient);
127        registerVariable(this->accelerationUpDown_,    variableDirection::toclient);
128    }
129
130    void Engine::networkcallback_shipID()
131    {
132        this->ship_ = 0;
133
134        if (this->shipID_ != OBJECTID_UNKNOWN)
135        {
136            Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_);
137            if (object)
138                this->addToSpaceShip(dynamic_cast<SpaceShip*>(object));
139        }
140    }
141
142    void Engine::tick(float dt)
143    {
144        if (!this->ship_)
145        {
146            if (this->shipID_)
147            {
148                this->networkcallback_shipID();
149
150                if (!this->ship_)
151                    return;
152            }
153            else
154                return;
155        }
156
157        if (!this->isActive())
158            return;
159
160        SUPER(Engine, tick, dt);
161
162        const Vector3& direction = this->getDirection();
163        Vector3 velocity = this->ship_->getLocalVelocity();
164        Vector3 acceleration = Vector3::ZERO;
165
166        float factor = 1.0f / this->speedFactor_;
167        velocity *= factor;
168
169        if (direction.z < 0)
170        {
171            if (this->maxSpeedFront_ != 0)
172            {
173                float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f);
174                acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f);
175            }
176        }
177        else if (direction.z > 0)
178        {
179            if (velocity.z < 0)
180                acceleration.z = direction.z * this->accelerationBrake_;
181            else if (this->maxSpeedBack_ != 0)
182                acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);
183        }
184
185        if (this->maxSpeedLeftRight_ != 0)
186        {
187            if (direction.x < 0)
188                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
189            else if (direction.x > 0)
190                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
191        }
192
193        if (this->maxSpeedUpDown_ != 0)
194        {
195            if (direction.y < 0)
196                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
197            else if (direction.y > 0)
198                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
199        }
200
201        this->ship_->setAcceleration(this->ship_->getPickups().processModifiers(ModifierType::Acceleration, this->ship_->getOrientation() * acceleration, false));
202
203        if (!this->ship_->getPermanentBoost())
204            this->ship_->setBoost(false);
205        this->ship_->setSteeringDirection(Vector3::ZERO);
206
207        if (!this->boostBlur_ && this->ship_->hasLocalController() && this->ship_->hasHumanController())
208        {
209            this->boostBlur_ = new Shader(this->ship_->getScene()->getSceneManager());
210            this->boostBlur_->setCompositor("Radial Blur");
211        }
212
213        if (this->boostBlur_ && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1)
214            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));
215    }
216
217    void Engine::changedActivity()
218    {
219        SUPER(Engine, changedActivity);
220
221        if (this->boostBlur_)
222            this->boostBlur_->setVisible(this->isVisible());
223    }
224
225    void Engine::addToSpaceShip(SpaceShip* ship)
226    {
227        this->ship_ = ship;
228
229        if (ship)
230        {
231            this->shipID_ = ship->getObjectID();
232            if (ship->getEngine() != this)
233                ship->setEngine(this);
234
235            if (this->boostBlur_)
236            {
237                delete this->boostBlur_;
238                this->boostBlur_ = 0;
239            }
240
241            if(this->sound_ != NULL)
242                this->sound_->attachToEntity(ship);
243        }
244    }
245
246    const Vector3& Engine::getDirection() const
247    {
248        if (this->ship_)
249            return this->ship_->getSteeringDirection();
250        else
251            return Vector3::ZERO;
252    }
253
254    void Engine::loadSound(const std::string filename)
255    {
256        if(filename == "") return;
257        else
258        {
259            if(this->sound_ == NULL)
260            {
261                this->sound_ = new SoundBase(this->ship_);
262            }
263
264            this->sound_->loadFile(filename);
265            this->sound_->play(true);
266        }
267    }
268}
Note: See TracBrowser for help on using the repository browser.