Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/pawns/SpaceShip.cc @ 8460

Last change on this file since 8460 was 7860, checked in by landauf, 14 years ago

fixed a few issues related to the free mouse look mode (default CTRL key):

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