Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/items/Engine.cc @ 8993

Last change on this file since 8993 was 8658, checked in by landauf, 14 years ago

the "lift" which is applied to the spaceship when pitching is now also applied when yawing.
moving up/down or left/right is now again possible.

  • Property svn:eol-style set to native
File size: 9.2 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"
[5735]35#include "Scene.h"
36#include "worldentities/pawns/SpaceShip.h"
[8589]37#include "core/Template.h"
[2254]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;
[8589]49        this->relativePosition_ = Vector3(0,0,0);
[2254]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
[6709]65        this->speedAdd_ = 0.0;
66        this->speedMultiply_ = 1.0;
67
[2478]68        this->setConfigValues();
[2254]69        this->registerVariables();
70    }
71
72    Engine::~Engine()
73    {
[8654]74        if (this->isInitialized())
75        {
76            if (this->ship_ && this->ship_->hasEngine(this))
77                this->ship_->removeEngine(this);
78        }
[2254]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);
[8589]97
98        XMLPortParam(Engine, "position", setRelativePosition, getRelativePosition, xmlelement, mode);
99        XMLPortParam(Engine, "template", setEngineTemplate, getEngineTemplate, xmlelement, mode);
[2254]100    }
101
[2478]102    void Engine::setConfigValues()
103    {
104    }
105
[2254]106    void Engine::registerVariables()
107    {
[3280]108        registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
[2254]109
[3280]110        registerVariable(this->speedFactor_, VariableDirection::ToClient);
111        registerVariable(this->boostFactor_, VariableDirection::ToClient);
[2254]112
[3280]113        registerVariable(this->maxSpeedFront_,     VariableDirection::ToClient);
114        registerVariable(this->maxSpeedBack_,      VariableDirection::ToClient);
115        registerVariable(this->maxSpeedLeftRight_, VariableDirection::ToClient);
116        registerVariable(this->maxSpeedUpDown_,    VariableDirection::ToClient);
[2254]117
[3280]118        registerVariable(this->accelerationFront_,     VariableDirection::ToClient);
119        registerVariable(this->accelerationBrake_,     VariableDirection::ToClient);
120        registerVariable(this->accelerationBack_,      VariableDirection::ToClient);
121        registerVariable(this->accelerationLeftRight_, VariableDirection::ToClient);
122        registerVariable(this->accelerationUpDown_,    VariableDirection::ToClient);
[6709]123
124        registerVariable(this->speedAdd_, VariableDirection::ToClient);
125        registerVariable(this->speedMultiply_, VariableDirection::ToClient);
[2254]126    }
127
128    void Engine::networkcallback_shipID()
129    {
130        this->ship_ = 0;
131
132        if (this->shipID_ != OBJECTID_UNKNOWN)
133        {
134            Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_);
135            if (object)
[3325]136                this->addToSpaceShip(orxonox_cast<SpaceShip*>(object));
[2254]137        }
138    }
139
140    void Engine::tick(float dt)
141    {
142        if (!this->ship_)
143        {
144            if (this->shipID_)
145            {
146                this->networkcallback_shipID();
147
148                if (!this->ship_)
149                    return;
150            }
151            else
152                return;
153        }
154
155        if (!this->isActive())
156            return;
157
[2361]158        SUPER(Engine, tick, dt);
159
[8658]160        Vector3 direction = this->getDirection();
161        float directionLength = direction.length();
162        if (directionLength > 1.0f)
163            direction /= directionLength; // normalize
164       
[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
[8589]203        // NOTE: Bullet always uses global coordinates.
204        this->ship_->addAcceleration(this->ship_->getOrientation() * (acceleration*this->getSpeedMultiply()+Vector3(0,0,-this->getSpeedAdd())), this->ship_->getOrientation() * this->relativePosition_);
[2254]205
[8589]206        // Hack to reset a temporary variable "direction"
207        this->ship_->oneEngineTickDone();
208        if(!this->ship_->hasEngineTicksRemaining())
[2350]209        {
[8589]210            this->ship_->setSteeringDirection(Vector3::ZERO);
211            this->ship_->resetEngineTicks();
[2350]212        }
[2254]213    }
214
[2350]215    void Engine::changedActivity()
216    {
217        SUPER(Engine, changedActivity);
218    }
219
[2254]220    void Engine::addToSpaceShip(SpaceShip* ship)
221    {
222        this->ship_ = ship;
[3060]223
[2254]224        if (ship)
[2256]225        {
[2254]226            this->shipID_ = ship->getObjectID();
[8589]227            if (!ship->hasEngine(this))
228                ship->addEngine(this);
[2256]229        }
[2254]230    }
231
232    const Vector3& Engine::getDirection() const
233    {
234        if (this->ship_)
235            return this->ship_->getSteeringDirection();
236        else
237            return Vector3::ZERO;
238    }
[6709]239
[7547]240    PickupCarrier* Engine::getCarrierParent(void) const
[6709]241    {
242        return this->ship_;
243    }
244
[7547]245    const Vector3& Engine::getCarrierPosition(void) const
[6709]246    {
247        return this->ship_->getWorldPosition();
248    }
[8079]249
[8589]250    void Engine::loadEngineTemplate()
[8079]251    {
[8589]252        if(!this->engineTemplate_.empty())
[8079]253        {
[8589]254            COUT(4)<<"Loading an engine template: "<<this->engineTemplate_<<"\n";
255            Template *temp = Template::getTemplate(this->engineTemplate_);
256            if(temp)
257            {
258                this->addTemplate(temp);
259            }
[8079]260        }
261    }
[2254]262}
Note: See TracBrowser for help on using the repository browser.