Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8648 was 8648, checked in by scheusso, 13 years ago

some network related fixes

  • Property svn:eol-style set to native
File size: 9.0 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->ship_ && this->ship_->hasEngine(this) )
75            this->ship_->removeEngine(this);
76    }
77
78    void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
79    {
80        SUPER(Engine, XMLPort, xmlelement, mode);
81
82        XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode);
83
84        XMLPortParam(Engine, "speedfront",     setMaxSpeedFront,     setMaxSpeedFront,     xmlelement, mode);
85        XMLPortParam(Engine, "speedback",      setMaxSpeedBack,      setMaxSpeedBack,      xmlelement, mode);
86        XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode);
87        XMLPortParam(Engine, "speedupdown",    setMaxSpeedUpDown,    setMaxSpeedUpDown,    xmlelement, mode);
88
89        XMLPortParam(Engine, "accelerationfront",     setAccelerationFront,     setAccelerationFront,     xmlelement, mode);
90        XMLPortParam(Engine, "accelerationbrake",     setAccelerationBrake,     setAccelerationBrake,     xmlelement, mode);
91        XMLPortParam(Engine, "accelerationback",      setAccelerationBack,      setAccelerationBack,      xmlelement, mode);
92        XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode);
93        XMLPortParam(Engine, "accelerationupdown",    setAccelerationUpDown,    setAccelerationUpDown,    xmlelement, mode);
94
95        XMLPortParam(Engine, "position", setRelativePosition, getRelativePosition, xmlelement, mode);
96        XMLPortParam(Engine, "template", setEngineTemplate, getEngineTemplate, xmlelement, mode);
97    }
98
99    void Engine::setConfigValues()
100    {
101    }
102
103    void Engine::registerVariables()
104    {
105        registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
106
107        registerVariable(this->speedFactor_, VariableDirection::ToClient);
108        registerVariable(this->boostFactor_, VariableDirection::ToClient);
109
110        registerVariable(this->maxSpeedFront_,     VariableDirection::ToClient);
111        registerVariable(this->maxSpeedBack_,      VariableDirection::ToClient);
112        registerVariable(this->maxSpeedLeftRight_, VariableDirection::ToClient);
113        registerVariable(this->maxSpeedUpDown_,    VariableDirection::ToClient);
114
115        registerVariable(this->accelerationFront_,     VariableDirection::ToClient);
116        registerVariable(this->accelerationBrake_,     VariableDirection::ToClient);
117        registerVariable(this->accelerationBack_,      VariableDirection::ToClient);
118        registerVariable(this->accelerationLeftRight_, VariableDirection::ToClient);
119        registerVariable(this->accelerationUpDown_,    VariableDirection::ToClient);
120
121        registerVariable(this->speedAdd_, VariableDirection::ToClient);
122        registerVariable(this->speedMultiply_, VariableDirection::ToClient);
123    }
124
125    void Engine::networkcallback_shipID()
126    {
127        this->ship_ = 0;
128
129        if (this->shipID_ != OBJECTID_UNKNOWN)
130        {
131            Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_);
132            if (object)
133                this->addToSpaceShip(orxonox_cast<SpaceShip*>(object));
134        }
135    }
136
137    void Engine::tick(float dt)
138    {
139        if (!this->ship_)
140        {
141            if (this->shipID_)
142            {
143                this->networkcallback_shipID();
144
145                if (!this->ship_)
146                    return;
147            }
148            else
149                return;
150        }
151
152        if (!this->isActive())
153            return;
154
155        SUPER(Engine, tick, dt);
156
157        const Vector3& direction = this->getDirection();
158        Vector3 velocity = this->ship_->getLocalVelocity();
159        Vector3 acceleration = Vector3::ZERO;
160
161        float factor = 1.0f / this->speedFactor_;
162        velocity *= factor;
163
164        if (direction.z < 0)
165        {
166            if (this->maxSpeedFront_ != 0)
167            {
168                float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f);
169                acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f);
170            }
171        }
172        else if (direction.z > 0)
173        {
174            if (velocity.z < 0)
175                acceleration.z = direction.z * this->accelerationBrake_;
176            else if (this->maxSpeedBack_ != 0)
177                acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);
178        }
179
180        if (this->maxSpeedLeftRight_ != 0)
181        {
182            if (direction.x < 0)
183                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
184            else if (direction.x > 0)
185                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
186        }
187
188        if (this->maxSpeedUpDown_ != 0)
189        {
190            if (direction.y < 0)
191                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
192            else if (direction.y > 0)
193                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
194        }
195
196        // NOTE: Bullet always uses global coordinates.
197        this->ship_->addAcceleration(this->ship_->getOrientation() * (acceleration*this->getSpeedMultiply()+Vector3(0,0,-this->getSpeedAdd())), this->ship_->getOrientation() * this->relativePosition_);
198
199        // Hack to reset a temporary variable "direction"
200        this->ship_->oneEngineTickDone();
201        if(!this->ship_->hasEngineTicksRemaining())
202        {
203            this->ship_->setSteeringDirection(Vector3::ZERO);
204            this->ship_->resetEngineTicks();
205        }
206    }
207
208    void Engine::changedActivity()
209    {
210        SUPER(Engine, changedActivity);
211    }
212
213    void Engine::addToSpaceShip(SpaceShip* ship)
214    {
215        this->ship_ = ship;
216
217        if (ship)
218        {
219            this->shipID_ = ship->getObjectID();
220            if (!ship->hasEngine(this))
221                ship->addEngine(this);
222        }
223    }
224
225    const Vector3& Engine::getDirection() const
226    {
227        if (this->ship_)
228            return this->ship_->getSteeringDirection();
229        else
230            return Vector3::ZERO;
231    }
232
233    PickupCarrier* Engine::getCarrierParent(void) const
234    {
235        return this->ship_;
236    }
237
238    const Vector3& Engine::getCarrierPosition(void) const
239    {
240        return this->ship_->getWorldPosition();
241    }
242
243    void Engine::loadEngineTemplate()
244    {
245        if(!this->engineTemplate_.empty())
246        {
247            COUT(4)<<"Loading an engine template: "<<this->engineTemplate_<<"\n";
248            Template *temp = Template::getTemplate(this->engineTemplate_);
249            if(temp)
250            {
251                this->addTemplate(temp);
252            }
253        }
254    }
255}
Note: See TracBrowser for help on using the repository browser.