Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound/src/orxonox/objects/items/Engine.cc @ 2982

Last change on this file since 2982 was 2982, checked in by erwin, 15 years ago

added some error checking

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