1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * ... |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "Ogre.h" |
---|
29 | #include "spaceship_steering.h" |
---|
30 | #include <iostream> |
---|
31 | using namespace Ogre; |
---|
32 | |
---|
33 | SpaceshipSteering::SpaceshipSteering(float maxSpeedForward, float |
---|
34 | maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float |
---|
35 | maxSpeedLoopRightLeft) { |
---|
36 | |
---|
37 | std::cout << "Steering was loaded" << std::endl; |
---|
38 | moveForward_ = 0; |
---|
39 | rotateUp_ = 0; |
---|
40 | rotateDown_ = 0; |
---|
41 | rotateRight_ = 0; |
---|
42 | rotateLeft_ = 0; |
---|
43 | loopRight_ = 0; |
---|
44 | loopLeft_ = 0; |
---|
45 | brakeForward_ = 0; |
---|
46 | brakeRotate_ = 0; |
---|
47 | brakeLoop_ = 0; |
---|
48 | speedForward_ = 0; |
---|
49 | speedRotateUpDown_ = 0; |
---|
50 | speedRotateRightLeft_ = 0; |
---|
51 | speedLoopRightLeft_ = 0; |
---|
52 | maxSpeedForward_ = maxSpeedForward; |
---|
53 | maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
54 | maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
55 | maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
56 | accelerationForward_ = 0; |
---|
57 | accelerationRotateUpDown_ = 0; |
---|
58 | accelerationRotateRightLeft_ = 0; |
---|
59 | accelerationLoopRightLeft_ = 0; |
---|
60 | } |
---|
61 | |
---|
62 | void SpaceshipSteering::tick(float time) { |
---|
63 | |
---|
64 | if(moveForward_ > 0) { |
---|
65 | accelerationForward_ = moveForward_; |
---|
66 | if(speedForward_ < maxSpeedForward_) |
---|
67 | speedForward_ += accelerationForward_*time; |
---|
68 | if(speedForward_ > maxSpeedForward_) |
---|
69 | speedForward_ = maxSpeedForward_; |
---|
70 | } |
---|
71 | if(moveForward_ <= 0) { |
---|
72 | accelerationForward_ = brakeForward_; |
---|
73 | if(speedForward_ > 0) |
---|
74 | speedForward_ -= accelerationForward_*time; |
---|
75 | if(speedForward_ < 0) |
---|
76 | speedForward_ = 0; |
---|
77 | } |
---|
78 | |
---|
79 | if(rotateUp_ > 0) { |
---|
80 | accelerationRotateUpDown_ = rotateUp_; |
---|
81 | if(speedRotateUpDown_ < maxSpeedRotateUpDown_) |
---|
82 | speedRotateUpDown_ += accelerationRotateUpDown_*time; |
---|
83 | if(speedRotateUpDown_ > maxSpeedRotateUpDown_) |
---|
84 | speedRotateUpDown_ = maxSpeedRotateUpDown_; |
---|
85 | } |
---|
86 | if(rotateDown_ > 0) { |
---|
87 | accelerationRotateUpDown_ = rotateDown_; |
---|
88 | if(speedRotateUpDown_ > -maxSpeedRotateUpDown_) |
---|
89 | speedRotateUpDown_ -= accelerationRotateUpDown_*time; |
---|
90 | if(speedRotateUpDown_ < -maxSpeedRotateUpDown_) |
---|
91 | speedRotateUpDown_ = -maxSpeedRotateUpDown_; |
---|
92 | } |
---|
93 | if(rotateUp_ == 0 && rotateDown_ == 0) { |
---|
94 | accelerationRotateUpDown_ = brakeRotate_; |
---|
95 | if(speedRotateUpDown_ > 0) |
---|
96 | speedRotateUpDown_ -= accelerationRotateUpDown_*time; |
---|
97 | if(speedRotateUpDown_ < 0) |
---|
98 | speedRotateUpDown_ += accelerationRotateUpDown_*time; |
---|
99 | if(abs(speedRotateUpDown_)<accelerationRotateUpDown_*time) |
---|
100 | speedRotateUpDown_ = 0; |
---|
101 | } |
---|
102 | |
---|
103 | if(rotateRight_ > 0) { |
---|
104 | accelerationRotateRightLeft_ = rotateRight_; |
---|
105 | if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_) |
---|
106 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*time; |
---|
107 | if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_) |
---|
108 | speedRotateRightLeft_ = -maxSpeedRotateRightLeft_; |
---|
109 | } |
---|
110 | if(rotateLeft_ > 0) { |
---|
111 | accelerationRotateRightLeft_ = rotateLeft_; |
---|
112 | if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_) |
---|
113 | speedRotateRightLeft_ += accelerationRotateRightLeft_*time; |
---|
114 | if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_) |
---|
115 | speedRotateRightLeft_ = maxSpeedRotateRightLeft_; |
---|
116 | } |
---|
117 | if(rotateRight_ == 0 && rotateLeft_ == 0) { |
---|
118 | accelerationRotateRightLeft_ = brakeRotate_; |
---|
119 | if(speedRotateRightLeft_ > 0) |
---|
120 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*time; |
---|
121 | if(speedRotateRightLeft_ < 0) |
---|
122 | speedRotateRightLeft_ += accelerationRotateRightLeft_*time; |
---|
123 | if(abs(speedRotateRightLeft_)<accelerationRotateRightLeft_*time) |
---|
124 | speedRotateRightLeft_ = 0; |
---|
125 | } |
---|
126 | |
---|
127 | if(loopRight_ > 0) { |
---|
128 | accelerationLoopRightLeft_ = loopRight_; |
---|
129 | if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_) |
---|
130 | speedLoopRightLeft_ += accelerationLoopRightLeft_*time; |
---|
131 | if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_) |
---|
132 | speedLoopRightLeft_ = maxSpeedLoopRightLeft_; |
---|
133 | } |
---|
134 | if(loopLeft_ > 0) { |
---|
135 | accelerationLoopRightLeft_ = loopLeft_; |
---|
136 | if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_) |
---|
137 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*time; |
---|
138 | if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_) |
---|
139 | speedLoopRightLeft_ = -maxSpeedLoopRightLeft_; |
---|
140 | } |
---|
141 | if(loopLeft_ == 0 && loopRight_ == 0) { |
---|
142 | accelerationLoopRightLeft_ = brakeLoop_; |
---|
143 | if(speedLoopRightLeft_ > 0) |
---|
144 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*time; |
---|
145 | if(speedLoopRightLeft_ < 0) |
---|
146 | speedLoopRightLeft_ += accelerationLoopRightLeft_*time; |
---|
147 | if(abs(speedLoopRightLeft_)<accelerationLoopRightLeft_*time) |
---|
148 | speedLoopRightLeft_ = 0; |
---|
149 | } |
---|
150 | |
---|
151 | Vector3 transVector = Vector3::ZERO; |
---|
152 | transVector.z = 1; |
---|
153 | steeringNode_->translate(transVector*speedForward_*time, |
---|
154 | Node::TS_LOCAL); |
---|
155 | steeringNode_->pitch(Degree(speedRotateUpDown_*time), |
---|
156 | Node::TS_LOCAL); |
---|
157 | steeringNode_->yaw(Degree(speedRotateRightLeft_*time), |
---|
158 | Node::TS_LOCAL); |
---|
159 | steeringNode_->roll(Degree(speedLoopRightLeft_*time), |
---|
160 | Node::TS_LOCAL); |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | void SpaceshipSteering::moveForward(float moveForward) { |
---|
165 | moveForward_ = moveForward; |
---|
166 | } |
---|
167 | |
---|
168 | void SpaceshipSteering::rotateUp(float rotateUp) { |
---|
169 | rotateUp_ = rotateUp; |
---|
170 | } |
---|
171 | |
---|
172 | void SpaceshipSteering::rotateDown(float rotateDown) { |
---|
173 | rotateDown_ = rotateDown; |
---|
174 | } |
---|
175 | |
---|
176 | void SpaceshipSteering::rotateLeft(float rotateLeft) { |
---|
177 | rotateLeft_ = rotateLeft; |
---|
178 | } |
---|
179 | |
---|
180 | void SpaceshipSteering::rotateRight(float rotateRight) { |
---|
181 | rotateRight_ = rotateRight; |
---|
182 | } |
---|
183 | |
---|
184 | void SpaceshipSteering::loopLeft(float loopLeft) { |
---|
185 | loopLeft_ = loopLeft; |
---|
186 | } |
---|
187 | |
---|
188 | void SpaceshipSteering::loopRight(float loopRight) { |
---|
189 | loopRight_ = loopRight; |
---|
190 | } |
---|
191 | |
---|
192 | void SpaceshipSteering::brakeForward(float brakeForward) { |
---|
193 | brakeForward_ = brakeForward; |
---|
194 | } |
---|
195 | |
---|
196 | void SpaceshipSteering::brakeRotate(float brakeRotate) { |
---|
197 | brakeRotate_ = brakeRotate; |
---|
198 | } |
---|
199 | |
---|
200 | void SpaceshipSteering::brakeLoop(float brakeLoop) { |
---|
201 | brakeLoop_ = brakeLoop; |
---|
202 | } |
---|
203 | |
---|
204 | void SpaceshipSteering::maxSpeedForward(float maxSpeedForward) { |
---|
205 | maxSpeedForward_ = maxSpeedForward; |
---|
206 | } |
---|
207 | |
---|
208 | void SpaceshipSteering::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) { |
---|
209 | maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
210 | } |
---|
211 | |
---|
212 | void SpaceshipSteering::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) { |
---|
213 | maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
214 | } |
---|
215 | |
---|
216 | void SpaceshipSteering::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) { |
---|
217 | maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
218 | } |
---|
219 | |
---|
220 | void SpaceshipSteering::addNode(Ogre::SceneNode *steeringNode) { |
---|
221 | steeringNode_ = steeringNode; |
---|
222 | } |
---|