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