Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/bigships/src/orxonox/worldentities/pawns/SpaceShip.cc @ 8503

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

Values that are needed in the destructor need to be initialized before RegisterObject(…) is executed, because when the class hierarchy is created an object of each class is created and destroyed when RegisterObject(…) is executed.

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