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 | * Benjamin Knecht |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "SpaceShip.h" |
---|
31 | |
---|
32 | #include <string> |
---|
33 | |
---|
34 | #include <OgreCamera.h> |
---|
35 | #include <OgreRenderWindow.h> |
---|
36 | #include <OgreParticleSystem.h> |
---|
37 | #include <OgreSceneNode.h> |
---|
38 | |
---|
39 | #include "CameraHandler.h" |
---|
40 | #include "tinyxml/tinyxml.h" |
---|
41 | #include "ois/OIS.h" |
---|
42 | #include "util/Convert.h" |
---|
43 | #include "util/Math.h" |
---|
44 | #include "core/CoreIncludes.h" |
---|
45 | #include "core/ConfigValueIncludes.h" |
---|
46 | #include "core/Debug.h" |
---|
47 | #include "GraphicsEngine.h" |
---|
48 | #include "core/InputManager.h" |
---|
49 | #include "particle/ParticleInterface.h" |
---|
50 | #include "Projectile.h" |
---|
51 | #include "core/XMLPort.h" |
---|
52 | #include "core/ConsoleCommand.h" |
---|
53 | #include "network/Client.h" |
---|
54 | |
---|
55 | namespace orxonox |
---|
56 | { |
---|
57 | ConsoleCommand(SpaceShip, setMaxSpeedTest, AccessLevel::Debug, false); |
---|
58 | ConsoleCommandGeneric(test1, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxSpeed", AccessLevel::Debug), false); |
---|
59 | ConsoleCommandGeneric(test2, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxBlubber", AccessLevel::Debug), false); |
---|
60 | ConsoleCommandGeneric(test3, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setRofl", AccessLevel::Debug), false); |
---|
61 | |
---|
62 | CreateFactory(SpaceShip); |
---|
63 | |
---|
64 | SpaceShip* SpaceShip::instance_s; |
---|
65 | |
---|
66 | SpaceShip::SpaceShip() : |
---|
67 | //testvector_(0,0,0), |
---|
68 | //bInvertYAxis_(false), |
---|
69 | setMouseEventCallback_(false), |
---|
70 | bLMousePressed_(false), |
---|
71 | bRMousePressed_(false), |
---|
72 | camNode_(0), |
---|
73 | cam_(0), |
---|
74 | camName_("CamNode"), |
---|
75 | tt_(0), |
---|
76 | redNode_(0), |
---|
77 | greenNode_(0), |
---|
78 | blinkTime_(0.0f), |
---|
79 | chNearNode_(0), |
---|
80 | chFarNode_(0), |
---|
81 | timeToReload_(0.0f), |
---|
82 | //reloadTime_(0.0f), |
---|
83 | maxSideAndBackSpeed_(0.0f), |
---|
84 | maxSpeed_(0.0f), |
---|
85 | maxRotation_(0.0f), |
---|
86 | translationAcceleration_(0.0f), |
---|
87 | rotationAcceleration_(0.0f), |
---|
88 | translationDamping_(0.0f), |
---|
89 | rotationDamping_(0.0f), |
---|
90 | maxRotationRadian_(0), |
---|
91 | rotationAccelerationRadian_(0), |
---|
92 | rotationDampingRadian_(0), |
---|
93 | zeroRadian_(0), |
---|
94 | mouseXRotation_(0), |
---|
95 | mouseYRotation_(0), |
---|
96 | mouseX_(0.0f), |
---|
97 | mouseY_(0.0f), |
---|
98 | emitterRate_(0.0f), |
---|
99 | server_(false) |
---|
100 | { |
---|
101 | RegisterObject(SpaceShip); |
---|
102 | this->registerAllVariables(); |
---|
103 | |
---|
104 | SpaceShip::instance_s = this; |
---|
105 | |
---|
106 | this->setConfigValues(); |
---|
107 | |
---|
108 | |
---|
109 | this->setRotationAxis(1, 0, 0); |
---|
110 | this->setStatic(false); |
---|
111 | |
---|
112 | COUT(3) << "Info: SpaceShip was loaded" << std::endl; |
---|
113 | } |
---|
114 | |
---|
115 | SpaceShip::~SpaceShip() |
---|
116 | { |
---|
117 | if (this->tt_) |
---|
118 | delete this->tt_; |
---|
119 | if(setMouseEventCallback_) |
---|
120 | InputManager::removeMouseHandler("SpaceShip"); |
---|
121 | if (this->cam_) |
---|
122 | delete this->cam_; |
---|
123 | } |
---|
124 | |
---|
125 | bool SpaceShip::create(){ |
---|
126 | if(Model::create()) |
---|
127 | this->init(); |
---|
128 | else |
---|
129 | return false; |
---|
130 | return true; |
---|
131 | } |
---|
132 | |
---|
133 | void SpaceShip::registerAllVariables(){ |
---|
134 | registerVar( &camName_, camName_.length()+1, network::STRING, 0x1); |
---|
135 | registerVar( &maxSpeed_, sizeof(maxSpeed_), network::DATA, 0x1); |
---|
136 | registerVar( &maxSideAndBackSpeed_, sizeof(maxSideAndBackSpeed_), network::DATA, 0x1); |
---|
137 | registerVar( &maxRotation_, sizeof(maxRotation_), network::DATA, 0x1); |
---|
138 | registerVar( &translationAcceleration_, sizeof(translationAcceleration_), network::DATA, 0x1); |
---|
139 | registerVar( &rotationAcceleration_, sizeof(rotationAcceleration_), network::DATA, 0x1); |
---|
140 | registerVar( &rotationAccelerationRadian_, sizeof(rotationAccelerationRadian_), network::DATA, 0x1); |
---|
141 | registerVar( &translationDamping_, sizeof(translationDamping_), network::DATA, 0x1); |
---|
142 | registerVar( &rotationDamping_, sizeof(rotationDamping_), network::DATA, 0x1); |
---|
143 | registerVar( &rotationDampingRadian_, sizeof(rotationDampingRadian_), network::DATA, 0x1); |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | void SpaceShip::init() |
---|
148 | { |
---|
149 | if ((server_ || ( network::Client::getSingleton() && network::Client::getSingleton()->getShipID()==objectID ) )) |
---|
150 | { |
---|
151 | if (!setMouseEventCallback_) |
---|
152 | { |
---|
153 | InputManager::addMouseHandler(this, "SpaceShip"); |
---|
154 | setMouseEventCallback_ = true; |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | // START CREATING THRUSTER |
---|
159 | this->tt_ = new ParticleInterface(GraphicsEngine::getSingleton().getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow"); |
---|
160 | this->tt_->getParticleSystem()->setParameter("local_space","true"); |
---|
161 | this->tt_->newEmitter(); |
---|
162 | /* |
---|
163 | this->tt_->setDirection(Vector3(0,0,1)); |
---|
164 | this->tt_->setPositionOfEmitter(0, Vector3(20,-1,-15)); |
---|
165 | this->tt_->setPositionOfEmitter(1, Vector3(-20,-1,-15)); |
---|
166 | */ |
---|
167 | this->tt_->setDirection(Vector3(-1,0,0)); |
---|
168 | this->tt_->setPositionOfEmitter(0, Vector3(-15,20,-1)); |
---|
169 | this->tt_->setPositionOfEmitter(1, Vector3(-15,-20,-1)); |
---|
170 | this->tt_->setVelocity(50); |
---|
171 | |
---|
172 | emitterRate_ = tt_->getRate(); |
---|
173 | |
---|
174 | Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2"); |
---|
175 | node2->setInheritScale(false); |
---|
176 | tt_->addToSceneNode(node2); |
---|
177 | // END CREATING THRUSTER |
---|
178 | |
---|
179 | // START CREATING BLINKING LIGHTS |
---|
180 | this->redBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); |
---|
181 | this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1); |
---|
182 | |
---|
183 | this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.0, -0.3)); |
---|
184 | this->redNode_->setInheritScale(false); |
---|
185 | this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.0, -0.3)); |
---|
186 | this->greenNode_->setInheritScale(false); |
---|
187 | |
---|
188 | this->redNode_->attachObject(this->redBillboard_.getBillboardSet()); |
---|
189 | this->redNode_->setScale(0.3, 0.3, 0.3); |
---|
190 | |
---|
191 | this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet()); |
---|
192 | this->greenNode_->setScale(0.3, 0.3, 0.3); |
---|
193 | // END CREATING BLINKING LIGHTS |
---|
194 | |
---|
195 | // START of testing crosshair |
---|
196 | this->crosshairNear_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
197 | this->crosshairFar_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1); |
---|
198 | |
---|
199 | this->chNearNode_ = this->getNode()->createChildSceneNode(this->getName() + "near", Vector3(50.0, 0.0, 0.0)); |
---|
200 | this->chNearNode_->setInheritScale(false); |
---|
201 | this->chFarNode_ = this->getNode()->createChildSceneNode(this->getName() + "far", Vector3(200.0, 0.0, 0.0)); |
---|
202 | this->chFarNode_->setInheritScale(false); |
---|
203 | |
---|
204 | this->chNearNode_->attachObject(this->crosshairNear_.getBillboardSet()); |
---|
205 | this->chNearNode_->setScale(0.2, 0.2, 0.2); |
---|
206 | |
---|
207 | this->chFarNode_->attachObject(this->crosshairFar_.getBillboardSet()); |
---|
208 | this->chFarNode_->setScale(0.4, 0.4, 0.4); |
---|
209 | |
---|
210 | createCamera(); |
---|
211 | // END of testing crosshair |
---|
212 | } |
---|
213 | |
---|
214 | void SpaceShip::setConfigValues() |
---|
215 | { |
---|
216 | SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); |
---|
217 | SetConfigValue(reloadTime_, 0.125).description("The reload time of the weapon in seconds"); |
---|
218 | SetConfigValue(testvector_, Vector3()).description("asdfblah"); |
---|
219 | } |
---|
220 | |
---|
221 | void SpaceShip::loadParams(TiXmlElement* xmlElem) |
---|
222 | { |
---|
223 | Model::loadParams(xmlElem); |
---|
224 | this->create(); |
---|
225 | this->getFocus(); |
---|
226 | /* |
---|
227 | if (xmlElem->Attribute("forward") && xmlElem->Attribute("rotateupdown") && xmlElem->Attribute("rotaterightleft") && xmlElem->Attribute("looprightleft")) |
---|
228 | { |
---|
229 | std::string forwardStr = xmlElem->Attribute("forward"); |
---|
230 | std::string rotateupdownStr = xmlElem->Attribute("rotateupdown"); |
---|
231 | std::string rotaterightleftStr = xmlElem->Attribute("rotaterightleft"); |
---|
232 | std::string looprightleftStr = xmlElem->Attribute("looprightleft"); |
---|
233 | |
---|
234 | String2Number<float>(this->maxSpeedForward_, forwardStr); |
---|
235 | String2Number<float>(this->maxSpeedRotateUpDown_, rotateupdownStr); |
---|
236 | String2Number<float>(this->maxSpeedRotateRightLeft_, rotaterightleftStr); |
---|
237 | String2Number<float>(this->maxSpeedLoopRightLeft_, looprightleftStr); |
---|
238 | |
---|
239 | COUT(4) << "Loader: Initialized spaceship steering with values " << maxSpeedForward_ << " " << maxSpeedRotateUpDown_ << " " << maxSpeedRotateRightLeft_ << " " << maxSpeedLoopRightLeft_ << " " << std::endl; |
---|
240 | } |
---|
241 | */ |
---|
242 | if (xmlElem->Attribute("maxSpeed") && xmlElem->Attribute("maxSideAndBackSpeed") && xmlElem->Attribute("maxRotation") && xmlElem->Attribute("transAcc") && xmlElem->Attribute("rotAcc") && xmlElem->Attribute("transDamp") && xmlElem->Attribute("rotDamp")) |
---|
243 | { |
---|
244 | |
---|
245 | std::string msStr = xmlElem->Attribute("maxSpeed"); |
---|
246 | std::string msabsStr = xmlElem->Attribute("maxSideAndBackSpeed"); |
---|
247 | std::string mrStr = xmlElem->Attribute("maxRotation"); |
---|
248 | std::string taStr = xmlElem->Attribute("transAcc"); |
---|
249 | std::string raStr = xmlElem->Attribute("rotAcc"); |
---|
250 | std::string tdStr = xmlElem->Attribute("transDamp"); |
---|
251 | std::string rdStr = xmlElem->Attribute("rotDamp"); |
---|
252 | |
---|
253 | convertValue<std::string, float>(&this->maxSpeed_, msStr); |
---|
254 | convertValue<std::string, float>(&this->maxSideAndBackSpeed_, msabsStr); |
---|
255 | convertValue<std::string, float>(&this->maxRotation_, mrStr); |
---|
256 | convertValue<std::string, float>(&this->translationAcceleration_, taStr); |
---|
257 | convertValue<std::string, float>(&this->rotationAcceleration_, raStr); |
---|
258 | convertValue<std::string, float>(&this->translationDamping_, tdStr); |
---|
259 | convertValue<std::string, float>(&this->rotationDamping_, rdStr); |
---|
260 | |
---|
261 | this->maxRotationRadian_ = Radian(this->maxRotation_); |
---|
262 | this->rotationAccelerationRadian_ = Radian(this->rotationAcceleration_); |
---|
263 | this->rotationDampingRadian_ = Radian(this->rotationDamping_); |
---|
264 | |
---|
265 | COUT(4) << "Loader: Initialized SpaceShip" << std::endl; |
---|
266 | } |
---|
267 | |
---|
268 | if (xmlElem->Attribute("camera")) |
---|
269 | { |
---|
270 | this->setCamera(); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | void SpaceShip::setCamera(const std::string& camera) |
---|
275 | { |
---|
276 | camName_=camera; |
---|
277 | // change camera attributes here, if you want to ;) |
---|
278 | } |
---|
279 | |
---|
280 | void SpaceShip::getFocus(){ |
---|
281 | COUT(4) << "requesting focus" << std::endl; |
---|
282 | if(network::Client::getSingleton()==0 || network::Client::getSingleton()->getShipID()==objectID) |
---|
283 | CameraHandler::getInstance()->requestFocus(cam_); |
---|
284 | |
---|
285 | } |
---|
286 | |
---|
287 | void SpaceShip::createCamera(){ |
---|
288 | // COUT(4) << "begin camera creation" << std::endl; |
---|
289 | this->camNode_ = this->getNode()->createChildSceneNode(camName_); |
---|
290 | COUT(4) << "position: (this)" << this->getNode()->getPosition() << std::endl; |
---|
291 | this->camNode_->setPosition(Vector3(-50,0,10)); |
---|
292 | Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0)); |
---|
293 | Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(0,0,-1)); |
---|
294 | camNode_->setOrientation(q1*q2); |
---|
295 | COUT(4) << "position: (cam)" << this->camNode_->getPosition() << std::endl; |
---|
296 | cam_ = new Camera(this->camNode_); |
---|
297 | |
---|
298 | cam_->setTargetNode(this->getNode()); |
---|
299 | // cam->setPosition(Vector3(0,-350,0)); |
---|
300 | if(network::Client::getSingleton()!=0 && network::Client::getSingleton()->getShipID()==objectID){ |
---|
301 | this->setBacksync(true); |
---|
302 | CameraHandler::getInstance()->requestFocus(cam_); |
---|
303 | } |
---|
304 | |
---|
305 | } |
---|
306 | |
---|
307 | void SpaceShip::setMaxSpeed(float value) |
---|
308 | { this->maxSpeed_ = value; } |
---|
309 | void SpaceShip::setMaxSideAndBackSpeed(float value) |
---|
310 | { this->maxSideAndBackSpeed_ = value; } |
---|
311 | void SpaceShip::setMaxRotation(float value) |
---|
312 | { this->maxRotation_ = value; this->maxRotationRadian_ = Radian(value); } |
---|
313 | void SpaceShip::setTransAcc(float value) |
---|
314 | { this->translationAcceleration_ = value; } |
---|
315 | void SpaceShip::setRotAcc(float value) |
---|
316 | { this->rotationAcceleration_ = value; this->rotationAccelerationRadian_ = Radian(value); } |
---|
317 | void SpaceShip::setTransDamp(float value) |
---|
318 | { this->translationDamping_ = value; } |
---|
319 | void SpaceShip::setRotDamp(float value) |
---|
320 | { this->rotationDamping_ = value; this->rotationDampingRadian_ = Radian(value); } |
---|
321 | |
---|
322 | /** |
---|
323 | @brief XML loading and saving. |
---|
324 | @param xmlelement The XML-element |
---|
325 | @param loading Loading (true) or saving (false) |
---|
326 | @return The XML-element |
---|
327 | */ |
---|
328 | void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
329 | { |
---|
330 | Model::XMLPort(xmlelement, mode); |
---|
331 | |
---|
332 | XMLPortParamLoadOnly(SpaceShip, "camera", setCamera, xmlelement, mode); |
---|
333 | XMLPortParamLoadOnly(SpaceShip, "maxSpeed", setMaxSpeed, xmlelement, mode); |
---|
334 | XMLPortParamLoadOnly(SpaceShip, "maxSideAndBackSpeed", setMaxSideAndBackSpeed, xmlelement, mode); |
---|
335 | XMLPortParamLoadOnly(SpaceShip, "maxRotation", setMaxRotation, xmlelement, mode); |
---|
336 | XMLPortParamLoadOnly(SpaceShip, "transAcc", setTransAcc, xmlelement, mode); |
---|
337 | XMLPortParamLoadOnly(SpaceShip, "rotAcc", setRotAcc, xmlelement, mode); |
---|
338 | XMLPortParamLoadOnly(SpaceShip, "transDamp", setTransDamp, xmlelement, mode); |
---|
339 | XMLPortParamLoadOnly(SpaceShip, "rotDamp", setRotDamp, xmlelement, mode); |
---|
340 | server_=true; // TODO: this is only a hack |
---|
341 | SpaceShip::create(); |
---|
342 | getFocus(); |
---|
343 | } |
---|
344 | |
---|
345 | int sgn(float x) |
---|
346 | { |
---|
347 | if (x >= 0) |
---|
348 | return 1; |
---|
349 | else |
---|
350 | return -1; |
---|
351 | } |
---|
352 | |
---|
353 | bool SpaceShip::mouseMoved(const MouseState& state) |
---|
354 | { |
---|
355 | /* |
---|
356 | this->mouseX += e.state.X.rel; |
---|
357 | if (this->bInvertMouse_) |
---|
358 | this->mouseY += e.state.Y.rel; |
---|
359 | else |
---|
360 | this->mouseY -= e.state.Y.rel; |
---|
361 | |
---|
362 | // if(mouseX>maxMouseX) maxMouseX = mouseX; |
---|
363 | // if(mouseX<minMouseX) minMouseX = mouseX; |
---|
364 | // cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
---|
365 | |
---|
366 | this->moved = true; |
---|
367 | */ |
---|
368 | if (this->bRMousePressed_) |
---|
369 | { |
---|
370 | this->camNode_->roll(Degree(-state.X.rel * 0.10)); |
---|
371 | this->camNode_->yaw(Degree(state.Y.rel * 0.10)); |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | float minDimension = state.height; |
---|
376 | if (state.width < minDimension) |
---|
377 | minDimension = state.width; |
---|
378 | |
---|
379 | this->mouseX_ += state.X.rel; |
---|
380 | if (this->mouseX_ < -minDimension) |
---|
381 | this->mouseX_ = -minDimension; |
---|
382 | if (this->mouseX_ > minDimension) |
---|
383 | this->mouseX_ = minDimension; |
---|
384 | |
---|
385 | this->mouseY_ += state.Y.rel; |
---|
386 | if (this->mouseY_ < -minDimension) |
---|
387 | this->mouseY_ = -minDimension; |
---|
388 | if (this->mouseY_ > minDimension) |
---|
389 | this->mouseY_ = minDimension; |
---|
390 | |
---|
391 | float xRotation = this->mouseX_ / minDimension; |
---|
392 | xRotation = xRotation*xRotation * sgn(xRotation); |
---|
393 | xRotation *= -this->rotationAcceleration_; |
---|
394 | if (xRotation > this->maxRotation_) |
---|
395 | xRotation = this->maxRotation_; |
---|
396 | if (xRotation < -this->maxRotation_) |
---|
397 | xRotation = -this->maxRotation_; |
---|
398 | this->mouseXRotation_ = Radian(xRotation); |
---|
399 | |
---|
400 | float yRotation = this->mouseY_ / minDimension; |
---|
401 | yRotation = yRotation*yRotation * sgn(yRotation); |
---|
402 | yRotation *= this->rotationAcceleration_; |
---|
403 | if (yRotation > this->maxRotation_) |
---|
404 | yRotation = this->maxRotation_; |
---|
405 | if (yRotation < -this->maxRotation_) |
---|
406 | yRotation = -this->maxRotation_; |
---|
407 | this->mouseYRotation_ = Radian(yRotation); |
---|
408 | } |
---|
409 | |
---|
410 | return true; |
---|
411 | } |
---|
412 | |
---|
413 | bool SpaceShip::mouseButtonPressed(const MouseState& state, MouseButton::Enum id) |
---|
414 | { |
---|
415 | if (id == MouseButton::Left) |
---|
416 | this->bLMousePressed_ = true; |
---|
417 | else if (id == MouseButton::Right) |
---|
418 | this->bRMousePressed_ = true; |
---|
419 | |
---|
420 | return true; |
---|
421 | } |
---|
422 | |
---|
423 | bool SpaceShip::mouseButtonReleased(const MouseState& state, MouseButton::Enum id) |
---|
424 | { |
---|
425 | if (id == MouseButton::Left) |
---|
426 | this->bLMousePressed_ = false; |
---|
427 | else if (id == MouseButton::Right) |
---|
428 | { |
---|
429 | this->bRMousePressed_ = false; |
---|
430 | this->camNode_->resetOrientation(); |
---|
431 | } |
---|
432 | |
---|
433 | return true; |
---|
434 | } |
---|
435 | |
---|
436 | void SpaceShip::tick(float dt) |
---|
437 | { |
---|
438 | if (this->cam_) |
---|
439 | this->cam_->tick(dt); |
---|
440 | |
---|
441 | if (this->redNode_ && this->greenNode_) |
---|
442 | { |
---|
443 | this->blinkTime_ += dt; |
---|
444 | float redScale = 0.15 + 0.15 * sin(this->blinkTime_ * 10.0); |
---|
445 | float greenScale = 0.15 - 0.15 * sin(this->blinkTime_ * 10.0); |
---|
446 | this->redNode_->setScale(redScale, redScale, redScale); |
---|
447 | this->greenNode_->setScale(greenScale, greenScale, greenScale); |
---|
448 | } |
---|
449 | |
---|
450 | if (this->timeToReload_ > 0) |
---|
451 | this->timeToReload_ -= dt; |
---|
452 | else |
---|
453 | this->timeToReload_ = 0; |
---|
454 | |
---|
455 | if (this->bLMousePressed_ && this->timeToReload_ <= 0) |
---|
456 | { |
---|
457 | Projectile *p = new Projectile(this); |
---|
458 | p->setBacksync(true); |
---|
459 | this->timeToReload_ = this->reloadTime_; |
---|
460 | } |
---|
461 | |
---|
462 | |
---|
463 | // ##################################### |
---|
464 | // ############# STEERING ############## |
---|
465 | // ##################################### |
---|
466 | |
---|
467 | if (this->velocity_.x > this->maxSpeed_) |
---|
468 | this->velocity_.x = this->maxSpeed_; |
---|
469 | if (this->velocity_.x < -this->maxSideAndBackSpeed_) |
---|
470 | this->velocity_.x = -this->maxSideAndBackSpeed_; |
---|
471 | if (this->velocity_.y > this->maxSideAndBackSpeed_) |
---|
472 | this->velocity_.y = this->maxSideAndBackSpeed_; |
---|
473 | if (this->velocity_.y < -this->maxSideAndBackSpeed_) |
---|
474 | this->velocity_.y = -this->maxSideAndBackSpeed_; |
---|
475 | if (this->rotationRate_ > this->maxRotationRadian_) |
---|
476 | this->rotationRate_ = this->maxRotationRadian_; |
---|
477 | if (this->rotationRate_ < -this->maxRotationRadian_) |
---|
478 | this->rotationRate_ = -this->maxRotationRadian_; |
---|
479 | |
---|
480 | if (this->acceleration_.x == 0) |
---|
481 | { |
---|
482 | if (this->velocity_.x > 0) |
---|
483 | { |
---|
484 | this->velocity_.x -= (this->translationDamping_ * dt); |
---|
485 | if (this->velocity_.x < 0) |
---|
486 | this->velocity_.x = 0; |
---|
487 | } |
---|
488 | else if (this->velocity_.x < 0) |
---|
489 | { |
---|
490 | this->velocity_.x += (this->translationDamping_ * dt); |
---|
491 | if (this->velocity_.x > 0) |
---|
492 | this->velocity_.x = 0; |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | if (this->acceleration_.y == 0) |
---|
497 | { |
---|
498 | if (this->velocity_.y > 0) |
---|
499 | { |
---|
500 | this->velocity_.y -= (this->translationDamping_ * dt); |
---|
501 | if (this->velocity_.y < 0) |
---|
502 | this->velocity_.y = 0; |
---|
503 | } |
---|
504 | else if (this->velocity_.y < 0) |
---|
505 | { |
---|
506 | this->velocity_.y += (this->translationDamping_ * dt); |
---|
507 | if (this->velocity_.y > 0) |
---|
508 | this->velocity_.y = 0; |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | if (this->momentum_ == this->zeroRadian_) |
---|
513 | { |
---|
514 | if (this->rotationRate_ > this->zeroRadian_) |
---|
515 | { |
---|
516 | this->rotationRate_ -= (this->rotationDampingRadian_ * dt); |
---|
517 | if (this->rotationRate_ < this->zeroRadian_) |
---|
518 | this->rotationRate_ = 0; |
---|
519 | } |
---|
520 | else if (this->rotationRate_ < this->zeroRadian_) |
---|
521 | { |
---|
522 | this->rotationRate_ += (this->rotationDampingRadian_ * dt); |
---|
523 | if (this->rotationRate_ > this->zeroRadian_) |
---|
524 | this->rotationRate_ = 0; |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | if( (network::Client::getSingleton() && network::Client::getSingleton()->getShipID() == objectID) || server_ ) |
---|
529 | { |
---|
530 | COUT(4) << "steering our ship: " << objectID << std::endl; |
---|
531 | if (InputManager::isKeyDown(KeyCode::Up) || InputManager::isKeyDown(KeyCode::W)) |
---|
532 | this->acceleration_.x = this->translationAcceleration_; |
---|
533 | else if(InputManager::isKeyDown(KeyCode::Down) || InputManager::isKeyDown(KeyCode::S)) |
---|
534 | this->acceleration_.x = -this->translationAcceleration_; |
---|
535 | else |
---|
536 | this->acceleration_.x = 0; |
---|
537 | |
---|
538 | if (InputManager::isKeyDown(KeyCode::Right) || InputManager::isKeyDown(KeyCode::D)) |
---|
539 | this->acceleration_.y = -this->translationAcceleration_; |
---|
540 | else if (InputManager::isKeyDown(KeyCode::Left) || InputManager::isKeyDown(KeyCode::A)) |
---|
541 | this->acceleration_.y = this->translationAcceleration_; |
---|
542 | else |
---|
543 | this->acceleration_.y = 0; |
---|
544 | |
---|
545 | if (InputManager::isKeyDown(KeyCode::Delete) || InputManager::isKeyDown(KeyCode::Q)) |
---|
546 | this->momentum_ = Radian(-this->rotationAccelerationRadian_); |
---|
547 | else if (InputManager::isKeyDown(KeyCode::PageDown) || InputManager::isKeyDown(KeyCode::E)) |
---|
548 | this->momentum_ = Radian(this->rotationAccelerationRadian_); |
---|
549 | else |
---|
550 | this->momentum_ = 0; |
---|
551 | }/*else |
---|
552 | COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/ |
---|
553 | |
---|
554 | WorldEntity::tick(dt); |
---|
555 | |
---|
556 | this->roll(this->mouseXRotation_ * dt); |
---|
557 | if (this->bInvertYAxis_) |
---|
558 | this->yaw(Radian(-this->mouseYRotation_ * dt)); |
---|
559 | else |
---|
560 | this->yaw(Radian(this->mouseYRotation_ * dt)); |
---|
561 | |
---|
562 | if (this->acceleration_.x > 0) |
---|
563 | this->tt_->setRate(emitterRate_); |
---|
564 | else |
---|
565 | this->tt_->setRate(0); |
---|
566 | |
---|
567 | /* |
---|
568 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
569 | this->moveForward(speed); |
---|
570 | else |
---|
571 | this->moveForward(0); |
---|
572 | |
---|
573 | if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
574 | this->brakeForward(speed); |
---|
575 | else |
---|
576 | this->brakeForward(speed/10); |
---|
577 | |
---|
578 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
579 | this->loopRight(loop); |
---|
580 | else |
---|
581 | this->loopRight(0); |
---|
582 | |
---|
583 | if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
584 | this->loopLeft(loop); |
---|
585 | else |
---|
586 | this->loopLeft(0); |
---|
587 | |
---|
588 | if(moved) |
---|
589 | { |
---|
590 | if (mouseY<=0) |
---|
591 | this->rotateUp(-mouseY*rotate); |
---|
592 | if (mouseY>0) |
---|
593 | this->rotateDown(mouseY*rotate); |
---|
594 | if (mouseX>0) |
---|
595 | this->rotateRight(mouseX*rotate); |
---|
596 | if (mouseX<=0) |
---|
597 | this->rotateLeft(-mouseX*rotate); |
---|
598 | |
---|
599 | mouseY = 0; |
---|
600 | mouseX = 0; |
---|
601 | moved = false; |
---|
602 | }*/ |
---|
603 | /* else |
---|
604 | { |
---|
605 | this->rotateUp(0); |
---|
606 | this->rotateDown(0); |
---|
607 | this->rotateRight(0); |
---|
608 | this->rotateLeft(0); |
---|
609 | }*/ |
---|
610 | /* |
---|
611 | if(moveForward_ > 0) |
---|
612 | { |
---|
613 | accelerationForward_ = moveForward_; |
---|
614 | if(speedForward_ < maxSpeedForward_) |
---|
615 | speedForward_ += accelerationForward_*dt; |
---|
616 | if(speedForward_ > maxSpeedForward_) |
---|
617 | speedForward_ = maxSpeedForward_; |
---|
618 | } |
---|
619 | |
---|
620 | if(moveForward_ <= 0) |
---|
621 | { |
---|
622 | accelerationForward_ = -brakeForward_; |
---|
623 | if(speedForward_ > 0) |
---|
624 | speedForward_ += accelerationForward_*dt; |
---|
625 | if(speedForward_ < 0) |
---|
626 | speedForward_ = 0; |
---|
627 | } |
---|
628 | |
---|
629 | if(rotateUp_ > 0) |
---|
630 | { |
---|
631 | accelerationRotateUpDown_ = rotateUp_; |
---|
632 | if(speedRotateUpDown_ < maxSpeedRotateUpDown_) |
---|
633 | speedRotateUpDown_ += accelerationRotateUpDown_*dt; |
---|
634 | if(speedRotateUpDown_ > maxSpeedRotateUpDown_) |
---|
635 | speedRotateUpDown_ = maxSpeedRotateUpDown_; |
---|
636 | } |
---|
637 | |
---|
638 | if(rotateDown_ > 0) |
---|
639 | { |
---|
640 | accelerationRotateUpDown_ = rotateDown_; |
---|
641 | if(speedRotateUpDown_ > -maxSpeedRotateUpDown_) |
---|
642 | speedRotateUpDown_ -= accelerationRotateUpDown_*dt; |
---|
643 | if(speedRotateUpDown_ < -maxSpeedRotateUpDown_) |
---|
644 | speedRotateUpDown_ = -maxSpeedRotateUpDown_; |
---|
645 | } |
---|
646 | |
---|
647 | if(rotateUp_ == 0 && rotateDown_ == 0) |
---|
648 | { |
---|
649 | accelerationRotateUpDown_ = brakeRotate_; |
---|
650 | if(speedRotateUpDown_ > 0) |
---|
651 | speedRotateUpDown_ -= accelerationRotateUpDown_*dt; |
---|
652 | if(speedRotateUpDown_ < 0) |
---|
653 | speedRotateUpDown_ += accelerationRotateUpDown_*dt; |
---|
654 | if(fabs(speedRotateUpDown_) < accelerationRotateUpDown_*dt) |
---|
655 | speedRotateUpDown_ = 0; |
---|
656 | } |
---|
657 | |
---|
658 | if(rotateRight_ > 0) |
---|
659 | { |
---|
660 | accelerationRotateRightLeft_ = rotateRight_; |
---|
661 | if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_) |
---|
662 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt; |
---|
663 | if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_) |
---|
664 | speedRotateRightLeft_ = -maxSpeedRotateRightLeft_; |
---|
665 | } |
---|
666 | |
---|
667 | if(rotateLeft_ > 0) |
---|
668 | { |
---|
669 | accelerationRotateRightLeft_ = rotateLeft_; |
---|
670 | if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_) |
---|
671 | speedRotateRightLeft_ += accelerationRotateRightLeft_*dt; |
---|
672 | if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_) |
---|
673 | speedRotateRightLeft_ = maxSpeedRotateRightLeft_; |
---|
674 | } |
---|
675 | |
---|
676 | if(rotateRight_ == 0 && rotateLeft_ == 0) |
---|
677 | { |
---|
678 | accelerationRotateRightLeft_ = brakeRotate_; |
---|
679 | if(speedRotateRightLeft_ > 0) |
---|
680 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt; |
---|
681 | if(speedRotateRightLeft_ < 0) |
---|
682 | speedRotateRightLeft_ += accelerationRotateRightLeft_*dt; |
---|
683 | if(fabs(speedRotateRightLeft_) < accelerationRotateRightLeft_*dt) |
---|
684 | speedRotateRightLeft_ = 0; |
---|
685 | } |
---|
686 | |
---|
687 | if(loopRight_ > 0) |
---|
688 | { |
---|
689 | accelerationLoopRightLeft_ = loopRight_; |
---|
690 | if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_) |
---|
691 | speedLoopRightLeft_ += accelerationLoopRightLeft_*dt; |
---|
692 | if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_) |
---|
693 | speedLoopRightLeft_ = maxSpeedLoopRightLeft_; |
---|
694 | } |
---|
695 | |
---|
696 | if(loopLeft_ > 0) |
---|
697 | { |
---|
698 | accelerationLoopRightLeft_ = loopLeft_; |
---|
699 | if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_) |
---|
700 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt; |
---|
701 | if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_) |
---|
702 | speedLoopRightLeft_ = -maxSpeedLoopRightLeft_; |
---|
703 | } |
---|
704 | |
---|
705 | if(loopLeft_ == 0 && loopRight_ == 0) |
---|
706 | { |
---|
707 | accelerationLoopRightLeft_ = brakeLoop_; |
---|
708 | if(speedLoopRightLeft_ > 0) |
---|
709 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt; |
---|
710 | if(speedLoopRightLeft_ < 0) |
---|
711 | speedLoopRightLeft_ += accelerationLoopRightLeft_*dt; |
---|
712 | if(fabs(speedLoopRightLeft_) < accelerationLoopRightLeft_*dt) |
---|
713 | speedLoopRightLeft_ = 0; |
---|
714 | } |
---|
715 | |
---|
716 | Vector3 transVector = Vector3::ZERO; |
---|
717 | */ |
---|
718 | /* |
---|
719 | transVector.z = 1; |
---|
720 | this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL); |
---|
721 | this->pitch(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL); |
---|
722 | this->yaw(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
723 | this->roll(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
724 | */ |
---|
725 | /* |
---|
726 | transVector.x = 1; |
---|
727 | this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL); |
---|
728 | this->yaw(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL); |
---|
729 | this->roll(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
730 | this->pitch(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
731 | */ |
---|
732 | } |
---|
733 | /* |
---|
734 | void SpaceShip::moveForward(float moveForward) { |
---|
735 | moveForward_ = moveForward; |
---|
736 | } |
---|
737 | |
---|
738 | void SpaceShip::rotateUp(float rotateUp) { |
---|
739 | rotateUp_ = rotateUp; |
---|
740 | } |
---|
741 | |
---|
742 | void SpaceShip::rotateDown(float rotateDown) { |
---|
743 | rotateDown_ = rotateDown; |
---|
744 | } |
---|
745 | |
---|
746 | void SpaceShip::rotateLeft(float rotateLeft) { |
---|
747 | rotateLeft_ = rotateLeft; |
---|
748 | } |
---|
749 | |
---|
750 | void SpaceShip::rotateRight(float rotateRight) { |
---|
751 | rotateRight_ = rotateRight; |
---|
752 | } |
---|
753 | |
---|
754 | void SpaceShip::loopLeft(float loopLeft) { |
---|
755 | loopLeft_ = loopLeft; |
---|
756 | } |
---|
757 | |
---|
758 | void SpaceShip::loopRight(float loopRight) { |
---|
759 | loopRight_ = loopRight; |
---|
760 | } |
---|
761 | |
---|
762 | void SpaceShip::brakeForward(float brakeForward) { |
---|
763 | brakeForward_ = brakeForward; |
---|
764 | } |
---|
765 | |
---|
766 | void SpaceShip::brakeRotate(float brakeRotate) { |
---|
767 | brakeRotate_ = brakeRotate; |
---|
768 | } |
---|
769 | |
---|
770 | void SpaceShip::brakeLoop(float brakeLoop) { |
---|
771 | brakeLoop_ = brakeLoop; |
---|
772 | } |
---|
773 | |
---|
774 | void SpaceShip::maxSpeedForward(float maxSpeedForward) { |
---|
775 | maxSpeedForward_ = maxSpeedForward; |
---|
776 | } |
---|
777 | |
---|
778 | void SpaceShip::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) { |
---|
779 | maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
780 | } |
---|
781 | |
---|
782 | void SpaceShip::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) { |
---|
783 | maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
784 | } |
---|
785 | |
---|
786 | void SpaceShip::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) { |
---|
787 | maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
788 | } |
---|
789 | */ |
---|
790 | } |
---|