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