Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/pawns/SpaceShip.cc @ 2518

Last change on this file since 2518 was 2171, checked in by landauf, 16 years ago

merged revisions 2111-2170 from objecthierarchy branch back to trunk.

  • Property svn:eol-style set to native
File size: 7.2 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/XMLPort.h"
35#include "util/Math.h"
36
37namespace orxonox
38{
39    CreateFactory(SpaceShip);
40
41    SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator)
42    {
43        RegisterObject(SpaceShip);
44
45        this->zeroDegree_ = 0;
46
47        this->maxSpeed_ = 0;
48        this->maxSecondarySpeed_ = 0;
49        this->maxRotation_ = 0;
50        this->translationAcceleration_ = 0;
51        this->rotationAcceleration_ = 0;
52        this->translationDamping_ = 0;
53
54        this->yawRotation_ = 0;
55        this->pitchRotation_ = 0;
56        this->rollRotation_ = 0;
57
58        this->bInvertYAxis_ = false;
59
60        this->setDestroyWhenPlayerLeft(true);
61
62        this->setConfigValues();
63        this->registerVariables();
64    }
65
66    SpaceShip::~SpaceShip()
67    {
68    }
69
70    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
71    {
72        SUPER(SpaceShip, XMLPort, xmlelement, mode);
73
74        XMLPortParam(SpaceShip, "maxspeed",          setMaxSpeed,          getMaxSpeed,          xmlelement, mode);
75        XMLPortParam(SpaceShip, "maxsecondaryspeed", setMaxSecondarySpeed, getMaxSecondarySpeed, xmlelement, mode);
76        XMLPortParam(SpaceShip, "maxrotation",       setMaxRotation,       getMaxRotation,       xmlelement, mode);
77        XMLPortParam(SpaceShip, "transacc",          setTransAcc,          getTransAcc,          xmlelement, mode);
78        XMLPortParam(SpaceShip, "rotacc",            setRotAcc,            getRotAcc,            xmlelement, mode);
79        XMLPortParam(SpaceShip, "transdamp",         setTransDamp,         getTransDamp,         xmlelement, mode);
80    }
81
82    void SpaceShip::registerVariables()
83    {
84        REGISTERDATA(this->maxSpeed_,                direction::toclient);
85        REGISTERDATA(this->maxSecondarySpeed_,       direction::toclient);
86        REGISTERDATA(this->maxRotation_,             direction::toclient);
87        REGISTERDATA(this->translationAcceleration_, direction::toclient);
88        REGISTERDATA(this->rotationAcceleration_,    direction::toclient);
89        REGISTERDATA(this->translationDamping_,      direction::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->isLocallyControlled())
100        {
101            // #####################################
102            // ############# STEERING ##############
103            // #####################################
104
105            Vector3 velocity = this->getVelocity();
106            if (velocity.x > this->maxSecondarySpeed_)
107                velocity.x = this->maxSecondarySpeed_;
108            if (velocity.x < -this->maxSecondarySpeed_)
109                velocity.x = -this->maxSecondarySpeed_;
110            if (velocity.y > this->maxSecondarySpeed_)
111                velocity.y = this->maxSecondarySpeed_;
112            if (velocity.y < -this->maxSecondarySpeed_)
113                velocity.y = -this->maxSecondarySpeed_;
114            if (velocity.z > this->maxSecondarySpeed_)
115                velocity.z = this->maxSecondarySpeed_;
116            if (velocity.z < -this->maxSpeed_)
117                velocity.z = -this->maxSpeed_;
118
119            // normalize velocity and acceleration
120            for (size_t dimension = 0; dimension < 3; ++dimension)
121            {
122                if (this->acceleration_[dimension] == 0)
123                {
124                    if (velocity[dimension] > 0)
125                    {
126                        velocity[dimension] -= (this->translationDamping_ * dt);
127                        if (velocity[dimension] < 0)
128                            velocity[dimension] = 0;
129                    }
130                    else if (velocity[dimension] < 0)
131                    {
132                        velocity[dimension] += (this->translationDamping_ * dt);
133                        if (velocity[dimension] > 0)
134                            velocity[dimension] = 0;
135                    }
136                }
137            }
138
139            this->setVelocity(velocity);
140        }
141
142
143        SUPER(SpaceShip, tick, dt);
144
145
146        if (this->isLocallyControlled())
147        {
148            this->yaw(this->yawRotation_ * dt);
149            if (this->bInvertYAxis_)
150                this->pitch(Degree(-this->pitchRotation_ * dt));
151            else
152                this->pitch(Degree( this->pitchRotation_ * dt));
153            this->roll(this->rollRotation_ * dt);
154
155            this->acceleration_.x = 0;
156            this->acceleration_.y = 0;
157            this->acceleration_.z = 0;
158
159            this->yawRotation_   = this->zeroDegree_;
160            this->pitchRotation_ = this->zeroDegree_;
161            this->rollRotation_  = this->zeroDegree_;
162        }
163    }
164
165    void SpaceShip::moveFrontBack(const Vector2& value)
166    {
167        this->acceleration_.z = -this->translationAcceleration_ * value.x;
168    }
169
170    void SpaceShip::moveRightLeft(const Vector2& value)
171    {
172        this->acceleration_.x = this->translationAcceleration_ * value.x;
173    }
174
175    void SpaceShip::moveUpDown(const Vector2& value)
176    {
177        this->acceleration_.y = this->translationAcceleration_ * value.x;
178    }
179
180    void SpaceShip::rotateYaw(const Vector2& value)
181    {
182        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
183        if (temp > this->maxRotation_)
184            temp = this->maxRotation_;
185        if (temp < -this->maxRotation_)
186            temp = -this->maxRotation_;
187        this->yawRotation_ = Degree(temp);
188    }
189
190    void SpaceShip::rotatePitch(const Vector2& value)
191    {
192        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
193        if (temp > this->maxRotation_)
194            temp = this->maxRotation_;
195        if (temp < -this->maxRotation_)
196            temp = -this->maxRotation_;
197        this->pitchRotation_ = Degree(temp);
198    }
199
200    void SpaceShip::rotateRoll(const Vector2& value)
201    {
202        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
203        if (temp > this->maxRotation_)
204            temp = this->maxRotation_;
205        if (temp < -this->maxRotation_)
206            temp = -this->maxRotation_;
207        this->rollRotation_ = Degree(temp);
208    }
209
210    void SpaceShip::fire()
211    {
212    }
213}
Note: See TracBrowser for help on using the repository browser.