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