Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gameimmersion/src/orxonox/worldentities/pawns/SpaceShip.cc @ 8138

Last change on this file since 8138 was 8138, checked in by dboehi, 13 years ago

First version of the camera shake effect for the afterburner

  • 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 "graphics/Camera.h"
39#include "CameraManager.h"
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->bPermanentBoost_ = false;
58        this->steering_ = Vector3::ZERO;
59        this->engine_ = 0;
60
61        this->boostPower_ = 10.0f;
62        this->initialBoostPower_ = 10.0f;
63        this->boostRate_ = 5.0;
64        this->boostPowerRate_ = 1.0;
65        this->boostCooldownDuration_ = 5.0;
66        this->bBoostCooldown_ = false;
67
68        this->bInvertYAxis_ = false;
69
70        this->setDestroyWhenPlayerLeft(true);
71
72        // SpaceShip is always a physical object per default
73        // Be aware of this call: The collision type legality check will not reach derived classes!
74        this->setCollisionType(WorldEntity::Dynamic);
75        // Get notification about collisions
76        this->enableCollisionCallback();
77
78        this->setConfigValues();
79        this->registerVariables();
80       
81        Camera* c = CameraManager::getInstance().getActiveCamera();
82        this->cameraOriginalPosition = c->getPosition();
83        this->cameraOriginalOrientation = c->getOrientation();
84    }
85
86    SpaceShip::~SpaceShip()
87    {
88        if (this->isInitialized() && this->engine_)
89            this->engine_->destroy();
90    }
91
92    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
93    {
94        SUPER(SpaceShip, XMLPort, xmlelement, mode);
95
96        XMLPortParam(SpaceShip, "engine",            setEngineTemplate,    getEngineTemplate,    xmlelement, mode);
97        XMLPortParamVariable(SpaceShip, "primaryThrust",  primaryThrust_,  xmlelement, mode);
98        XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
99        XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode);
100        XMLPortParamVariable(SpaceShip, "boostPower", initialBoostPower_, xmlelement, mode);
101        XMLPortParamVariable(SpaceShip, "boostPowerRate", boostPowerRate_, xmlelement, mode);
102        XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode);
103        XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode);
104    }
105
106    void SpaceShip::registerVariables()
107    {
108        registerVariable(this->primaryThrust_,  VariableDirection::ToClient);
109        registerVariable(this->auxilaryThrust_, VariableDirection::ToClient);
110        registerVariable(this->rotationThrust_, VariableDirection::ToClient);
111    }
112
113    void SpaceShip::setConfigValues()
114    {
115        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
116    }
117
118    bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const
119    {
120        if (type != WorldEntity::Dynamic)
121        {
122            CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl;
123            assert(false); // Only in debug mode
124            return false;
125        }
126        else
127            return true;
128    }
129
130    void SpaceShip::tick(float dt)
131    {
132        SUPER(SpaceShip, tick, dt);
133
134        if (this->hasLocalController())
135        {
136/*
137            this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_);
138            this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_);
139            if (this->localLinearAcceleration_.z() > 0)
140                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_);
141            else
142                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
143            this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
144            this->localLinearAcceleration_.setValue(0, 0, 0);
145*/
146            if (!this->isInMouseLook())
147            {
148                this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
149                this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
150            }
151
152            this->localAngularAcceleration_.setValue(0, 0, 0);
153
154            if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_)
155            {
156                this->boostPower_ += this->boostPowerRate_*dt;
157            }
158            if(this->bBoost_)
159            {
160                Camera* c = CameraManager::getInstance().getActiveCamera();
161                this->boostPower_ -=this->boostRate_*dt;
162                if(this->boostPower_ <= 0.0f)
163                {
164                    this->bBoost_ = false;
165                    this->bBoostCooldown_ = true;
166                    this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this)));
167                   
168                    c->setVelocity(0,0,0);
169                    c->setPosition(this->cameraOriginalPosition);
170                    c->setOrientation(this->cameraOriginalOrientation);
171                }
172                else
173                {
174                        //Kamera schuettelt
175                        COUT(1) << "Afterburner effect\n";
176                        if (c != 0)
177                        {
178                                c->setVelocity(0.1, 2.0, 0.3);
179                        }
180                }
181            }
182        }
183    }
184
185    void SpaceShip::boostCooledDown(void)
186    {
187        this->bBoostCooldown_ = false;
188    }
189
190    void SpaceShip::moveFrontBack(const Vector2& value)
191    {
192        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
193        this->steering_.z = -value.x;
194    }
195
196    void SpaceShip::moveRightLeft(const Vector2& value)
197    {
198        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
199        this->steering_.x = value.x;
200    }
201
202    void SpaceShip::moveUpDown(const Vector2& value)
203    {
204        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
205        this->steering_.y = value.x;
206    }
207
208    void SpaceShip::rotateYaw(const Vector2& value)
209    {
210        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x);
211
212        Pawn::rotateYaw(value);
213    }
214
215    void SpaceShip::rotatePitch(const Vector2& value)
216    {
217        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x);
218
219        Pawn::rotatePitch(value);
220    }
221
222    void SpaceShip::rotateRoll(const Vector2& value)
223    {
224        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x);
225
226        Pawn::rotateRoll(value);
227    }
228
229    // TODO: something seems to call this function every tick, could probably handled a little more efficiently!
230    void SpaceShip::setBoost(bool bBoost)
231    {
232        if(bBoost == this->bBoost_)
233            return;
234
235        if(bBoost)
236            this->boost();
237        else
238        {
239            this->bBoost_ = false;
240        }
241    }
242
243    void SpaceShip::fire()
244    {
245    }
246
247    void SpaceShip::boost()
248    {
249        if(!this->bBoostCooldown_)
250            this->bBoost_ = true;
251    }
252
253    void SpaceShip::loadEngineTemplate()
254    {
255        if (!this->enginetemplate_.empty())
256        {
257            Template* temp = Template::getTemplate(this->enginetemplate_);
258
259            if (temp)
260            {
261                Identifier* identifier = temp->getBaseclassIdentifier();
262
263                if (identifier)
264                {
265                    BaseObject* object = identifier->fabricate(this);
266                    this->engine_ = orxonox_cast<Engine*>(object);
267
268                    if (this->engine_)
269                    {
270                        this->engine_->addTemplate(temp);
271                        this->engine_->addToSpaceShip(this);
272                    }
273                    else
274                    {
275                        object->destroy();
276                    }
277                }
278            }
279        }
280    }
281
282    void SpaceShip::setEngine(Engine* engine)
283    {
284        this->engine_ = engine;
285        if (engine && engine->getShip() != this)
286            engine->addToSpaceShip(this);
287    }
288
289    std::vector<PickupCarrier*>* SpaceShip::getCarrierChildren(void) const
290    {
291        std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>();
292        list->push_back(this->engine_);
293        return list;
294    }
295}
Note: See TracBrowser for help on using the repository browser.