1 | |
---|
2 | //TODO: Sounds (all the sounds are still from the OrxoBlox module...) |
---|
3 | //TODO: Blocks (the Ball-Block comunication is based on how the blocks are implemented) |
---|
4 | //TODO: The bottom boundary/ the Ball collecter |
---|
5 | //TODO: Ability to shoot the ball (the ball is still constructed like the OrxoBlox ball) |
---|
6 | |
---|
7 | /* |
---|
8 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
9 | * > www.orxonox.net < |
---|
10 | * |
---|
11 | * |
---|
12 | * License notice: |
---|
13 | * |
---|
14 | * This program is free software; you can redistribute it and/or |
---|
15 | * modify it under the terms of the GNU General Public License |
---|
16 | * as published by the Free Software Foundation; either version 2 |
---|
17 | * of the License, or (at your option) any later version. |
---|
18 | * |
---|
19 | * This program is distributed in the hope that it will be useful, |
---|
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | * GNU General Public License for more details. |
---|
23 | * |
---|
24 | * You should have received a copy of the GNU General Public License |
---|
25 | * along with this program; if not, write to the Free Software |
---|
26 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
27 | * |
---|
28 | * Author: |
---|
29 | * Fabian 'x3n' Landau |
---|
30 | * Co-authors: |
---|
31 | * ... |
---|
32 | * |
---|
33 | */ |
---|
34 | |
---|
35 | /** |
---|
36 | @file OrxoBloxBall.cc |
---|
37 | @brief Implementation of the OrxoBloxBall class. |
---|
38 | */ |
---|
39 | |
---|
40 | #include "OrxoBloxBall.h" |
---|
41 | |
---|
42 | #include "core/CoreIncludes.h" |
---|
43 | #include "core/GameMode.h" |
---|
44 | |
---|
45 | #include "gametypes/Gametype.h" |
---|
46 | |
---|
47 | #include "OrxoBloxBlocks.h" |
---|
48 | |
---|
49 | #include "sound/WorldSound.h" |
---|
50 | #include "core/XMLPort.h" |
---|
51 | |
---|
52 | namespace orxonox |
---|
53 | { |
---|
54 | RegisterClass(OrxoBloxBall); |
---|
55 | |
---|
56 | const float OrxoBloxBall::MAX_REL_Z_VELOCITY = 1.5; |
---|
57 | |
---|
58 | /** |
---|
59 | @brief |
---|
60 | Constructor. Registers and initializes the object. |
---|
61 | */ |
---|
62 | OrxoBloxBall::OrxoBloxBall(Context* context) |
---|
63 | : MovableEntity(context) |
---|
64 | { |
---|
65 | RegisterObject(OrxoBloxBall); |
---|
66 | |
---|
67 | this->speed_ = 0; |
---|
68 | //NUMBER OF BLOCKS IS A CONSTANT AT THE MOMENT, DYNAMICAL IMPLEMENTATION WOULD BE NICE |
---|
69 | this->nblocks_ = 20; //number of blocks on the field |
---|
70 | this->blocks_ = new OrxoBloxStone[this.nblocks_]; //the blocks in a block array |
---|
71 | this->fieldWidth_ = 100; |
---|
72 | this->fieldHeight_ = 100; |
---|
73 | |
---|
74 | this->registerVariables(); |
---|
75 | |
---|
76 | //initialize sound |
---|
77 | if (GameMode::isMaster()) |
---|
78 | { |
---|
79 | this->defBoundarySound_ = new WorldSound(this->getContext()); |
---|
80 | this->defBoundarySound_->setVolume(0.5f); |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | this->defBoundarySound_ = nullptr; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | @brief |
---|
90 | Destructor. |
---|
91 | */ |
---|
92 | OrxoBloxBall::~OrxoBloxBall() |
---|
93 | { |
---|
94 | this.destroy(); |
---|
95 | } |
---|
96 | |
---|
97 | //xml port for loading sounds |
---|
98 | void OrxoBloxBall::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
99 | { |
---|
100 | SUPER(OrxoBloxBall, XMLPort, xmlelement, mode); |
---|
101 | XMLPortParam(OrxoBloxBall, "defBoundarySound", setDefBoundarySound, getDefBoundarySound, xmlelement, mode); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | @brief |
---|
106 | Register variables to synchronize over the network. |
---|
107 | */ |
---|
108 | void OrxoBloxBall::registerVariables() |
---|
109 | { |
---|
110 | registerVariable( this->fieldWidth_ ); |
---|
111 | registerVariable( this->fieldHeight_ ); |
---|
112 | registerVariable( this->speed_ ); |
---|
113 | registerVariable( this->nblocks_ ); |
---|
114 | registerVariable( this->blocks_ ); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | @brief |
---|
119 | Is called every tick. |
---|
120 | Handles the movement of the ball and its interaction with the boundaries and blocks. |
---|
121 | @param dt |
---|
122 | The time since the last tick. |
---|
123 | */ |
---|
124 | void OrxoBloxBall::tick(float dt) |
---|
125 | { |
---|
126 | SUPER(OrxoBloxBall, tick, dt); |
---|
127 | |
---|
128 | // Get the current position, velocity and acceleration of the ball. |
---|
129 | Vector3 position = this->getPosition(); |
---|
130 | Vector3 velocity = this->getVelocity(); |
---|
131 | Vector3 acceleration = this->getAcceleration(); |
---|
132 | |
---|
133 | // If the ball has hit the boundaries on either the right side or the left |
---|
134 | if (position.x > this->fieldWidth_ || position.x < 0) |
---|
135 | { |
---|
136 | defBoundarySound_->play(); //play boundary sound |
---|
137 | // Its velocity in x-direction is inverted (i.e. it bounces off). |
---|
138 | velocity.x = -velocity.x; |
---|
139 | // And its position is set as to not overstep the boundary it has just crossed. |
---|
140 | if (position.x > this->fieldWidth_) |
---|
141 | position.x = this->fieldWidth_; |
---|
142 | else if (position.x < 0) |
---|
143 | position.x = 0; |
---|
144 | |
---|
145 | this->fireEvent(); |
---|
146 | } |
---|
147 | |
---|
148 | // If the ball has hit the boundary on the top |
---|
149 | if (position.y > this->fieldHeight_) |
---|
150 | { |
---|
151 | defBoundarySound_->play(); //play boundary sound |
---|
152 | // Its velocity in z-direction is inverted (i.e. it bounces off). |
---|
153 | velocity.y = -velocity.y; |
---|
154 | // And its position is set as to not overstep the boundary it has just crossed. |
---|
155 | position.y = this->fieldHeight_; |
---|
156 | |
---|
157 | this->fireEvent(); |
---|
158 | } |
---|
159 | |
---|
160 | //If the ball collides with a block |
---|
161 | for (int i = 0; i < this.nblocks_ ; i++) { |
---|
162 | |
---|
163 | if ( |
---|
164 | (-position.x + this->blocks[i]_.getLeft()) < 0 || |
---|
165 | (position.x - this->blocks[i]_.getRight()) < 0 || |
---|
166 | (-position.y + this->blocks[i]_.getBottom()) < 0 || |
---|
167 | (position.y - this->blocks[i]_.getTop()) < 0 |
---|
168 | ) { |
---|
169 | int dist_left = position.x - this->blocks[i]_.getLeft(); |
---|
170 | int dist_right = -position.x + this->blocks[i]_.getRight(); |
---|
171 | int dist_bot = position.y - this->blocks[i]_.getBottom(); |
---|
172 | int dist top = -position.y + this->blocks[i]_.getTop(); |
---|
173 | |
---|
174 | if (dist_left < dist_right){ |
---|
175 | if(dist_left < dist_top && dist_left < dist_bot) { |
---|
176 | defBoundarySound_->play(); |
---|
177 | velocity.x = -velocity.x; |
---|
178 | position.x = this->blocks[i]_.getLeft(); |
---|
179 | |
---|
180 | } |
---|
181 | else if(dist_left == dist_top) { |
---|
182 | defBoundarySound_->play(); |
---|
183 | velocity.x = -velocity.x; |
---|
184 | velocity.y = -velocity.y; |
---|
185 | position.x = this->blocks[i]_.getLeft(); |
---|
186 | position.y = this->blocks[i]_.getTop(); |
---|
187 | } |
---|
188 | else if(dist_left == dist_bot) { |
---|
189 | defBoundarySound_->play(); |
---|
190 | velocity.x = -velocity.x; |
---|
191 | velocity.y = -velocity.y; |
---|
192 | position.x = this->blocks[i]_.getLeft(); |
---|
193 | position.y = this->blocks[i]_.getBot(); |
---|
194 | } |
---|
195 | } |
---|
196 | else if (dist_right < dist_left){ |
---|
197 | if(dist_right < dist_top && dist_left < dist_bot) { |
---|
198 | defBoundarySound_->play(); |
---|
199 | velocity.x = -velocity.x; |
---|
200 | position.x = this->blocks[i]_.getRight(); |
---|
201 | |
---|
202 | } |
---|
203 | else if(dist_right == dist_top) { |
---|
204 | defBoundarySound_->play(); |
---|
205 | velocity.x = -velocity.x; |
---|
206 | velocity.y = -velocity.y; |
---|
207 | position.x = this->blocks[i]_.getRight(); |
---|
208 | position.y = this->blocks[i]_.getTop(); |
---|
209 | } |
---|
210 | else if(dist_right == dist_bot) { |
---|
211 | defBoundarySound_->play(); |
---|
212 | velocity.x = -velocity.x; |
---|
213 | velocity.y = -velocity.y; |
---|
214 | position.x = this->blocks[i]_.getRight(); |
---|
215 | position.y = this->blocks[i]_.getBot(); |
---|
216 | } |
---|
217 | } |
---|
218 | else if (dist_bot < dist_top){ |
---|
219 | if(dist_bot < dist_left && dist_bot < dist_right) { |
---|
220 | defBoundarySound_->play(); |
---|
221 | velocity.y = -velocity.y; |
---|
222 | position.y = this->blocks[i]_.getBot(); |
---|
223 | |
---|
224 | } |
---|
225 | else if(dist_bot == dist_left) { |
---|
226 | defBoundarySound_->play(); |
---|
227 | velocity.x = -velocity.x; |
---|
228 | velocity.y = -velocity.y; |
---|
229 | position.x = this->blocks[i]_.getLeft(); |
---|
230 | position.y = this->blocks[i]_.getBot(); |
---|
231 | } |
---|
232 | else if(dist_bot == dist_right) { |
---|
233 | defBoundarySound_->play(); |
---|
234 | velocity.x = -velocity.x; |
---|
235 | velocity.y = -velocity.y; |
---|
236 | position.x = this->blocks[i]_.getRight(); |
---|
237 | position.y = this->blocks[i]_.getBot(); |
---|
238 | } |
---|
239 | } |
---|
240 | else if (dist_top bot){ |
---|
241 | if(dist_top < dist_left && dist_top < dist_right) { |
---|
242 | defBoundarySound_->play(); |
---|
243 | velocity.y = -velocity.y; |
---|
244 | position.y = this->blocks[i]_.getBot(); |
---|
245 | |
---|
246 | } |
---|
247 | else if(dist_top == dist_left) { |
---|
248 | defBoundarySound_->play(); |
---|
249 | velocity.x = -velocity.x; |
---|
250 | velocity.y = -velocity.y; |
---|
251 | position.x = this->blocks[i]_.getLeft(); |
---|
252 | position.y = this->blocks[i]_.getTop(); |
---|
253 | } |
---|
254 | else if(dist_top == dist_right) { |
---|
255 | defBoundarySound_->play(); |
---|
256 | velocity.x = -velocity.x; |
---|
257 | velocity.y = -velocity.y; |
---|
258 | position.x = this->blocks[i]_.getRight(); |
---|
259 | position.y = this->blocks[i]_.getTop(); |
---|
260 | } |
---|
261 | } |
---|
262 | this->blocks[i]_.gotHit() |
---|
263 | this->fireEvent(); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | // If the ball has crossed the bottom boundary |
---|
268 | if (position.y < 0) |
---|
269 | { |
---|
270 | ~OrxoBloxBall(); |
---|
271 | //TODO: THE BALLS NEED TO BE COLLECTED, DESTROYED AND COUNTED... NEW SHOOTING POSITION NEEDS TO BE SET |
---|
272 | } |
---|
273 | |
---|
274 | // Set the position, velocity and acceleration of the ball, if they have changed. |
---|
275 | if (acceleration != this->getAcceleration()) |
---|
276 | this->setAcceleration(acceleration); |
---|
277 | if (velocity != this->getVelocity()) |
---|
278 | this->setVelocity(velocity); |
---|
279 | if (position != this->getPosition()) |
---|
280 | this->setPosition(position); |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | @brief |
---|
285 | Set the speed of the ball (in x,y-direction). |
---|
286 | @param speed |
---|
287 | The speed to be set. |
---|
288 | */ |
---|
289 | void OrxoBloxBall::setSpeed(float speed) |
---|
290 | { |
---|
291 | if (speed != this->speed_) // If the speed changes |
---|
292 | { |
---|
293 | this->speed_ = speed; |
---|
294 | |
---|
295 | // Set the speed in the direction of the balls current velocity. |
---|
296 | Vector3 velocity = this->getVelocity(); |
---|
297 | if (velocity.y != 0) |
---|
298 | velocity.y = sgn(velocity.y) * this->speed_; |
---|
299 | else // If the balls current velocity is zero, the speed is set in a random direction. |
---|
300 | velocity.y = this->speed_ * sgn(rnd(-1,1)); |
---|
301 | |
---|
302 | if (velocity.x != 0) |
---|
303 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
304 | else // If the balls current velocity is zero, the speed is set in a random direction. |
---|
305 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
306 | |
---|
307 | this->setVelocity(velocity); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | @brief |
---|
313 | Set the blocks for the ball. |
---|
314 | @param bats |
---|
315 | An array (of size n (n=#Blocks) of weak pointers, to be set as the new blocks. |
---|
316 | */ |
---|
317 | void OrxoBloxBall::setBlock(OrxoBloxStone[] blocks, int n) |
---|
318 | { |
---|
319 | if(n > this.nblocks_) { |
---|
320 | this.nblocks = n; |
---|
321 | delete this.blocks_; |
---|
322 | this.nblocks_ = new OrxoBloxStone[this.nblocks_]; |
---|
323 | } |
---|
324 | for (int i = 0; i < n; i++) { |
---|
325 | this->blocks_[i] = blocks[i]; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound) |
---|
330 | { |
---|
331 | if( defBoundarySound_ ) |
---|
332 | defBoundarySound_->setSource(OrxoBloxSound); |
---|
333 | else |
---|
334 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
335 | } |
---|
336 | |
---|
337 | const std::string& OrxoBloxBall::getDefBoundarySound() |
---|
338 | { |
---|
339 | if( defBoundarySound_ ) |
---|
340 | return defBoundarySound_->getSource(); |
---|
341 | else |
---|
342 | assert(0); |
---|
343 | return BLANKSTRING; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | |
---|