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 | /** |
---|
30 | @file OrxoBloxBall.cc |
---|
31 | @brief Implementation of the OrxoBloxBall class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "OrxoBloxBall.h" |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/GameMode.h" |
---|
38 | |
---|
39 | #include "gametypes/Gametype.h" |
---|
40 | |
---|
41 | #include "OrxoBloxBat.h" |
---|
42 | |
---|
43 | #include "sound/WorldSound.h" |
---|
44 | #include "core/XMLPort.h" |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | RegisterClass(OrxoBloxBall); |
---|
49 | |
---|
50 | const float OrxoBloxBall::MAX_REL_Z_VELOCITY = 1.5; |
---|
51 | |
---|
52 | /** |
---|
53 | @brief |
---|
54 | Constructor. Registers and initializes the object. |
---|
55 | */ |
---|
56 | OrxoBloxBall::OrxoBloxBall(Context* context) |
---|
57 | : MovableEntity(context) |
---|
58 | { |
---|
59 | RegisterObject(OrxoBloxBall); |
---|
60 | |
---|
61 | this->speed_ = 0; |
---|
62 | this->accelerationFactor_ = 1.0f; |
---|
63 | this->bat_ = nullptr; |
---|
64 | this->bDeleteBats_ = false; |
---|
65 | this->batID_ = new unsigned int[2]; |
---|
66 | this->batID_[0] = OBJECTID_UNKNOWN; |
---|
67 | this->batID_[1] = OBJECTID_UNKNOWN; |
---|
68 | this->relMercyOffset_ = 0.05f; |
---|
69 | |
---|
70 | this->registerVariables(); |
---|
71 | |
---|
72 | //initialize sound |
---|
73 | if (GameMode::isMaster()) |
---|
74 | { |
---|
75 | this->defScoreSound_ = new WorldSound(this->getContext()); |
---|
76 | this->defScoreSound_->setVolume(1.0f); |
---|
77 | this->defBatSound_ = new WorldSound(this->getContext()); |
---|
78 | this->defBatSound_->setVolume(0.4f); |
---|
79 | this->defBoundarySound_ = new WorldSound(this->getContext()); |
---|
80 | this->defBoundarySound_->setVolume(0.5f); |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | this->defScoreSound_ = nullptr; |
---|
85 | this->defBatSound_ = nullptr; |
---|
86 | this->defBoundarySound_ = nullptr; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | @brief |
---|
92 | Destructor. |
---|
93 | */ |
---|
94 | OrxoBloxBall::~OrxoBloxBall() |
---|
95 | { |
---|
96 | if (this->isInitialized()) |
---|
97 | { |
---|
98 | if (this->bDeleteBats_) |
---|
99 | delete[] this->bat_; |
---|
100 | |
---|
101 | delete[] this->batID_; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | //xml port for loading sounds |
---|
106 | void OrxoBloxBall::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
107 | { |
---|
108 | SUPER(OrxoBloxBall, XMLPort, xmlelement, mode); |
---|
109 | XMLPortParam(OrxoBloxBall, "defScoreSound", setDefScoreSound, getDefScoreSound, xmlelement, mode); |
---|
110 | XMLPortParam(OrxoBloxBall, "defBatSound", setDefBatSound, getDefBatSound, xmlelement, mode); |
---|
111 | XMLPortParam(OrxoBloxBall, "defBoundarySound", setDefBoundarySound, getDefBoundarySound, xmlelement, mode); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | @brief |
---|
116 | Register variables to synchronize over the network. |
---|
117 | */ |
---|
118 | void OrxoBloxBall::registerVariables() |
---|
119 | { |
---|
120 | registerVariable( this->fieldWidth_ ); |
---|
121 | registerVariable( this->fieldHeight_ ); |
---|
122 | registerVariable( this->batlength_ ); |
---|
123 | registerVariable( this->speed_ ); |
---|
124 | registerVariable( this->relMercyOffset_ ); |
---|
125 | registerVariable( this->batID_[0] ); |
---|
126 | registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<OrxoBloxBall>( this, &OrxoBloxBall::applyBats) ); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | @brief |
---|
131 | Is called every tick. |
---|
132 | Handles the movement of the ball and its interaction with the boundaries and bats. |
---|
133 | @param dt |
---|
134 | The time since the last tick. |
---|
135 | */ |
---|
136 | void OrxoBloxBall::tick(float dt) |
---|
137 | { |
---|
138 | SUPER(OrxoBloxBall, tick, dt); |
---|
139 | |
---|
140 | // Get the current position, velocity and acceleration of the ball. |
---|
141 | Vector3 position = this->getPosition(); |
---|
142 | Vector3 velocity = this->getVelocity(); |
---|
143 | Vector3 acceleration = this->getAcceleration(); |
---|
144 | |
---|
145 | // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters). |
---|
146 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
147 | { |
---|
148 | defBoundarySound_->play(); //play boundary sound |
---|
149 | // Its velocity in z-direction is inverted (i.e. it bounces off). |
---|
150 | velocity.z = -velocity.z; |
---|
151 | // And its position is set as to not overstep the boundary it has just crossed. |
---|
152 | if (position.z > this->fieldHeight_ / 2) |
---|
153 | position.z = this->fieldHeight_ / 2; |
---|
154 | if (position.z < -this->fieldHeight_ / 2) |
---|
155 | position.z = -this->fieldHeight_ / 2; |
---|
156 | |
---|
157 | this->fireEvent(); |
---|
158 | } |
---|
159 | |
---|
160 | // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry). |
---|
161 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
162 | { |
---|
163 | float distance = 0; |
---|
164 | |
---|
165 | if (this->bat_ != nullptr) // If there are bats. |
---|
166 | { |
---|
167 | // If the right boundary has been crossed. |
---|
168 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != nullptr) |
---|
169 | { |
---|
170 | // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%) |
---|
171 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
172 | |
---|
173 | |
---|
174 | if (fabs(distance) <= 1) // If the bat is there to parry. |
---|
175 | { |
---|
176 | defBatSound_->play(); //play bat sound |
---|
177 | // Set the ball to be exactly at the boundary. |
---|
178 | position.x = this->fieldWidth_ / 2; |
---|
179 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
180 | velocity.x = -velocity.x; |
---|
181 | // Adjust the velocity in the z-direction, depending on where the ball hit the bat. |
---|
182 | velocity.z = distance * distance * sgn(distance) * OrxoBloxBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
183 | acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; |
---|
184 | |
---|
185 | this->fireEvent(); |
---|
186 | } |
---|
187 | |
---|
188 | // If the left player scores. |
---|
189 | else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
190 | { |
---|
191 | ChatManager::message("You suck!!"); |
---|
192 | defScoreSound_->play();//play score sound |
---|
193 | if (this->getGametype() && this->bat_[0]) |
---|
194 | { |
---|
195 | ChatManager::message("You suck!!"); |
---|
196 | |
---|
197 | |
---|
198 | this->getGametype()->playerScored(this->bat_[0]->getPlayer()); |
---|
199 | return; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | } |
---|
208 | // If the left boundary has been crossed. |
---|
209 | else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != nullptr) |
---|
210 | { |
---|
211 | // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%) |
---|
212 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
213 | if (fabs(distance) <= 1) // If the bat is there to parry. |
---|
214 | { |
---|
215 | defBatSound_->play(); //play bat sound |
---|
216 | // Set the ball to be exactly at the boundary. |
---|
217 | position.x = -this->fieldWidth_ / 2; |
---|
218 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
219 | velocity.x = -velocity.x; |
---|
220 | // Adjust the velocity in the z-direction, depending on where the ball hit the bat. |
---|
221 | velocity.z = distance * distance * sgn(distance) * OrxoBloxBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
222 | acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; |
---|
223 | |
---|
224 | this->fireEvent(); |
---|
225 | } |
---|
226 | // If the right player scores. |
---|
227 | else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
228 | { |
---|
229 | defScoreSound_->play();//play score sound |
---|
230 | if (this->getGametype() && this->bat_[1]) |
---|
231 | { |
---|
232 | ChatManager::message("You suck!!"); |
---|
233 | |
---|
234 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
---|
235 | return; |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | // Set the position, velocity and acceleration of the ball, if they have changed. |
---|
243 | if (acceleration != this->getAcceleration()) |
---|
244 | this->setAcceleration(acceleration); |
---|
245 | if (velocity != this->getVelocity()) |
---|
246 | this->setVelocity(velocity); |
---|
247 | if (position != this->getPosition()) |
---|
248 | this->setPosition(position); |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | @brief |
---|
253 | Set the speed of the ball (in x-direction). |
---|
254 | @param speed |
---|
255 | The speed to be set. |
---|
256 | */ |
---|
257 | void OrxoBloxBall::setSpeed(float speed) |
---|
258 | { |
---|
259 | if (speed != this->speed_) // If the speed changes |
---|
260 | { |
---|
261 | this->speed_ = speed; |
---|
262 | |
---|
263 | // Set the speed in the direction of the balls current velocity. |
---|
264 | Vector3 velocity = this->getVelocity(); |
---|
265 | if (velocity.x != 0) |
---|
266 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
267 | else // If the balls current velocity is zero, the speed is set in a random direction. |
---|
268 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
269 | |
---|
270 | this->setVelocity(velocity); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | @brief |
---|
276 | Set the bats for the ball. |
---|
277 | @param bats |
---|
278 | An array (of size 2) of weak pointers, to be set as the new bats. |
---|
279 | */ |
---|
280 | void OrxoBloxBall::setBats(WeakPtr<OrxoBloxBat>* bats) |
---|
281 | { |
---|
282 | if (this->bDeleteBats_) // If there are already some bats, delete them. |
---|
283 | { |
---|
284 | delete[] this->bat_; |
---|
285 | this->bDeleteBats_ = false; |
---|
286 | } |
---|
287 | |
---|
288 | this->bat_ = bats; |
---|
289 | // Also store their object IDs, for synchronization. |
---|
290 | this->batID_[0] = this->bat_[0]->getObjectID(); |
---|
291 | this->batID_[1] = this->bat_[1]->getObjectID(); |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | @brief |
---|
296 | Get the bats over the network. |
---|
297 | */ |
---|
298 | void OrxoBloxBall::applyBats() |
---|
299 | { |
---|
300 | // Make space for the bats, if they don't exist, yet. |
---|
301 | if (this->bat_ == nullptr) |
---|
302 | { |
---|
303 | this->bat_ = new WeakPtr<OrxoBloxBat>[2]; |
---|
304 | this->bDeleteBats_ = true; |
---|
305 | } |
---|
306 | |
---|
307 | if (this->batID_[0] != OBJECTID_UNKNOWN) |
---|
308 | this->bat_[0] = orxonox_cast<OrxoBloxBat*>(Synchronisable::getSynchronisable(this->batID_[0])); |
---|
309 | if (this->batID_[1] != OBJECTID_UNKNOWN) |
---|
310 | this->bat_[1] = orxonox_cast<OrxoBloxBat*>(Synchronisable::getSynchronisable(this->batID_[1])); |
---|
311 | } |
---|
312 | |
---|
313 | void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound) |
---|
314 | { |
---|
315 | if( defScoreSound_ ) |
---|
316 | defScoreSound_->setSource(OrxoBloxSound); |
---|
317 | else |
---|
318 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
319 | } |
---|
320 | |
---|
321 | const std::string& OrxoBloxBall::getDefScoreSound() |
---|
322 | { |
---|
323 | if( defScoreSound_ ) |
---|
324 | return defScoreSound_->getSource(); |
---|
325 | else |
---|
326 | assert(0); |
---|
327 | return BLANKSTRING; |
---|
328 | } |
---|
329 | |
---|
330 | void OrxoBloxBall::setDefBatSound(const std::string &OrxoBloxSound) |
---|
331 | { |
---|
332 | if( defBatSound_ ) |
---|
333 | defBatSound_->setSource(OrxoBloxSound); |
---|
334 | else |
---|
335 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
336 | } |
---|
337 | |
---|
338 | const std::string& OrxoBloxBall::getDefBatSound() |
---|
339 | { |
---|
340 | if( defBatSound_ ) |
---|
341 | return defBatSound_->getSource(); |
---|
342 | else |
---|
343 | assert(0); |
---|
344 | return BLANKSTRING; |
---|
345 | } |
---|
346 | |
---|
347 | void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound) |
---|
348 | { |
---|
349 | if( defBoundarySound_ ) |
---|
350 | defBoundarySound_->setSource(OrxoBloxSound); |
---|
351 | else |
---|
352 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
353 | } |
---|
354 | |
---|
355 | const std::string& OrxoBloxBall::getDefBoundarySound() |
---|
356 | { |
---|
357 | if( defBoundarySound_ ) |
---|
358 | return defBoundarySound_->getSource(); |
---|
359 | else |
---|
360 | assert(0); |
---|
361 | return BLANKSTRING; |
---|
362 | } |
---|
363 | } |
---|