Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/steering/src/orxonox/worldentities/pawns/SpaceShip.cc @ 8501

Last change on this file since 8501 was 8431, checked in by mastalde, 14 years ago

added lift_ and stallSpeed_ for better flight behavior without errors…

  • Property svn:eol-style set to native
File size: 9.5 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 "SpaceShip.h"
30
31#include <BulletDynamics/Dynamics/btRigidBody.h>
32
33#include "core/CoreIncludes.h"
34#include "core/ConfigValueIncludes.h"
35#include "core/Template.h"
36#include "core/XMLPort.h"
37#include "items/Engine.h"
38#include "util/Debug.h"
39
40
41namespace orxonox
42{
43    const float orientationGain = 100;
44    CreateFactory(SpaceShip);
45
46    SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator)
47    {
48        RegisterObject(SpaceShip);
49
50        this->primaryThrust_  = 100;
51        this->auxilaryThrust_ =  30;
52        this->rotationThrust_ =  10;
53
54        this->localLinearAcceleration_.setValue(0, 0, 0);
55        this->localAngularAcceleration_.setValue(0, 0, 0);
56        this->bBoost_ = false;
57        this->steering_ = Vector3::ZERO;
58        this->engine_ = 0;
59
60        this->boostPower_ = 10.0f;
61        this->initialBoostPower_ = 10.0f;
62        this->boostRate_ = 5.0;
63        this->boostPowerRate_ = 1.0;
64        this->boostCooldownDuration_ = 5.0;
65        this->bBoostCooldown_ = false;
66
67        this->lift_ = 0.2f;
68        this->stallSpeed_ = 220.0f;
69
70        this->bInvertYAxis_ = false;
71
72        this->setDestroyWhenPlayerLeft(true);
73
74        // SpaceShip is always a physical object per default
75        // Be aware of this call: The collision type legality check will not reach derived classes!
76        this->setCollisionType(WorldEntity::Dynamic);
77        // Get notification about collisions
78        this->enableCollisionCallback();
79
80        this->setConfigValues();
81        this->registerVariables();
82
83
84
85       
86}
87
88    SpaceShip::~SpaceShip()
89    {
90        if (this->isInitialized() && this->engine_)
91            this->engine_->destroy();
92    }
93
94    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
95    {
96        SUPER(SpaceShip, XMLPort, xmlelement, mode);
97
98        XMLPortParam(SpaceShip, "engine",            setEngineTemplate,    getEngineTemplate,    xmlelement, mode);
99        XMLPortParamVariable(SpaceShip, "primaryThrust",  primaryThrust_,  xmlelement, mode);
100        XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
101        XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode);
102        XMLPortParamVariable(SpaceShip, "boostPower", initialBoostPower_, xmlelement, mode);
103        XMLPortParamVariable(SpaceShip, "boostPowerRate", boostPowerRate_, xmlelement, mode);
104        XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode);
105        XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode);
106        XMLPortParamVariable(SpaceShip, "lift", lift_, xmlelement, mode);
107        XMLPortParamVariable(SpaceShip, "stallSpeed", stallSpeed_, xmlelement, mode);
108    }
109
110    void SpaceShip::registerVariables()
111    {
112        registerVariable(this->primaryThrust_,  VariableDirection::ToClient);
113        registerVariable(this->auxilaryThrust_, VariableDirection::ToClient);
114        registerVariable(this->rotationThrust_, VariableDirection::ToClient);
115        registerVariable(this->boostPower_, VariableDirection::ToClient);
116        registerVariable(this->boostPowerRate_, VariableDirection::ToClient);
117        registerVariable(this->boostRate_, VariableDirection::ToClient);
118        registerVariable(this->boostCooldownDuration_, VariableDirection::ToClient);
119    }
120
121    void SpaceShip::setConfigValues()
122    {
123        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
124    }
125
126    bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const
127    {
128        if (type != WorldEntity::Dynamic)
129        {
130            CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl;
131            assert(false); // Only in debug mode
132            return false;
133        }
134        else
135            return true;
136    }
137
138    void SpaceShip::tick(float dt)
139    {
140        SUPER(SpaceShip, tick, dt);
141
142        if (this->hasLocalController())
143        {
144/*
145            this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_);
146            this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_);
147            if (this->localLinearAcceleration_.z() > 0)
148                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_);
149            else
150                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
151            this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
152            this->localLinearAcceleration_.setValue(0, 0, 0);
153*/
154            if (!this->isInMouseLook())
155            {
156                this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
157                this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
158            }
159
160            this->localAngularAcceleration_.setValue(0, 0, 0);
161
162            if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_)
163            {
164                this->boostPower_ += this->boostPowerRate_*dt;
165            }
166            if(this->bBoost_)
167            {
168                this->boostPower_ -=this->boostRate_*dt;
169                if(this->boostPower_ <= 0.0f)
170                {
171                    this->bBoost_ = false;
172                    this->bBoostCooldown_ = true;
173                    this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this)));
174                }
175            }
176       
177//COUT(1) << "Vel:" << this-> getLocalVelocity().z * -1 << endl;
178               
179        }
180    }
181
182    void SpaceShip::boostCooledDown(void)
183    {
184        this->bBoostCooldown_ = false;
185    }
186
187    void SpaceShip::moveFrontBack(const Vector2& value)
188    {
189        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
190        this->steering_.z = -value.x;
191
192    }
193
194    void SpaceShip::moveRightLeft(const Vector2& value)
195    {
196        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
197        this->steering_.x = value.x;
198 
199    }
200
201    void SpaceShip::moveUpDown(const Vector2& value)
202    {
203        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
204        this->steering_.y = value.x;
205
206
207
208
209
210    }
211
212    void SpaceShip::rotateYaw(const Vector2& value)
213    {
214        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x);
215
216        Pawn::rotateYaw(value);
217
218    }
219
220    void SpaceShip::rotatePitch(const Vector2& value)
221    {
222        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x * 0.8);
223
224        Pawn::rotatePitch(value);
225
226       
227       
228        if (abs(this-> getLocalVelocity().z) < stallSpeed_)  {this->moveUpDown(lift_*value*sqrt(abs(this-> getLocalVelocity().z)));}
229
230       
231
232
233    }
234
235    void SpaceShip::rotateRoll(const Vector2& value)
236    {
237        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x);
238
239        Pawn::rotateRoll(value);
240 
241    }
242
243    void SpaceShip::fire()
244    {
245    }
246
247    /**
248    @brief
249        Starts or stops boosting.
250    @param bBoost
251        Whether to start or stop boosting.
252    */
253    void SpaceShip::boost(bool bBoost)
254    {
255        if(bBoost && !this->bBoostCooldown_)
256            this->bBoost_ = true;
257        if(!bBoost)
258            this->bBoost_ = false;
259    }
260
261    void SpaceShip::loadEngineTemplate()
262    {
263        if (!this->enginetemplate_.empty())
264        {
265            Template* temp = Template::getTemplate(this->enginetemplate_);
266
267            if (temp)
268            {
269                Identifier* identifier = temp->getBaseclassIdentifier();
270
271                if (identifier)
272                {
273                    BaseObject* object = identifier->fabricate(this);
274                    this->engine_ = orxonox_cast<Engine*>(object);
275
276                    if (this->engine_)
277                    {
278                        this->engine_->addTemplate(temp);
279                        this->engine_->addToSpaceShip(this);
280                    }
281                    else
282                    {
283                        object->destroy();
284                    }
285                }
286            }
287        }
288    }
289
290    void SpaceShip::setEngine(Engine* engine)
291    {
292        this->engine_ = engine;
293        if (engine && engine->getShip() != this)
294            engine->addToSpaceShip(this);
295    }
296
297    std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const
298    {
299        std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>();
300        list->push_back(this->engine_);
301        return list;
302    }
303}
Note: See TracBrowser for help on using the repository browser.