Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5678 was 3325, checked in by rgrieder, 15 years ago

Merged orxonox_cast related revisions from core4 back to trunk.

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