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 ParticleProjectile.h |
---|
31 | @brief Implementation of the ParticleProjectile class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "BallProjectile.h" |
---|
35 | #include "gametypes/Gametype.h" |
---|
36 | #include <OrxoBlox/OrxoBloxStones.h> |
---|
37 | |
---|
38 | |
---|
39 | #include <OgreParticleEmitter.h> |
---|
40 | #include "core/CoreIncludes.h" |
---|
41 | #include "tools/ParticleInterface.h" |
---|
42 | #include "Scene.h" |
---|
43 | #include "core/command/Executor.h" |
---|
44 | #include "util/Convert.h" |
---|
45 | #include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h> |
---|
46 | #include <bullet/LinearMath/btVector3.h> |
---|
47 | |
---|
48 | namespace orxonox |
---|
49 | { |
---|
50 | RegisterClass(BallProjectile); |
---|
51 | |
---|
52 | BallProjectile::BallProjectile(Context* context) : BillboardProjectile(context) |
---|
53 | { |
---|
54 | RegisterObject(BallProjectile); |
---|
55 | this->textureIndex_ = 1; |
---|
56 | this->setMass(0.1f); |
---|
57 | this->maxTextureIndex_ = 8; |
---|
58 | this->setDestroyAfterCollision(false); //I want the ball to bounce, not to be destroyed |
---|
59 | this->fieldWidth_ = 46; |
---|
60 | this->fieldHeight_ = 49; |
---|
61 | this->orxoblox_ = this->getOrxoBlox(); |
---|
62 | this->setCollisionShapeRadius(2.5); |
---|
63 | |
---|
64 | //setEffect("Orxonox/sparks2"); |
---|
65 | } |
---|
66 | |
---|
67 | void BallProjectile::registerVariables() |
---|
68 | { |
---|
69 | registerVariable(this->materialBase_); |
---|
70 | registerVariable( this->speed_ ); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | @brief |
---|
75 | Set the material. |
---|
76 | @param material |
---|
77 | The name of the material. Material names with 1 to 8 appended must exist. |
---|
78 | */ |
---|
79 | void BallProjectile::setMaterial(const std::string& material) |
---|
80 | { |
---|
81 | this->materialBase_ = material; |
---|
82 | |
---|
83 | BillboardProjectile::setMaterial(material + multi_cast<std::string>(this->textureIndex_)); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | @brief |
---|
88 | Change the texture. |
---|
89 | */ |
---|
90 | void BallProjectile::changeTexture() |
---|
91 | { |
---|
92 | this->textureIndex_++; |
---|
93 | if (this->textureIndex_ > this->maxTextureIndex_) |
---|
94 | this->textureIndex_ = 1; |
---|
95 | |
---|
96 | this->setMaterial(this->materialBase_); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) { |
---|
102 | |
---|
103 | Vector3 velocity = this->getVelocity(); |
---|
104 | Vector3 myPosition = otherObject->getPosition(); |
---|
105 | btVector3 positionOtherObject = contactPoint.getPositionWorldOnA(); |
---|
106 | |
---|
107 | int distance_X = positionOtherObject.getX() - myPosition.x; |
---|
108 | int distance_Z = positionOtherObject.getZ() - myPosition.z; |
---|
109 | |
---|
110 | if (distance_X < 0) |
---|
111 | distance_X = -distance_X; |
---|
112 | |
---|
113 | |
---|
114 | if (distance_Z < 0) |
---|
115 | distance_Z = -distance_Z; |
---|
116 | |
---|
117 | //orxout() << distance_X << endl; |
---|
118 | //orxout() << distance_Z << endl; |
---|
119 | |
---|
120 | if (distance_X < distance_Z) { |
---|
121 | velocity.z = -velocity.z; |
---|
122 | //orxout() << "z" << endl; |
---|
123 | } |
---|
124 | if (distance_Z < distance_X) { |
---|
125 | velocity.x = -velocity.x; |
---|
126 | //orxout() << "x" << endl; |
---|
127 | } |
---|
128 | else { |
---|
129 | velocity.x = -velocity.x; |
---|
130 | velocity.z = -velocity.z; |
---|
131 | //orxout() << "both" << endl; |
---|
132 | } |
---|
133 | this->setVelocity(velocity); |
---|
134 | //} |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) |
---|
139 | { |
---|
140 | bool result = BasicProjectile::processCollision(otherObject, contactPoint, cs); |
---|
141 | if (result == true) { |
---|
142 | if (otherObject->isA(Class(OrxoBloxStones))) { |
---|
143 | Bounce(otherObject, contactPoint, cs); |
---|
144 | (orxonox_cast<OrxoBloxStones*>(otherObject))->gotHit(); |
---|
145 | |
---|
146 | } |
---|
147 | } |
---|
148 | return result; |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | OrxoBlox* BallProjectile::getOrxoBlox() |
---|
154 | { |
---|
155 | if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox))) |
---|
156 | { |
---|
157 | OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype()); |
---|
158 | return orxobloxGametype; |
---|
159 | } |
---|
160 | else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl; |
---|
161 | return nullptr; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | void BallProjectile::tick(float dt) |
---|
167 | { |
---|
168 | SUPER(BallProjectile, tick, dt); |
---|
169 | |
---|
170 | // Get the current position, velocity and acceleration of the ball. |
---|
171 | bool suicidal = false; |
---|
172 | Vector3 position = this->getPosition(); |
---|
173 | Vector3 velocity = this->getVelocity(); |
---|
174 | Vector3 acceleration = this->getAcceleration(); |
---|
175 | |
---|
176 | velocity.y = 0; |
---|
177 | position.y = 0; |
---|
178 | |
---|
179 | // 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). |
---|
180 | if (position.z > this->fieldHeight_ || position.z < -this->fieldHeight_) |
---|
181 | { |
---|
182 | |
---|
183 | velocity.z = -velocity.z; |
---|
184 | // And its position is set as to not overstep the boundary it has just crossed. Remember z axis is reverted!!! |
---|
185 | if (position.z > this->fieldHeight_){ |
---|
186 | // Set the ball to be exactly at the boundary. |
---|
187 | position.z = this-> fieldHeight_; |
---|
188 | orxoblox_->count(); |
---|
189 | suicidal = true; |
---|
190 | |
---|
191 | } |
---|
192 | if (position.z < -this->fieldHeight_){ |
---|
193 | position.z = -this->fieldHeight_; |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | this->fireEvent(); |
---|
198 | } |
---|
199 | |
---|
200 | //Ball hits the right or left wall and should bounce back. |
---|
201 | // If the ball has crossed the left or right boundary of the playing field. |
---|
202 | if (position.x > this->fieldWidth_ || position.x < -this->fieldWidth_) |
---|
203 | { |
---|
204 | //Ball hits the right Wall |
---|
205 | if (position.x > this->fieldWidth_) |
---|
206 | { |
---|
207 | // Set the ball to be exactly at the boundary. |
---|
208 | position.x = this->fieldWidth_; |
---|
209 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
210 | velocity.x = -velocity.x; |
---|
211 | this->fireEvent(); |
---|
212 | } |
---|
213 | |
---|
214 | //Ball hits the left wall |
---|
215 | else if (position.x < -this->fieldWidth_) |
---|
216 | { |
---|
217 | // Set the ball to be exactly at the boundary. |
---|
218 | position.x = -this->fieldWidth_; |
---|
219 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
220 | velocity.x = -velocity.x; |
---|
221 | this->fireEvent(); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | // Set the position, velocity and acceleration of the ball, if they have changed. |
---|
226 | if (acceleration != this->getAcceleration()) |
---|
227 | this->setAcceleration(acceleration); |
---|
228 | if (velocity != this->getVelocity()) |
---|
229 | this->setVelocity(velocity); |
---|
230 | if (position != this->getPosition()) |
---|
231 | this->setPosition(position); |
---|
232 | //this->Collides((this->orxoblox_->CheckForCollision(this))); |
---|
233 | if (suicidal == true) { |
---|
234 | this->destroy(); |
---|
235 | } |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | void BallProjectile::setSpeed(float speed) |
---|
242 | { |
---|
243 | if (speed != this->speed_) // If the speed changes |
---|
244 | { |
---|
245 | this->speed_ = speed; |
---|
246 | |
---|
247 | // Set the speed in the direction of the balls current velocity. |
---|
248 | Vector3 velocity = this->getVelocity(); |
---|
249 | if (velocity.x != 0) |
---|
250 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
251 | else // If the balls current velocity is zero, the speed is set in a random direction. |
---|
252 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
253 | |
---|
254 | this->setVelocity(velocity); |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | void BallProjectile::destroyObject(void) |
---|
259 | { |
---|
260 | if(GameMode::isMaster()) { |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | } |
---|