Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/worldentities/pawns/SpaceShip.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: 14.8 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"
[8589]37#include "tools/Shader.h"
[8597]38#include "util/Debug.h" // TODO: Needed?
[8589]39#include "util/Math.h"
40
41#include "graphics/Camera.h"
[5735]42#include "items/Engine.h"
[8589]43
[8580]44#include "CameraManager.h"
[8589]45#include "Scene.h"
[2072]46
47namespace orxonox
48{
[2662]49    const float orientationGain = 100;
[2072]50    CreateFactory(SpaceShip);
51
[8589]52    SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator), boostBlur_(NULL)
[2072]53    {
54        RegisterObject(SpaceShip);
55
[2662]56        this->primaryThrust_  = 100;
57        this->auxilaryThrust_ =  30;
58        this->rotationThrust_ =  10;
[2072]59
[2662]60        this->localLinearAcceleration_.setValue(0, 0, 0);
61        this->localAngularAcceleration_.setValue(0, 0, 0);
62        this->bBoost_ = false;
63        this->steering_ = Vector3::ZERO;
[2072]64
[7801]65        this->boostPower_ = 10.0f;
66        this->initialBoostPower_ = 10.0f;
67        this->boostRate_ = 5.0;
68        this->boostPowerRate_ = 1.0;
69        this->boostCooldownDuration_ = 5.0;
70        this->bBoostCooldown_ = false;
[2072]71
[8597]72        this->lift_ = 1.0f;                         // factor of the lift, standard is 1
73        this->stallSpeed_ = 220.0f;                 // max speed where lift is added
74
[2072]75        this->bInvertYAxis_ = false;
76
77        this->setDestroyWhenPlayerLeft(true);
78
[2662]79        // SpaceShip is always a physical object per default
80        // Be aware of this call: The collision type legality check will not reach derived classes!
81        this->setCollisionType(WorldEntity::Dynamic);
82        // Get notification about collisions
83        this->enableCollisionCallback();
84
[8589]85        this->engineTicksNotDone = 0;
[2072]86        this->setConfigValues();
87        this->registerVariables();
[8580]88       
[8582]89        this->cameraOriginalPosition_ = Vector3::UNIT_SCALE;
90        this->cameraOriginalOrientation_ = Quaternion::IDENTITY;
[8580]91
92        this->shakeFrequency_ = 15;
93        this->shakeAmplitude_ = 5;
94        this->shakeDt_ = 0;
[2072]95    }
96
97    SpaceShip::~SpaceShip()
98    {
[8589]99        if (this->isInitialized())
100        {
101            this->removeAllEngines();
102       
103            if (this->boostBlur_)
104                this->boostBlur_->destroy();
105        }
[2072]106    }
107
108    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
109    {
110        SUPER(SpaceShip, XMLPort, xmlelement, mode);
111
[8589]112        //XMLPortParam(SpaceShip, "engine",            setEngineTemplate,    getEngineTemplate,    xmlelement, mode);
[2662]113        XMLPortParamVariable(SpaceShip, "primaryThrust",  primaryThrust_,  xmlelement, mode);
114        XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
115        XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode);
[7801]116        XMLPortParamVariable(SpaceShip, "boostPower", initialBoostPower_, xmlelement, mode);
117        XMLPortParamVariable(SpaceShip, "boostPowerRate", boostPowerRate_, xmlelement, mode);
118        XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode);
119        XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode);
[8589]120                XMLPortParamVariable(SpaceShip, "shakeFrequency", shakeFrequency_, xmlelement, mode);
[8580]121        XMLPortParamVariable(SpaceShip, "shakeAmplitude", shakeAmplitude_, xmlelement, mode);
[8597]122                XMLPortParamVariable(SpaceShip, "lift", lift_, xmlelement, mode);
123        XMLPortParamVariable(SpaceShip, "stallSpeed", stallSpeed_, xmlelement, mode);
[8589]124
125        XMLPortObject(SpaceShip, Engine, "engines", addEngine, getEngine, xmlelement, mode);
[2072]126    }
127
128    void SpaceShip::registerVariables()
129    {
[3280]130        registerVariable(this->primaryThrust_,  VariableDirection::ToClient);
131        registerVariable(this->auxilaryThrust_, VariableDirection::ToClient);
132        registerVariable(this->rotationThrust_, VariableDirection::ToClient);
[8580]133        // TODO: Synchronization of boost needed?
134        registerVariable(this->boostPower_, VariableDirection::ToClient);
135        registerVariable(this->boostPowerRate_, VariableDirection::ToClient);
136        registerVariable(this->boostRate_, VariableDirection::ToClient);
137        registerVariable(this->boostCooldownDuration_, VariableDirection::ToClient);
138        registerVariable(this->shakeFrequency_, VariableDirection::ToClient);
139        registerVariable(this->shakeAmplitude_, VariableDirection::ToClient);
[8597]140        registerVariable(this->lift_, VariableDirection::ToClient);
141        registerVariable(this->stallSpeed_, VariableDirection::ToClient);
[2072]142    }
143
144    void SpaceShip::setConfigValues()
145    {
146        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
[8589]147       
148        SetConfigValueExternal(bEnableMotionBlur_, "GraphicsSettings", "enableMotionBlur", true)
149            .description("Enable or disable the motion blur effect when moving very fast")
150            .callback(this, &SpaceShip::changedEnableMotionBlur);
151        SetConfigValueExternal(blurStrength_, "GraphicsSettings", "blurStrength", 3.0f)
152            .description("Defines the strength of the motion blur effect");
[2072]153    }
154
[2662]155    bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const
[2072]156    {
[2662]157        if (type != WorldEntity::Dynamic)
[2072]158        {
[2662]159            CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl;
160            assert(false); // Only in debug mode
161            return false;
[2072]162        }
[2662]163        else
164            return true;
165    }
[2072]166
[2662]167    void SpaceShip::tick(float dt)
168    {
[2809]169        SUPER(SpaceShip, tick, dt);
[2072]170
[2662]171        if (this->hasLocalController())
[2072]172        {
[8589]173            // Handle mouse look
[2662]174            if (!this->isInMouseLook())
175            {
176                this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
177                this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
178            }
[7860]179            this->localAngularAcceleration_.setValue(0, 0, 0);
180
[8589]181            // Charge boostPower
[7801]182            if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_)
183            {
184                this->boostPower_ += this->boostPowerRate_*dt;
185            }
[8589]186            // Use boostPower
[7801]187            if(this->bBoost_)
188            {
189                this->boostPower_ -=this->boostRate_*dt;
190                if(this->boostPower_ <= 0.0f)
191                {
[8580]192                    this->boost(false);
[7801]193                    this->bBoostCooldown_ = true;
194                    this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this)));
195                }
[8580]196
[8582]197                this->shakeCamera(dt);
[7801]198            }
[8589]199
200            // Enable Blur depending on settings
201            if (this->bEnableMotionBlur_ && !this->boostBlur_ && this->hasLocalController() && this->hasHumanController())
202            {
203                this->boostBlur_ = new Shader(this->getScene()->getSceneManager());
204                this->boostBlur_->setCompositorName("Radial Blur");
205            }
206
207            if (this->boostBlur_) // && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1)
208            {
209                // TODO: this->maxSpeedFront_ gets fastest engine
210                float blur = this->blurStrength_ * clamp((-this->getLocalVelocity().z - 0.0f /*this->maxSpeedFront_*/) / ((150.0f /*boostFactor_*/ - 1) * 1.5f /*this->maxSpeedFront_*/), 0.0f, 1.0f);
211
212                // Show and hide blur effect depending on state of booster
213                if(this->bBoost_)
214                    this->boostBlur_->setVisible(blur > 0);
215                else
216                    this->boostBlur_->setVisible(false);
217
218                this->boostBlur_->setParameter(0, 0, "sampleStrength", blur);
219            }
[2072]220        }
221    }
[7860]222
[2072]223    void SpaceShip::moveFrontBack(const Vector2& value)
224    {
[2662]225        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
226        this->steering_.z = -value.x;
[2072]227    }
228
229    void SpaceShip::moveRightLeft(const Vector2& value)
230    {
[2662]231        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
232        this->steering_.x = value.x;
[2072]233    }
234
235    void SpaceShip::moveUpDown(const Vector2& value)
236    {
[2662]237        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
238        this->steering_.y = value.x;
[2072]239    }
240
241    void SpaceShip::rotateYaw(const Vector2& value)
242    {
[3039]243        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x);
[2662]244
245        Pawn::rotateYaw(value);
[2072]246    }
247
248    void SpaceShip::rotatePitch(const Vector2& value)
249    {
[8607]250        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
[2662]251
252        Pawn::rotatePitch(value);
[8597]253
254                //This function call adds a lift to the ship when it is pitching to make it's movement more "realistic" and enhance the feeling.
255        if (abs(this-> getLocalVelocity().z) < stallSpeed_)  {this->moveUpDown(lift_ / 5 * value * sqrt(abs(this-> getLocalVelocity().z)));}
[2072]256    }
257
258    void SpaceShip::rotateRoll(const Vector2& value)
259    {
[2662]260        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x);
261
262        Pawn::rotateRoll(value);
[2072]263    }
[7860]264
[8580]265    void SpaceShip::fire()
[7801]266    {
[8580]267    }
[7860]268
[8580]269    /**
270    @brief
271        Starts or stops boosting.
272    @param bBoost
273        Whether to start or stop boosting.
274    */
275    void SpaceShip::boost(bool bBoost)
276    {
277        if(bBoost && !this->bBoostCooldown_)
[7801]278        {
[8580]279            this->bBoost_ = true;
[8582]280            Camera* camera = CameraManager::getInstance().getActiveCamera();
281            this->cameraOriginalPosition_ = camera->getPosition();
282            this->cameraOriginalOrientation_ = camera->getOrientation();
[8580]283        }
284        if(!bBoost)
285        {
[8589]286            this->bBoost_ = false;
[8580]287            this->resetCamera();
[7801]288        }
289    }
[8589]290
[8580]291    void SpaceShip::boostCooledDown(void)
[2072]292    {
[8580]293        this->bBoostCooldown_ = false;
[2072]294    }
[8580]295   
296    void SpaceShip::shakeCamera(float dt)
297    {
298        //make sure the ship is only shaking if it's moving
[8583]299        if (this->getVelocity().squaredLength() > 80.0f)
[8580]300        {
301            this->shakeDt_ += dt;
302   
[8583]303            float frequency = this->shakeFrequency_ * (this->getVelocity().squaredLength());
[8580]304   
[8583]305            if (this->shakeDt_ >= 1.0f/frequency)
[8580]306            {
[8583]307                this->shakeDt_ -= 1.0f/frequency;
[8580]308            }
309   
[8583]310            Degree angle = Degree(sin(this->shakeDt_ *2.0f* math::pi * frequency) * this->shakeAmplitude_);
[8580]311   
312            //COUT(0) << "Angle: " << angle << std::endl;
[8582]313            Camera* camera = this->getCamera();
[2662]314
[8580]315            //Shaking Camera effect
[8582]316            if (camera != 0)
[8580]317            {
[8582]318                camera->setOrientation(Vector3::UNIT_X, angle);
[8580]319            }
320        }
321    }
[8582]322
[8580]323    void SpaceShip::resetCamera()
[2662]324    {
[8582]325        Camera *camera = this->getCamera();
326
327        if (camera == 0)
[8580]328        {
329            COUT(2) << "Failed to reset camera!";
330            return;
331        }
332   
[8582]333        this->shakeDt_ = 0;
334        camera->setPosition(this->cameraOriginalPosition_);
335        camera->setOrientation(this->cameraOriginalOrientation_);
[2662]336    }
337
[8582]338    void SpaceShip::backupCamera()
339    {
340        Camera* camera = CameraManager::getInstance().getActiveCamera();
341        if(camera != NULL)
342        {
343            this->cameraOriginalPosition_ = camera->getPosition();
344            this->cameraOriginalOrientation_ = camera->getOrientation();
345        }
346    }
347
[8589]348    void SpaceShip::addEngine(orxonox::Engine* engine)
[2662]349    {
[8589]350        //COUT(0)<<"Adding an Engine: " << engine << endl;
351        this->engineList_.push_back(engine);
352        engine->addToSpaceShip(this);
353        this->resetEngineTicks();
354    }
355
356    bool SpaceShip::hasEngine(Engine* engine)
357    {
358        for(unsigned int i=0; i<this->engineList_.size(); i++)
[2662]359        {
[8589]360            if(this->engineList_[i]==engine)
361                return true;
362        }
363        return false;
364    }
[2662]365
[8589]366    Engine* SpaceShip::getEngine(unsigned int i)
367    {
368        if(this->engineList_.size()>=i)
369            return 0;
370        else
371            return this->engineList_[i];
372    }
[2662]373
[8589]374    void SpaceShip::removeAllEngines()
375    {
[8648]376        while(this->engineList_.size())
377            this->engineList_.back()->destroy();
[8589]378    }
[8648]379   
380    void SpaceShip::removeEngine(Engine* engine)
381    {
382        for(std::vector<Engine*>::iterator it=this->engineList_.begin(); it!=this->engineList_.end(); ++it)
383        {
384            if(*it==engine)
385            {
386                this->engineList_.erase(it);
387                return;
388            }
389        }
390    }
[2662]391
[8589]392    void SpaceShip::setSpeedFactor(float factor)
393    {
394        for(unsigned int i=0; i<this->engineList_.size(); i++)
395            this->engineList_[i]->setSpeedFactor(factor);
396    }
397    float SpaceShip::getSpeedFactor() // Calculate mean SpeedFactor.
398    {
399        float ret = 0; unsigned int i = 0;
400        for(; i<this->engineList_.size(); i++)
401            ret += this->engineList_[i]->getSpeedFactor();
402        ret /= (float)i;
403        return ret;
404    }
405    float SpaceShip::getMaxSpeedFront()
406    {
407        float ret=0;
408        for(unsigned int i=0; i<this->engineList_.size(); i++)
409        {
410            if(this->engineList_[i]->getMaxSpeedFront() > ret)
411                ret = this->engineList_[i]->getMaxSpeedFront();
[2662]412        }
[8589]413        return ret;
[2662]414    }
415
[8589]416    float SpaceShip::getBoostFactor()
[2662]417    {
[8589]418        float ret = 0; unsigned int i=0;
419        for(; i<this->engineList_.size(); i++)
420            ret += this->engineList_[i]->getBoostFactor();
421        ret /= (float)i;
422        return ret;
[2662]423    }
[6709]424
[7547]425    std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const
[6709]426    {
[6711]427        std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>();
[8589]428        for(unsigned int i=0; i<this->engineList_.size(); i++)
429            list->push_back(this->engineList_[i]);
[6709]430        return list;
431    }
[8580]432   
[8589]433    void SpaceShip::changedEnableMotionBlur()
434    {
435        if (!this->bEnableMotionBlur_)
436        {
437            this->boostBlur_->destroy();
438            this->boostBlur_ = 0;
439        }
440    }
[8580]441
[2072]442}
Note: See TracBrowser for help on using the repository browser.