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 "PongBall.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "core/GameMode.h" |
---|
33 | #include "gametypes/Gametype.h" |
---|
34 | #include "PongBat.h" |
---|
35 | #include "sound/SoundBase.h" |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | CreateFactory(PongBall); |
---|
40 | |
---|
41 | const float PongBall::MAX_REL_Z_VELOCITY = 1.5; |
---|
42 | |
---|
43 | PongBall::PongBall(BaseObject* creator) |
---|
44 | : MovableEntity(creator) |
---|
45 | , sidesound_(NULL) |
---|
46 | , batsound_(NULL) |
---|
47 | , scoresound_(NULL) |
---|
48 | { |
---|
49 | RegisterObject(PongBall); |
---|
50 | |
---|
51 | this->speed_ = 0; |
---|
52 | this->bat_ = 0; |
---|
53 | this->batID_ = new unsigned int[2]; |
---|
54 | this->batID_[0] = OBJECTID_UNKNOWN; |
---|
55 | this->batID_[1] = OBJECTID_UNKNOWN; |
---|
56 | this->relMercyOffset_ = 0.05f; |
---|
57 | |
---|
58 | this->registerVariables(); |
---|
59 | |
---|
60 | if (GameMode::playsSound()) |
---|
61 | { |
---|
62 | this->sidesound_ = new SoundBase(this); |
---|
63 | this->sidesound_->loadFile("sounds/pong_side.wav"); |
---|
64 | |
---|
65 | this->batsound_ = new SoundBase(this); |
---|
66 | this->batsound_->loadFile("sounds/pong_bat.wav"); |
---|
67 | |
---|
68 | this->scoresound_ = new SoundBase(this); |
---|
69 | this->scoresound_->loadFile("sounds/pong_score.wav"); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | PongBall::~PongBall() |
---|
74 | { |
---|
75 | if (this->sidesound_) |
---|
76 | delete this->sidesound_; |
---|
77 | if (this->batsound_) |
---|
78 | delete this->batsound_; |
---|
79 | if (this->scoresound_) |
---|
80 | delete this->scoresound_; |
---|
81 | } |
---|
82 | |
---|
83 | void PongBall::registerVariables() |
---|
84 | { |
---|
85 | registerVariable( this->fieldWidth_ ); |
---|
86 | registerVariable( this->fieldHeight_ ); |
---|
87 | registerVariable( this->batlength_ ); |
---|
88 | registerVariable( this->speed_ ); |
---|
89 | registerVariable( this->relMercyOffset_ ); |
---|
90 | registerVariable( this->batID_[0] ); |
---|
91 | registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) ); |
---|
92 | } |
---|
93 | |
---|
94 | void PongBall::tick(float dt) |
---|
95 | { |
---|
96 | SUPER(PongBall, tick, dt); |
---|
97 | |
---|
98 | if (GameMode::isMaster()) |
---|
99 | { |
---|
100 | Vector3 position = this->getPosition(); |
---|
101 | Vector3 velocity = this->getVelocity(); |
---|
102 | |
---|
103 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
104 | { |
---|
105 | velocity.z = -velocity.z; |
---|
106 | if (GameMode::playsSound()) |
---|
107 | this->sidesound_->play(); |
---|
108 | |
---|
109 | if (position.z > this->fieldHeight_ / 2) |
---|
110 | position.z = this->fieldHeight_ / 2; |
---|
111 | if (position.z < -this->fieldHeight_ / 2) |
---|
112 | position.z = -this->fieldHeight_ / 2; |
---|
113 | } |
---|
114 | |
---|
115 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
116 | { |
---|
117 | float distance = 0; |
---|
118 | |
---|
119 | if (this->bat_) |
---|
120 | { |
---|
121 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
---|
122 | { |
---|
123 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
124 | if (fabs(distance) <= 1) |
---|
125 | { |
---|
126 | position.x = this->fieldWidth_ / 2; |
---|
127 | velocity.x = -velocity.x; |
---|
128 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
129 | if (GameMode::playsSound()) |
---|
130 | this->batsound_->play(); |
---|
131 | } |
---|
132 | else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
133 | { |
---|
134 | if (this->getGametype() && this->bat_[0]) |
---|
135 | { |
---|
136 | this->getGametype()->playerScored(this->bat_[0]->getPlayer()); |
---|
137 | if (GameMode::playsSound()) |
---|
138 | this->scoresound_->play(); |
---|
139 | return; |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
---|
144 | { |
---|
145 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
146 | if (fabs(distance) <= 1) |
---|
147 | { |
---|
148 | position.x = -this->fieldWidth_ / 2; |
---|
149 | velocity.x = -velocity.x; |
---|
150 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
151 | if (GameMode::playsSound()) |
---|
152 | this->batsound_->play(); |
---|
153 | } |
---|
154 | else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) |
---|
155 | { |
---|
156 | if (this->getGametype() && this->bat_[1]) |
---|
157 | { |
---|
158 | if (GameMode::playsSound()) |
---|
159 | this->scoresound_->play(); |
---|
160 | this->getGametype()->playerScored(this->bat_[1]->getPlayer()); |
---|
161 | return; |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | if (velocity != this->getVelocity()) |
---|
169 | this->setVelocity(velocity); |
---|
170 | if (position != this->getPosition()) |
---|
171 | this->setPosition(position); |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | Vector3 position = this->getPosition(); |
---|
176 | Vector3 velocity = this->getVelocity(); |
---|
177 | |
---|
178 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
179 | { |
---|
180 | velocity.z = -velocity.z; |
---|
181 | if (GameMode::playsSound()) |
---|
182 | this->sidesound_->play(); |
---|
183 | |
---|
184 | if (position.z > this->fieldHeight_ / 2) |
---|
185 | position.z = this->fieldHeight_ / 2; |
---|
186 | if (position.z < -this->fieldHeight_ / 2) |
---|
187 | position.z = -this->fieldHeight_ / 2; |
---|
188 | } |
---|
189 | |
---|
190 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
191 | { |
---|
192 | float distance = 0; |
---|
193 | |
---|
194 | if (this->bat_) |
---|
195 | { |
---|
196 | if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) |
---|
197 | { |
---|
198 | distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
199 | if (fabs(distance) <= 1) |
---|
200 | { |
---|
201 | position.x = this->fieldWidth_ / 2; |
---|
202 | velocity.x = -velocity.x; |
---|
203 | if (GameMode::playsSound()) |
---|
204 | this->batsound_->play(); |
---|
205 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
206 | } |
---|
207 | } |
---|
208 | if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) |
---|
209 | { |
---|
210 | distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); |
---|
211 | if (fabs(distance) <= 1) |
---|
212 | { |
---|
213 | position.x = -this->fieldWidth_ / 2; |
---|
214 | velocity.x = -velocity.x; |
---|
215 | if (GameMode::playsSound()) |
---|
216 | this->batsound_->play(); |
---|
217 | velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | if (velocity != this->getVelocity()) |
---|
224 | this->setVelocity(velocity); |
---|
225 | if (position != this->getPosition()) |
---|
226 | this->setPosition(position); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | void PongBall::setSpeed(float speed) |
---|
231 | { |
---|
232 | if (speed != this->speed_) |
---|
233 | { |
---|
234 | this->speed_ = speed; |
---|
235 | |
---|
236 | Vector3 velocity = this->getVelocity(); |
---|
237 | if (velocity.x != 0) |
---|
238 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
239 | else |
---|
240 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
241 | |
---|
242 | this->setVelocity(velocity); |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | void PongBall::setBats(PongBat** bats) |
---|
247 | { |
---|
248 | this->bat_ = bats; |
---|
249 | this->batID_[0] = this->bat_[0]->getObjectID(); |
---|
250 | this->batID_[1] = this->bat_[1]->getObjectID(); |
---|
251 | } |
---|
252 | |
---|
253 | void PongBall::applyBats() |
---|
254 | { |
---|
255 | if (!this->bat_) |
---|
256 | this->bat_ = new PongBat*[2]; |
---|
257 | if (this->batID_[0] != OBJECTID_UNKNOWN) |
---|
258 | this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0])); |
---|
259 | if (this->batID_[1] != OBJECTID_UNKNOWN) |
---|
260 | this->bat_[1] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[1])); |
---|
261 | } |
---|
262 | } |
---|