Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/bigships/src/orxonox/items/Engine.cc @ 8474

Last change on this file since 8474 was 8426, checked in by decryphe, 14 years ago
  • Moved handling of blur effect from Engine.cc to SpaceShip.cc.
  • Implemented handling of multiple engines per SpaceShip, so that in future it would be possible to turn single engines on/off individually (for example if one gets destroyed on a big ship).
  • Updated some pointers accessing the single Engine of a SpaceShip to ask the SpaceShip itself instead for data.
  • Property svn:eol-style set to native
File size: 8.9 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 "util/Math.h"
32#include "core/CoreIncludes.h"
33#include "core/ConfigValueIncludes.h"
34#include "core/XMLPort.h"
35#include "Scene.h"
36#include "worldentities/pawns/SpaceShip.h"
37#include "core/Template.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                this->relativePosition_ = Vector3(0,0,0);
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->speedAdd_ = 0.0;
66        this->speedMultiply_ = 1.0;
67
68        this->setConfigValues();
69        this->registerVariables();
70    }
71
72    Engine::~Engine()
73    {
74        if (this->isInitialized() && this->ship_)
75        {
76            //this->ship_->setEngine(0);
77
78            //if (this->boostBlur_)
79            //    this->boostBlur_->destroy();
80        }
81    }
82
83    void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
84    {
85        SUPER(Engine, XMLPort, xmlelement, mode);
86
87        XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode);
88
89        XMLPortParam(Engine, "speedfront",     setMaxSpeedFront,     setMaxSpeedFront,     xmlelement, mode);
90        XMLPortParam(Engine, "speedback",      setMaxSpeedBack,      setMaxSpeedBack,      xmlelement, mode);
91        XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode);
92        XMLPortParam(Engine, "speedupdown",    setMaxSpeedUpDown,    setMaxSpeedUpDown,    xmlelement, mode);
93
94        XMLPortParam(Engine, "accelerationfront",     setAccelerationFront,     setAccelerationFront,     xmlelement, mode);
95        XMLPortParam(Engine, "accelerationbrake",     setAccelerationBrake,     setAccelerationBrake,     xmlelement, mode);
96        XMLPortParam(Engine, "accelerationback",      setAccelerationBack,      setAccelerationBack,      xmlelement, mode);
97        XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode);
98        XMLPortParam(Engine, "accelerationupdown",    setAccelerationUpDown,    setAccelerationUpDown,    xmlelement, mode);
99
100                XMLPortParam(Engine, "position", setRelativePosition, getRelativePosition, xmlelement, mode);
101                XMLPortParam(Engine, "template", setEngineTemplate, getEngineTemplate, xmlelement, mode);
102    }
103
104    void Engine::setConfigValues()
105    {
106    }
107
108    void Engine::registerVariables()
109    {
110        registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
111
112        registerVariable(this->speedFactor_, VariableDirection::ToClient);
113        registerVariable(this->boostFactor_, VariableDirection::ToClient);
114
115        registerVariable(this->maxSpeedFront_,     VariableDirection::ToClient);
116        registerVariable(this->maxSpeedBack_,      VariableDirection::ToClient);
117        registerVariable(this->maxSpeedLeftRight_, VariableDirection::ToClient);
118        registerVariable(this->maxSpeedUpDown_,    VariableDirection::ToClient);
119
120        registerVariable(this->accelerationFront_,     VariableDirection::ToClient);
121        registerVariable(this->accelerationBrake_,     VariableDirection::ToClient);
122        registerVariable(this->accelerationBack_,      VariableDirection::ToClient);
123        registerVariable(this->accelerationLeftRight_, VariableDirection::ToClient);
124        registerVariable(this->accelerationUpDown_,    VariableDirection::ToClient);
125
126        registerVariable(this->speedAdd_, VariableDirection::ToClient);
127        registerVariable(this->speedMultiply_, 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(orxonox_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                // NOTE: Bullet always uses global coordinates.
202        this->ship_->addAcceleration(this->ship_->getOrientation() * (acceleration*this->getSpeedMultiply()+Vector3(0,0,-this->getSpeedAdd())), this->ship_->getOrientation() * this->relativePosition_);
203
204
205                // Hack to reset a temporary variable "direction"
206                this->ship_->oneEngineTickDone();
207                if(!this->ship_->hasEngineTicksRemaining())
208                {
209                        this->ship_->setSteeringDirection(Vector3::ZERO);
210                        this->ship_->resetEngineTicks();
211                }
212    }
213
214    void Engine::changedActivity()
215    {
216        SUPER(Engine, changedActivity);
217    }
218
219    void Engine::addToSpaceShip(SpaceShip* ship)
220    {
221        this->ship_ = ship;
222
223        if (ship)
224        {
225            this->shipID_ = ship->getObjectID();
226            if (!ship->hasEngine(this))
227                ship->addEngine(this);
228        }
229    }
230
231    const Vector3& Engine::getDirection() const
232    {
233        if (this->ship_)
234            return this->ship_->getSteeringDirection();
235        else
236            return Vector3::ZERO;
237    }
238
239    PickupCarrier* Engine::getCarrierParent(void) const
240    {
241        return this->ship_;
242    }
243
244    const Vector3& Engine::getCarrierPosition(void) const
245    {
246        return this->ship_->getWorldPosition();
247    }
248
249        void Engine::loadEngineTemplate()
250        {
251                if(!this->engineTemplate_.empty())
252                {
253                        COUT(4)<<"Loading an engine template: "<<this->engineTemplate_<<"\n";
254                        Template *temp = Template::getTemplate(this->engineTemplate_);
255                        if(temp)
256                        {
257                                this->addTemplate(temp);
258                        }
259                }
260        }
261}
Note: See TracBrowser for help on using the repository browser.