Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mac_HS16/src/orxonox/items/Engine.cc @ 11273

Last change on this file since 11273 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 9.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 "core/CoreIncludes.h"
32#include "core/config/ConfigValueIncludes.h"
33#include "core/Template.h"
34#include "core/XMLPort.h"
35#include "util/Math.h"
36
37#include "Scene.h"
38#include "worldentities/pawns/SpaceShip.h"
39
40namespace orxonox
41{
42    RegisterClass(Engine);
43
44    /**
45    @brief
46        Constructor. Registers and initializes the object.
47    */
48    Engine::Engine(Context* context) : Item(context)
49    {
50        RegisterObject(Engine);
51
52        this->ship_ = nullptr;
53        this->shipID_ = OBJECTID_UNKNOWN;
54        this->relativePosition_ = Vector3::ZERO;
55
56        this->boostFactor_ = 1.5f;
57
58        this->maxSpeedFront_ = 0.0f;
59        this->maxSpeedBack_ = 0.0f;
60        this->maxSpeedLeftRight_ = 0.0f;
61        this->maxSpeedUpDown_ = 0.0f;
62
63        this->accelerationFront_ = 0.0f;
64        this->accelerationBrake_ = 0.0f;
65        this->accelerationBack_ = 0.0f;
66        this->accelerationLeftRight_ = 0.0f;
67        this->accelerationUpDown_ = 0.0f;
68
69        this->speedAdd_ = 0.0f;
70        this->speedMultiply_ = 1.0f;
71
72        this->setConfigValues();
73        this->registerVariables();
74    }
75
76    /**
77    @brief
78        Destructor. Destroys the object and removes it from the SpaceShip.
79    */
80    Engine::~Engine()
81    {
82        if (this->isInitialized())
83        {
84            // Remove the engine from the ShapeShip.
85            if (this->ship_ && this->ship_->hasEngine(this))
86                this->ship_->removeEngine(this);
87        }
88    }
89
90    void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
91    {
92        SUPER(Engine, XMLPort, xmlelement, mode);
93
94        XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode);
95
96        XMLPortParam(Engine, "speedfront",     setMaxSpeedFront,     setMaxSpeedFront,     xmlelement, mode);
97        XMLPortParam(Engine, "speedback",      setMaxSpeedBack,      setMaxSpeedBack,      xmlelement, mode);
98        XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode);
99        XMLPortParam(Engine, "speedupdown",    setMaxSpeedUpDown,    setMaxSpeedUpDown,    xmlelement, mode);
100
101        XMLPortParam(Engine, "accelerationfront",     setAccelerationFront,     setAccelerationFront,     xmlelement, mode);
102        XMLPortParam(Engine, "accelerationbrake",     setAccelerationBrake,     setAccelerationBrake,     xmlelement, mode);
103        XMLPortParam(Engine, "accelerationback",      setAccelerationBack,      setAccelerationBack,      xmlelement, mode);
104        XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode);
105        XMLPortParam(Engine, "accelerationupdown",    setAccelerationUpDown,    setAccelerationUpDown,    xmlelement, mode);
106
107        XMLPortParam(Engine, "position", setRelativePosition, getRelativePosition, xmlelement, mode);
108        XMLPortParam(Engine, "template", setEngineTemplate, getEngineTemplate, xmlelement, mode);
109    }
110
111    void Engine::setConfigValues()
112    {
113    }
114
115    void Engine::registerVariables()
116    {
117        registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
118
119        registerVariable(this->boostFactor_, VariableDirection::ToClient);
120
121        registerVariable(this->maxSpeedFront_,     VariableDirection::ToClient);
122        registerVariable(this->maxSpeedBack_,      VariableDirection::ToClient);
123        registerVariable(this->maxSpeedLeftRight_, VariableDirection::ToClient);
124        registerVariable(this->maxSpeedUpDown_,    VariableDirection::ToClient);
125
126        registerVariable(this->accelerationFront_,     VariableDirection::ToClient);
127        registerVariable(this->accelerationBrake_,     VariableDirection::ToClient);
128        registerVariable(this->accelerationBack_,      VariableDirection::ToClient);
129        registerVariable(this->accelerationLeftRight_, VariableDirection::ToClient);
130        registerVariable(this->accelerationUpDown_,    VariableDirection::ToClient);
131
132        registerVariable(this->speedAdd_, VariableDirection::ToClient);
133        registerVariable(this->speedMultiply_, VariableDirection::ToClient);
134    }
135
136    void Engine::networkcallback_shipID()
137    {
138        this->ship_ = nullptr;
139
140        if (this->shipID_ != OBJECTID_UNKNOWN)
141        {
142            Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_);
143            if (object)
144                this->addToSpaceShip(orxonox_cast<SpaceShip*>(object));
145        }
146    }
147
148    /**
149    @brief
150        Run the engine for a given time interval.
151        Is called each tick by SpaceShip.
152    @param dt
153        The time since last tick.
154    */
155    void Engine::run(float dt)
156    {
157        if (this->ship_ == nullptr)
158        {
159            if (this->shipID_ != 0)
160            {
161                this->networkcallback_shipID();
162
163                if (this->ship_ == nullptr)
164                    return;
165            }
166            else
167                return;
168        }
169
170        if (!this->isActive())
171            return;
172
173        // Get the desired steering direction and amount, clipped to length 1 at maximum.
174        Vector3 steering = (this->getSteering().length() > 1.0f ? this->getSteering().normalisedCopy() : this->getSteering());
175
176        // Get the ships velocity.
177        Vector3 velocity = this->ship_->getLocalVelocity();
178        Vector3 acceleration = Vector3::ZERO;
179
180        // If there is forward steering action.
181        if (steering.z < 0)
182        {
183            if (this->maxSpeedFront_ != 0)
184            {
185                float boostfactor = (this->ship_->isBoosting() ? this->boostFactor_ : 1.0f); // Boost factor is 1.0 if not boosting.
186                // Boosting can lead to velocities larger the maximal forward velocity.
187                acceleration.z = steering.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f);
188            }
189        }
190        // If there is backward steering action.
191        else if (steering.z > 0)
192        {
193            // Either breaking
194            if (velocity.z < 0)
195                acceleration.z = steering.z * this->accelerationBrake_;
196            // or backward flight.
197            else if (this->maxSpeedBack_ != 0)
198                acceleration.z = steering.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);
199        }
200        // If there is left-right steering action.
201        if (this->maxSpeedLeftRight_ != 0)
202        {
203            if (steering.x < 0)
204                acceleration.x = steering.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
205            else if (steering.x > 0)
206                acceleration.x = steering.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
207        }
208        // If there is up-down steering action.
209        if (this->maxSpeedUpDown_ != 0)
210        {
211            if (steering.y < 0)
212                acceleration.y = steering.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
213            else if (steering.y > 0)
214                acceleration.y = steering.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
215        }
216
217        // NOTE: Bullet always uses global coordinates.
218        this->ship_->addAcceleration(this->ship_->getOrientation() * (acceleration*this->getSpeedMultiply()+Vector3(0,0,-this->getSpeedAdd())), this->ship_->getOrientation() * this->relativePosition_);
219    }
220
221    /**
222    @brief
223        Adds the Engine to the input SpaceShip.
224    @param ship
225        A pointer to the SpaceShip to which the engine is added.
226    */
227    void Engine::addToSpaceShip(SpaceShip* ship)
228    {
229        this->ship_ = ship;
230
231        if (ship)
232        {
233            this->shipID_ = ship->getObjectID();
234            if (!ship->hasEngine(this))
235                ship->addEngine(this);
236        }
237    }
238
239    /**
240    @brief
241        Get the direction and magnitude of the steering imposed upon the Engine.
242    @return
243        Returns the direction and magnitude of the steering imposed upon the Engine.
244        Is the zero vector if the Engine doesn't belong to a ship.
245    */
246    const Vector3& Engine::getSteering() const
247    {
248        if (this->ship_)
249            return this->ship_->getSteeringDirection();
250        else
251            return Vector3::ZERO;
252    }
253
254    /**
255    @brief
256        Load the engine template.
257        Causes all parameters specified by the template to be applied to the Engine.
258    */
259    void Engine::loadEngineTemplate()
260    {
261        if(!this->engineTemplate_.empty())
262        {
263            orxout(verbose, context::templates) << "Loading an engine template: " << this->engineTemplate_ << endl;
264            Template *temp = Template::getTemplate(this->engineTemplate_);
265            if(temp)
266            {
267                this->addTemplate(temp);
268            }
269        }
270    }
271
272}
Note: See TracBrowser for help on using the repository browser.