[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(); |
---|
[12281] | 61 | |
---|
| 62 | //setEffect("Orxonox/sparks2"); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void BallProjectile::registerVariables() |
---|
| 66 | { |
---|
| 67 | registerVariable(this->materialBase_); |
---|
[12310] | 68 | registerVariable( this->speed_ ); |
---|
[12281] | 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | @brief |
---|
| 73 | Set the material. |
---|
| 74 | @param material |
---|
| 75 | The name of the material. Material names with 1 to 8 appended must exist. |
---|
| 76 | */ |
---|
| 77 | void BallProjectile::setMaterial(const std::string& material) |
---|
| 78 | { |
---|
| 79 | this->materialBase_ = material; |
---|
| 80 | |
---|
| 81 | BillboardProjectile::setMaterial(material + multi_cast<std::string>(this->textureIndex_)); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | @brief |
---|
| 86 | Change the texture. |
---|
| 87 | */ |
---|
| 88 | void BallProjectile::changeTexture() |
---|
| 89 | { |
---|
| 90 | this->textureIndex_++; |
---|
| 91 | if (this->textureIndex_ > this->maxTextureIndex_) |
---|
| 92 | this->textureIndex_ = 1; |
---|
| 93 | |
---|
| 94 | this->setMaterial(this->materialBase_); |
---|
| 95 | } |
---|
| 96 | |
---|
[12310] | 97 | |
---|
| 98 | |
---|
[12281] | 99 | void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) { |
---|
| 100 | |
---|
| 101 | Vector3 velocity = this->getVelocity(); |
---|
[12310] | 102 | Vector3 myPosition = otherObject->getPosition(); |
---|
| 103 | btVector3 positionOtherObject = contactPoint.getPositionWorldOnA(); |
---|
[12281] | 104 | |
---|
[12310] | 105 | int distance_X = positionOtherObject.getX() - myPosition.x; |
---|
| 106 | int distance_Z = positionOtherObject.getZ() - myPosition.z; |
---|
[12281] | 107 | |
---|
| 108 | if (distance_X < 0) |
---|
[12310] | 109 | distance_X = -distance_X; |
---|
[12281] | 110 | |
---|
| 111 | |
---|
[12310] | 112 | if (distance_Z < 0) |
---|
| 113 | distance_Z = -distance_Z; |
---|
[12281] | 114 | |
---|
[12376] | 115 | //orxout() << distance_X << endl; |
---|
| 116 | //orxout() << distance_Z << endl; |
---|
[12310] | 117 | |
---|
| 118 | if (distance_X < distance_Z) { |
---|
| 119 | velocity.z = -velocity.z; |
---|
[12376] | 120 | //orxout() << "z" << endl; |
---|
[12310] | 121 | } |
---|
| 122 | if (distance_Z < distance_X) { |
---|
[12281] | 123 | velocity.x = -velocity.x; |
---|
[12376] | 124 | //orxout() << "x" << endl; |
---|
[12310] | 125 | } |
---|
[12281] | 126 | else { |
---|
| 127 | velocity.x = -velocity.x; |
---|
[12310] | 128 | velocity.z = -velocity.z; |
---|
[12376] | 129 | //orxout() << "both" << endl; |
---|
[12281] | 130 | } |
---|
[12310] | 131 | this->setVelocity(velocity); |
---|
[12281] | 132 | //} |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) |
---|
| 137 | { |
---|
| 138 | bool result = BasicProjectile::processCollision(otherObject, contactPoint, cs); |
---|
[12371] | 139 | if (result == true) { |
---|
| 140 | if (otherObject->isA(Class(OrxoBloxStones))) { |
---|
| 141 | Bounce(otherObject, contactPoint, cs); |
---|
[12378] | 142 | orxout() << "BOUNCED!" << endl; |
---|
[12371] | 143 | } |
---|
| 144 | } |
---|
[12281] | 145 | return result; |
---|
| 146 | } |
---|
[12310] | 147 | |
---|
| 148 | |
---|
[12384] | 149 | |
---|
[12368] | 150 | OrxoBlox* BallProjectile::getOrxoBlox() |
---|
| 151 | { |
---|
| 152 | if (this->getGametype() != nullptr && this->getGametype()->isA(Class(OrxoBlox))) |
---|
| 153 | { |
---|
| 154 | OrxoBlox* orxobloxGametype = orxonox_cast<OrxoBlox*>(this->getGametype()); |
---|
| 155 | return orxobloxGametype; |
---|
| 156 | } |
---|
| 157 | else orxout()<<"There is no Gametype for OrxoBlox! ask Anna"<< endl; |
---|
| 158 | return nullptr; |
---|
| 159 | } |
---|
[12384] | 160 | |
---|
[12310] | 161 | |
---|
| 162 | |
---|
| 163 | void BallProjectile::tick(float dt) |
---|
| 164 | { |
---|
| 165 | SUPER(BallProjectile, tick, dt); |
---|
| 166 | |
---|
[12368] | 167 | // Get the current position, velocity and acceleration of the ball. |
---|
[12310] | 168 | Vector3 position = this->getPosition(); |
---|
| 169 | Vector3 velocity = this->getVelocity(); |
---|
| 170 | Vector3 acceleration = this->getAcceleration(); |
---|
| 171 | |
---|
[12371] | 172 | velocity.y = 0; |
---|
| 173 | position.y = 0; |
---|
| 174 | |
---|
[12310] | 175 | // 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] | 176 | if (position.z > this->fieldHeight_ || position.z < -this->fieldHeight_) |
---|
[12310] | 177 | { |
---|
[12368] | 178 | |
---|
[12310] | 179 | velocity.z = -velocity.z; |
---|
[12368] | 180 | // And its position is set as to not overstep the boundary it has just crossed. Remember z axis is reverted!!! |
---|
[12371] | 181 | if (position.z > this->fieldHeight_){ |
---|
[12368] | 182 | // Set the ball to be exactly at the boundary. |
---|
[12371] | 183 | position.z = this-> fieldHeight_; |
---|
[12368] | 184 | |
---|
[12384] | 185 | orxoblox_->LevelUp(); |
---|
[12310] | 186 | |
---|
[12368] | 187 | |
---|
| 188 | //this->setSpeed(0); // doesn't work here, why??; |
---|
| 189 | //Stopping ball |
---|
[12376] | 190 | //orxout() << "Ball stopped" << endl; |
---|
[12368] | 191 | velocity.x = 0; |
---|
| 192 | velocity.y = 0; |
---|
| 193 | velocity.z = 0; |
---|
| 194 | |
---|
| 195 | //ChatManager::message("Waiting for your input"); |
---|
| 196 | //Input new speed here: |
---|
| 197 | //ChatManager::message("Setting new speed"); |
---|
| 198 | |
---|
| 199 | //%%%%%%%%%%%% |
---|
| 200 | //MAUSPOSITION |
---|
| 201 | //%%%%%%%%%%%% |
---|
| 202 | //Reads current mouse position |
---|
| 203 | //TODO: read Mouse position on click! |
---|
| 204 | //int mousex = InputManager::getInstance().getMousePosition().first; |
---|
| 205 | //int mousey = InputManager::getInstance().getMousePosition().second; |
---|
| 206 | //ChatManager::message("Read mouse position"); |
---|
| 207 | //orxout() << "Mouseposition" << endl; |
---|
| 208 | //orxout() << mousex << endl; |
---|
| 209 | //ChatManager::message(mousex); |
---|
| 210 | //ChatManager::message("mousey"); |
---|
| 211 | //ChatManager::message(mousey); |
---|
| 212 | //Set new speed here!! |
---|
| 213 | velocity.x = rnd(-100,100); |
---|
| 214 | velocity.z = rnd(-50,-100); |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | } |
---|
[12371] | 218 | if (position.z < -this->fieldHeight_){ |
---|
| 219 | position.z = -this->fieldHeight_; |
---|
[12368] | 220 | |
---|
| 221 | } |
---|
| 222 | |
---|
[12310] | 223 | this->fireEvent(); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | //Ball hits the right or left wall and should bounce back. |
---|
| 227 | // If the ball has crossed the left or right boundary of the playing field. |
---|
[12371] | 228 | if (position.x > this->fieldWidth_ || position.x < -this->fieldWidth_) |
---|
[12310] | 229 | { |
---|
| 230 | //Ball hits the right Wall |
---|
[12371] | 231 | if (position.x > this->fieldWidth_) |
---|
[12310] | 232 | { |
---|
| 233 | // Set the ball to be exactly at the boundary. |
---|
[12371] | 234 | position.x = this->fieldWidth_; |
---|
[12310] | 235 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
| 236 | velocity.x = -velocity.x; |
---|
| 237 | this->fireEvent(); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | //Ball hits the left wall |
---|
[12371] | 241 | else if (position.x < -this->fieldWidth_) |
---|
[12310] | 242 | { |
---|
| 243 | // Set the ball to be exactly at the boundary. |
---|
[12371] | 244 | position.x = -this->fieldWidth_; |
---|
[12310] | 245 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
| 246 | velocity.x = -velocity.x; |
---|
| 247 | this->fireEvent(); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | // Set the position, velocity and acceleration of the ball, if they have changed. |
---|
| 252 | if (acceleration != this->getAcceleration()) |
---|
| 253 | this->setAcceleration(acceleration); |
---|
| 254 | if (velocity != this->getVelocity()) |
---|
| 255 | this->setVelocity(velocity); |
---|
| 256 | if (position != this->getPosition()) |
---|
| 257 | this->setPosition(position); |
---|
[12368] | 258 | //this->Collides((this->orxoblox_->CheckForCollision(this))); |
---|
| 259 | |
---|
[12310] | 260 | } |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | void BallProjectile::setSpeed(float speed) |
---|
| 265 | { |
---|
| 266 | if (speed != this->speed_) // If the speed changes |
---|
| 267 | { |
---|
| 268 | this->speed_ = speed; |
---|
| 269 | |
---|
| 270 | // Set the speed in the direction of the balls current velocity. |
---|
| 271 | Vector3 velocity = this->getVelocity(); |
---|
| 272 | if (velocity.x != 0) |
---|
| 273 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
| 274 | else // If the balls current velocity is zero, the speed is set in a random direction. |
---|
| 275 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
| 276 | |
---|
| 277 | this->setVelocity(velocity); |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
[12378] | 281 | void BallProjectile::destroyObject(void) |
---|
| 282 | { |
---|
| 283 | if(GameMode::isMaster()) { |
---|
| 284 | } |
---|
| 285 | } |
---|
[12310] | 286 | |
---|
[12378] | 287 | |
---|
| 288 | |
---|
[12281] | 289 | } |
---|