Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/bugger/src/orxonox/objects/worldentities/pawns/SpaceShip.cc @ 2531

Last change on this file since 2531 was 2531, checked in by rgrieder, 15 years ago

Merged revision 2371 to bugger branch.

  • Property svn:eol-style set to native
File size: 7.3 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 "OrxonoxStableHeaders.h"
30#include "SpaceShip.h"
31
32#include "core/CoreIncludes.h"
33#include "core/ConfigValueIncludes.h"
34#include "core/Template.h"
35#include "core/XMLPort.h"
36#include "util/Math.h"
37#include "objects/items/Engine.h"
38
39namespace orxonox
40{
41    CreateFactory(SpaceShip);
42
43    SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator)
44    {
45        RegisterObject(SpaceShip);
46
47        this->zeroDegree_ = 0;
48
49        this->bBoost_ = false;
50        this->steering_ = Vector3::ZERO;
51        this->engine_ = 0;
52
53        this->maxRotation_ = 0;
54        this->rotationAcceleration_ = 0;
55        this->translationDamping_ = 0;
56
57        this->yawRotation_ = 0;
58        this->pitchRotation_ = 0;
59        this->rollRotation_ = 0;
60
61        this->bInvertYAxis_ = false;
62
63        this->setDestroyWhenPlayerLeft(true);
64
65        this->setConfigValues();
66        this->registerVariables();
67    }
68
69    SpaceShip::~SpaceShip()
70    {
71        if (this->isInitialized() && this->engine_)
72            delete this->engine_;
73    }
74
75    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
76    {
77        SUPER(SpaceShip, XMLPort, xmlelement, mode);
78
79        XMLPortParam(SpaceShip, "engine",            setEngineTemplate,    getEngineTemplate,    xmlelement, mode);
80        XMLPortParam(SpaceShip, "maxrotation",       setMaxRotation,       getMaxRotation,       xmlelement, mode);
81        XMLPortParam(SpaceShip, "rotacc",            setRotAcc,            getRotAcc,            xmlelement, mode);
82        XMLPortParam(SpaceShip, "transdamp",         setTransDamp,         getTransDamp,         xmlelement, mode);
83    }
84
85    void SpaceShip::registerVariables()
86    {
87        registerVariable(this->maxRotation_,             variableDirection::toclient);
88        registerVariable(this->rotationAcceleration_,    variableDirection::toclient);
89        registerVariable(this->translationDamping_,      variableDirection::toclient);
90    }
91
92    void SpaceShip::setConfigValues()
93    {
94        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
95    }
96
97    void SpaceShip::tick(float dt)
98    {
99        if (this->hasLocalController())
100        {
101            // #####################################
102            // ############# STEERING ##############
103            // #####################################
104
105            Vector3 velocity = this->getVelocity();
106
107            // normalize velocity and acceleration
108            for (size_t dimension = 0; dimension < 3; ++dimension)
109            {
110                if (this->acceleration_[dimension] == 0)
111                {
112                    if (velocity[dimension] > 0)
113                    {
114                        velocity[dimension] -= (this->translationDamping_ * dt);
115                        if (velocity[dimension] < 0)
116                            velocity[dimension] = 0;
117                    }
118                    else if (velocity[dimension] < 0)
119                    {
120                        velocity[dimension] += (this->translationDamping_ * dt);
121                        if (velocity[dimension] > 0)
122                            velocity[dimension] = 0;
123                    }
124                }
125            }
126
127            this->setVelocity(velocity);
128        }
129
130
131        SUPER(SpaceShip, tick, dt);
132
133
134        if (this->hasLocalController())
135        {
136            if (!this->isInMouseLook())
137            {
138                this->yaw(this->yawRotation_ * dt);
139                if (this->bInvertYAxis_)
140                    this->pitch(Degree(-this->pitchRotation_ * dt));
141                else
142                    this->pitch(Degree( this->pitchRotation_ * dt));
143                this->roll(this->rollRotation_ * dt);
144            }
145
146            this->yawRotation_   = this->zeroDegree_;
147            this->pitchRotation_ = this->zeroDegree_;
148            this->rollRotation_  = this->zeroDegree_;
149        }
150    }
151
152    void SpaceShip::moveFrontBack(const Vector2& value)
153    {
154        this->steering_.z = -value.x;
155    }
156
157    void SpaceShip::moveRightLeft(const Vector2& value)
158    {
159        this->steering_.x = value.x;
160    }
161
162    void SpaceShip::moveUpDown(const Vector2& value)
163    {
164        this->steering_.y = value.x;
165    }
166
167    void SpaceShip::rotateYaw(const Vector2& value)
168    {
169        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
170        if (temp > this->maxRotation_)
171            temp = this->maxRotation_;
172        if (temp < -this->maxRotation_)
173            temp = -this->maxRotation_;
174        this->yawRotation_ = Degree(temp);
175
176        Pawn::rotateYaw(value);
177    }
178
179    void SpaceShip::rotatePitch(const Vector2& value)
180    {
181        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
182        if (temp > this->maxRotation_)
183            temp = this->maxRotation_;
184        if (temp < -this->maxRotation_)
185            temp = -this->maxRotation_;
186        this->pitchRotation_ = Degree(temp);
187
188        Pawn::rotatePitch(value);
189    }
190
191    void SpaceShip::rotateRoll(const Vector2& value)
192    {
193        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
194        if (temp > this->maxRotation_)
195            temp = this->maxRotation_;
196        if (temp < -this->maxRotation_)
197            temp = -this->maxRotation_;
198        this->rollRotation_ = Degree(temp);
199
200        Pawn::rotateRoll(value);
201    }
202
203    void SpaceShip::fire()
204    {
205    }
206
207    void SpaceShip::boost()
208    {
209        this->bBoost_ = true;
210    }
211
212    void SpaceShip::loadEngineTemplate()
213    {
214        if (this->enginetemplate_ != "")
215        {
216            Template* temp = Template::getTemplate(this->enginetemplate_);
217
218            if (temp)
219            {
220                Identifier* identifier = temp->getBaseclassIdentifier();
221
222                if (identifier)
223                {
224                    BaseObject* object = identifier->fabricate(this);
225                    this->engine_ = dynamic_cast<Engine*>(object);
226
227                    if (this->engine_)
228                    {
229                        this->engine_->addTemplate(temp);
230                        this->engine_->addToSpaceShip(this);
231                    }
232                    else
233                    {
234                        delete object;
235                    }
236                }
237            }
238        }
239    }
240
241    void SpaceShip::setEngine(Engine* engine)
242    {
243        this->engine_ = engine;
244        if (engine && engine->getShip() != this)
245            engine->addToSpaceShip(this);
246    }
247}
Note: See TracBrowser for help on using the repository browser.