Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/items/Engine.cc @ 2256

Last change on this file since 2256 was 2256, checked in by landauf, 15 years ago

The SpaceShips HUD is working again (Speedbar and Radar)

  • Property svn:eol-style set to native
File size: 7.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/XMLPort.h"
34#include "objects/worldentities/pawns/SpaceShip.h"
35
36namespace orxonox
37{
38    CreateFactory(Engine);
39
40    Engine::Engine(BaseObject* creator) : Item(creator)
41    {
42        RegisterObject(Engine);
43
44        this->ship_ = 0;
45        this->shipID_ = OBJECTID_UNKNOWN;
46
47        this->boostFactor_ = 1.5;
48        this->speedFactor_ = 1.0;
49
50        this->maxSpeedFront_ = 0.0;
51        this->maxSpeedBack_ = 0.0;
52        this->maxSpeedLeftRight_ = 0.0;
53        this->maxSpeedUpDown_ = 0.0;
54
55        this->accelerationFront_ = 0.0;
56        this->accelerationBrake_ = 0.0;
57        this->accelerationBack_ = 0.0;
58        this->accelerationLeftRight_ = 0.0;
59        this->accelerationUpDown_ = 0.0;
60
61        this->registerVariables();
62    }
63
64    Engine::~Engine()
65    {
66        if (this->isInitialized() && this->ship_)
67            this->ship_->setEngine(0);
68    }
69
70    void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
71    {
72        SUPER(Engine, XMLPort, xmlelement, mode);
73
74        XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode);
75
76        XMLPortParam(Engine, "speedfront",     setMaxSpeedFront,     setMaxSpeedFront,     xmlelement, mode);
77        XMLPortParam(Engine, "speedback",      setMaxSpeedBack,      setMaxSpeedBack,      xmlelement, mode);
78        XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode);
79        XMLPortParam(Engine, "speedupdown",    setMaxSpeedUpDown,    setMaxSpeedUpDown,    xmlelement, mode);
80
81        XMLPortParam(Engine, "accelerationfront",     setAccelerationFront,     setAccelerationFront,     xmlelement, mode);
82        XMLPortParam(Engine, "accelerationbrake",     setAccelerationBrake,     setAccelerationBrake,     xmlelement, mode);
83        XMLPortParam(Engine, "accelerationback",      setAccelerationBack,      setAccelerationBack,      xmlelement, mode);
84        XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode);
85        XMLPortParam(Engine, "accelerationupdown",    setAccelerationUpDown,    setAccelerationUpDown,    xmlelement, mode);
86    }
87
88    void Engine::registerVariables()
89    {
90        REGISTERDATA(this->shipID_, direction::toclient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
91
92        REGISTERDATA(this->speedFactor_, direction::toclient);
93        REGISTERDATA(this->boostFactor_, direction::toclient);
94
95        REGISTERDATA(this->maxSpeedFront_,     direction::toclient);
96        REGISTERDATA(this->maxSpeedBack_,      direction::toclient);
97        REGISTERDATA(this->maxSpeedLeftRight_, direction::toclient);
98        REGISTERDATA(this->maxSpeedUpDown_,    direction::toclient);
99
100        REGISTERDATA(this->accelerationFront_,     direction::toclient);
101        REGISTERDATA(this->accelerationBrake_,     direction::toclient);
102        REGISTERDATA(this->accelerationBack_,      direction::toclient);
103        REGISTERDATA(this->accelerationLeftRight_, direction::toclient);
104        REGISTERDATA(this->accelerationUpDown_,    direction::toclient);
105    }
106
107    void Engine::networkcallback_shipID()
108    {
109        this->ship_ = 0;
110
111        if (this->shipID_ != OBJECTID_UNKNOWN)
112        {
113            Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_);
114            if (object)
115                this->addToSpaceShip(dynamic_cast<SpaceShip*>(object));
116        }
117    }
118
119    void Engine::tick(float dt)
120    {
121        if (!this->ship_)
122        {
123            if (this->shipID_)
124            {
125                this->networkcallback_shipID();
126
127                if (!this->ship_)
128                    return;
129            }
130            else
131                return;
132        }
133
134        if (!this->isActive())
135            return;
136
137        const Vector3& direction = this->getDirection();
138        Vector3 velocity = this->ship_->getVelocity();
139        Vector3 acceleration = Vector3::ZERO;
140
141        float factor = 1.0f / this->speedFactor_;
142        velocity *= factor;
143
144        if (direction.z < 0)
145        {
146            if (this->maxSpeedFront_ != 0)
147            {
148                float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f);
149                acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f);
150            }
151        }
152        else if (direction.z > 0)
153        {
154            if (velocity.z < 0)
155                acceleration.z = direction.z * this->accelerationBrake_;
156            else if (this->maxSpeedBack_ != 0)
157                acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);
158        }
159
160        if (this->maxSpeedLeftRight_ != 0)
161        {
162            if (direction.x < 0)
163                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
164            else if (direction.x > 0)
165                acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
166        }
167
168        if (this->maxSpeedUpDown_ != 0)
169        {
170            if (direction.y < 0)
171                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
172            else if (direction.y > 0)
173                acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
174        }
175
176        this->ship_->setAcceleration(acceleration);
177
178        this->ship_->setBoost(false);
179        this->ship_->setSteeringDirection(Vector3::ZERO);
180    }
181
182    void Engine::addToSpaceShip(SpaceShip* ship)
183    {
184        this->ship_ = ship;
185        if (ship)
186        {
187            this->shipID_ = ship->getObjectID();
188            if (ship->getEngine() != this)
189                ship->setEngine(this);
190        }
191    }
192
193    const Vector3& Engine::getDirection() const
194    {
195        if (this->ship_)
196            return this->ship_->getSteeringDirection();
197        else
198            return Vector3::ZERO;
199    }
200}
Note: See TracBrowser for help on using the repository browser.