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 | |
---|
39 | namespace 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 | REGISTERDATA(this->maxRotation_, 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->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 | this->yaw(this->yawRotation_ * dt); |
---|
137 | if (this->bInvertYAxis_) |
---|
138 | this->pitch(Degree(-this->pitchRotation_ * dt)); |
---|
139 | else |
---|
140 | this->pitch(Degree( this->pitchRotation_ * dt)); |
---|
141 | this->roll(this->rollRotation_ * dt); |
---|
142 | |
---|
143 | this->yawRotation_ = this->zeroDegree_; |
---|
144 | this->pitchRotation_ = this->zeroDegree_; |
---|
145 | this->rollRotation_ = this->zeroDegree_; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | void SpaceShip::moveFrontBack(const Vector2& value) |
---|
150 | { |
---|
151 | this->steering_.z = -value.x; |
---|
152 | } |
---|
153 | |
---|
154 | void SpaceShip::moveRightLeft(const Vector2& value) |
---|
155 | { |
---|
156 | this->steering_.x = value.x; |
---|
157 | } |
---|
158 | |
---|
159 | void SpaceShip::moveUpDown(const Vector2& value) |
---|
160 | { |
---|
161 | this->steering_.y = value.x; |
---|
162 | } |
---|
163 | |
---|
164 | void SpaceShip::rotateYaw(const Vector2& value) |
---|
165 | { |
---|
166 | Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; |
---|
167 | if (temp > this->maxRotation_) |
---|
168 | temp = this->maxRotation_; |
---|
169 | if (temp < -this->maxRotation_) |
---|
170 | temp = -this->maxRotation_; |
---|
171 | this->yawRotation_ = Degree(temp); |
---|
172 | } |
---|
173 | |
---|
174 | void SpaceShip::rotatePitch(const Vector2& value) |
---|
175 | { |
---|
176 | Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; |
---|
177 | if (temp > this->maxRotation_) |
---|
178 | temp = this->maxRotation_; |
---|
179 | if (temp < -this->maxRotation_) |
---|
180 | temp = -this->maxRotation_; |
---|
181 | this->pitchRotation_ = Degree(temp); |
---|
182 | } |
---|
183 | |
---|
184 | void SpaceShip::rotateRoll(const Vector2& value) |
---|
185 | { |
---|
186 | Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; |
---|
187 | if (temp > this->maxRotation_) |
---|
188 | temp = this->maxRotation_; |
---|
189 | if (temp < -this->maxRotation_) |
---|
190 | temp = -this->maxRotation_; |
---|
191 | this->rollRotation_ = Degree(temp); |
---|
192 | } |
---|
193 | |
---|
194 | void SpaceShip::fire() |
---|
195 | { |
---|
196 | } |
---|
197 | |
---|
198 | void SpaceShip::boost() |
---|
199 | { |
---|
200 | this->bBoost_ = true; |
---|
201 | } |
---|
202 | |
---|
203 | void SpaceShip::loadEngineTemplate() |
---|
204 | { |
---|
205 | if (this->enginetemplate_ != "") |
---|
206 | { |
---|
207 | Template* temp = Template::getTemplate(this->enginetemplate_); |
---|
208 | |
---|
209 | if (temp) |
---|
210 | { |
---|
211 | Identifier* identifier = temp->getBaseclassIdentifier(); |
---|
212 | |
---|
213 | if (identifier) |
---|
214 | { |
---|
215 | BaseObject* object = identifier->fabricate(this); |
---|
216 | this->engine_ = dynamic_cast<Engine*>(object); |
---|
217 | |
---|
218 | if (this->engine_) |
---|
219 | { |
---|
220 | this->engine_->addTemplate(temp); |
---|
221 | this->engine_->addToSpaceShip(this); |
---|
222 | } |
---|
223 | else |
---|
224 | { |
---|
225 | delete object; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | void SpaceShip::setEngine(Engine* engine) |
---|
233 | { |
---|
234 | this->engine_ = engine; |
---|
235 | if (engine && engine->getShip() != this) |
---|
236 | engine->addToSpaceShip(this); |
---|
237 | } |
---|
238 | } |
---|