Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/worldentities/pawns/SpaceShip.cc @ 8589

Last change on this file since 8589 was 8589, checked in by dafrick, 13 years ago

Merging bigships branch into presentation branch.

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