| 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 | * Julien Kindle |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | @file SOBFigure.cc |
|---|
| 31 | @brief This class represents your figure when you play the minigame. Here the movement of the figure, activating items, ... are handled. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include "SOBFigure.h" |
|---|
| 35 | |
|---|
| 36 | #include "core/CoreIncludes.h" |
|---|
| 37 | #include "core/XMLPort.h" |
|---|
| 38 | #include "graphics/Model.h" |
|---|
| 39 | #include "graphics/Camera.h" |
|---|
| 40 | #include "graphics/ParticleSpawner.h" |
|---|
| 41 | |
|---|
| 42 | #include "SOBMushroom.h" |
|---|
| 43 | #include "SOBGumba.h" |
|---|
| 44 | #include "SOB.h" |
|---|
| 45 | #include "SOBFlagstone.h" |
|---|
| 46 | #include "SOBCastlestone.h" |
|---|
| 47 | #include <BulletCollision/NarrowPhaseCollision/btManifoldPoint.h> |
|---|
| 48 | |
|---|
| 49 | namespace orxonox |
|---|
| 50 | { |
|---|
| 51 | RegisterClass(SOBFigure); |
|---|
| 52 | |
|---|
| 53 | SOBFigure::SOBFigure(Context* context) : ControllableEntity(context) |
|---|
| 54 | { |
|---|
| 55 | RegisterObject(SOBFigure); |
|---|
| 56 | |
|---|
| 57 | // initialize variables |
|---|
| 58 | gravityAcceleration_ = 350.0; |
|---|
| 59 | |
|---|
| 60 | //Vars for movement of player |
|---|
| 61 | moveUpPressed_ = false; |
|---|
| 62 | moveDownPressed_ = false; |
|---|
| 63 | moveLeftPressed_ = false; |
|---|
| 64 | moveDownPressed_ = false; |
|---|
| 65 | firePressed_ = false; |
|---|
| 66 | collDisZ_ = 0; |
|---|
| 67 | //Times and turning |
|---|
| 68 | timeSinceLastFire_ = 0.0; |
|---|
| 69 | lastSpeed_z = 0.0; |
|---|
| 70 | pitch_ = 0.0; |
|---|
| 71 | timeCounter_ = 0; |
|---|
| 72 | |
|---|
| 73 | //Properties of player |
|---|
| 74 | gotPowerUp_ = false; |
|---|
| 75 | isColliding_ = true; |
|---|
| 76 | particlespawner_ = NULL; |
|---|
| 77 | |
|---|
| 78 | //Properties of players life |
|---|
| 79 | predead_ = false; |
|---|
| 80 | dead_ = false; |
|---|
| 81 | lvlEnded_ = false; |
|---|
| 82 | reachedLvlEndState_ = 0; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | setAngularFactor(0.0); //Means player doesn't turn on collision, so he doesn't fall over while walking over the ground |
|---|
| 86 | this->enableCollisionCallback(); // Turns on that on every collision function collidesAgainst is executed |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | bool SOBFigure::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) { |
|---|
| 92 | |
|---|
| 93 | //Inform tick fct that player is colliding and tell him how far away the collision point is from player middle point in z dir |
|---|
| 94 | isColliding_ = true; |
|---|
| 95 | collDisZ_ = getPosition().z - contactPoint.getPositionWorldOnB().getZ(); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | //Orxocast returns object with casted type if otherObject has that class, and if not a nullptr |
|---|
| 99 | SOBMushroom* mush = orxonox_cast<SOBMushroom*>(otherObject); |
|---|
| 100 | SOBGumba* gumba = orxonox_cast<SOBGumba*>(otherObject); |
|---|
| 101 | SOBFlagstone* flagstone = orxonox_cast<SOBFlagstone*>(otherObject); |
|---|
| 102 | SOBCastlestone* castlestone = orxonox_cast<SOBCastlestone*>(otherObject); |
|---|
| 103 | |
|---|
| 104 | //Check if otherObject is a powerup |
|---|
| 105 | if (mush != nullptr && !(mush->hasCollided_)) { |
|---|
| 106 | otherObject->destroyLater(); |
|---|
| 107 | gotPowerUp_ = true; |
|---|
| 108 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); //Get the Gametype |
|---|
| 109 | SOBGame->addMushroom(); // Tell the gametype to increase points |
|---|
| 110 | mush->hasCollided_ = true; // needed because of destroyLater takes some time and player should receive points only once |
|---|
| 111 | |
|---|
| 112 | //Check if otherObject is a Gumba (that walking enemies) |
|---|
| 113 | } else if (gumba != nullptr && !(gumba->hasCollided_)) { |
|---|
| 114 | |
|---|
| 115 | //If player jumps on its head, kill it, else, kill the player |
|---|
| 116 | if (getVelocity().z >= -20) { |
|---|
| 117 | Vector3 vel = getVelocity(); |
|---|
| 118 | vel.y = -80; |
|---|
| 119 | vel.z = 200; |
|---|
| 120 | setVelocity(vel); |
|---|
| 121 | predead_=true; |
|---|
| 122 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
|---|
| 123 | SOBGame->setDone(true); |
|---|
| 124 | |
|---|
| 125 | } else { |
|---|
| 126 | gumba->destroyLater(); |
|---|
| 127 | gumba->hasCollided_ = true; |
|---|
| 128 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
|---|
| 129 | SOBGame->addGumba(); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | //Purpose is that if player hits the flag, he should walk into the castle at the end of the level. For that we use SOBCastlestone |
|---|
| 136 | if (reachedLvlEndState_ == 0 && flagstone != nullptr && !(flagstone->hasCollided_)) { |
|---|
| 137 | flagstone->hasCollided_ = true; |
|---|
| 138 | reachedLvlEndState_ = 1; |
|---|
| 139 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
|---|
| 140 | SOBGame->setDone(true); |
|---|
| 141 | SOBGame->addPoints(flagstone->getPoints()); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | } |
|---|
| 145 | if (castlestone != nullptr && !(castlestone->hasCollided_)) { |
|---|
| 146 | castlestone->hasCollided_ = true; |
|---|
| 147 | reachedLvlEndState_++; |
|---|
| 148 | |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | return true; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | //Self implemented sign function that returns either 1 or -1 (and never 0) |
|---|
| 156 | int SOBFigure::sgn(float x) { |
|---|
| 157 | if (x < 0.0) return -1; |
|---|
| 158 | return 1; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | //For those of you who don't have an idea: the tick function is called about 50 times/sec |
|---|
| 162 | void SOBFigure::tick(float dt) |
|---|
| 163 | { |
|---|
| 164 | SUPER(SOBFigure, tick, dt); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | bool inputAllowed = true; |
|---|
| 168 | |
|---|
| 169 | //the particle spawner that generates the fire from the backpack when pressed |
|---|
| 170 | if (particlespawner_ == NULL) { |
|---|
| 171 | for (WorldEntity* object : this->getAttachedObjects()) |
|---|
| 172 | { |
|---|
| 173 | if (object->isA(Class(ParticleSpawner))) |
|---|
| 174 | particlespawner_ = object; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | //Behavior on level end - this is like described above for the movement from the player when hit the flag. He moves then into the castle |
|---|
| 180 | if (reachedLvlEndState_ != 0) { |
|---|
| 181 | timeCounter_+= dt; |
|---|
| 182 | inputAllowed = false; |
|---|
| 183 | } |
|---|
| 184 | if (reachedLvlEndState_ == 1 && timeCounter_ >= 1.5) { |
|---|
| 185 | timeCounter_ = 0; |
|---|
| 186 | reachedLvlEndState_ = 2; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | //if input blocked, then cancel every movement operation |
|---|
| 191 | if (!inputAllowed) { |
|---|
| 192 | moveUpPressed_ = false; |
|---|
| 193 | moveDownPressed_ = false; |
|---|
| 194 | moveLeftPressed_ = false; |
|---|
| 195 | moveRightPressed_ = false; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | //set the gravityto standard 350 |
|---|
| 199 | if (firePressed_ == false) { |
|---|
| 200 | gravityAcceleration_ = 350.0; |
|---|
| 201 | |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | if (hasLocalController()) |
|---|
| 205 | { |
|---|
| 206 | Vector3 velocity = getVelocity(); |
|---|
| 207 | Vector3 position = getPosition(); |
|---|
| 208 | |
|---|
| 209 | if (!predead_) |
|---|
| 210 | velocity.y = 0; |
|---|
| 211 | //If player falls in a hole |
|---|
| 212 | if (position.z < -100) { |
|---|
| 213 | dead_ = true; |
|---|
| 214 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
|---|
| 215 | SOBGame->setDone(true); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | if (dead_) { |
|---|
| 220 | velocity.x = 0; |
|---|
| 221 | velocity.z = 0; |
|---|
| 222 | setVelocity(velocity); |
|---|
| 223 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
|---|
| 224 | if (firePressed_) |
|---|
| 225 | SOBGame->restart(); |
|---|
| 226 | return; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | int maxvelocity_x = 100; |
|---|
| 231 | int speedAddedPerTick = 5; |
|---|
| 232 | int camMaxOffset = 25; |
|---|
| 233 | |
|---|
| 234 | timeSinceLastFire_ += dt; |
|---|
| 235 | lastSpeed_z = velocity.z; |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | //Handle the rocket fire from the jetpack |
|---|
| 240 | if (velocity.z > 40) |
|---|
| 241 | particlespawner_->setVisible(true); |
|---|
| 242 | else |
|---|
| 243 | particlespawner_->setVisible(false); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | //If player hits space and collides against an object under him then jump |
|---|
| 247 | if (inputAllowed && firePressed_ && isColliding_ && (collDisZ_ >= 7.75 && collDisZ_ <+ 8.25)) { |
|---|
| 248 | gravityAcceleration_ = 100.0; |
|---|
| 249 | velocity.z = 110; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | //Left-right movement with acceleration and rotation |
|---|
| 254 | float rot = getOrientation().getRoll().valueDegrees(); |
|---|
| 255 | if (moveRightPressed_) { |
|---|
| 256 | if (!(rot < 5.0 && -5.0 < rot)) |
|---|
| 257 | setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() - sgn(rot)*dt*Radian(6)); |
|---|
| 258 | |
|---|
| 259 | if (std::abs(velocity.x) < maxvelocity_x) { |
|---|
| 260 | velocity.x += speedAddedPerTick; |
|---|
| 261 | |
|---|
| 262 | } |
|---|
| 263 | } else if (moveLeftPressed_) { |
|---|
| 264 | if (!(abs(rot) > 175.0 )) |
|---|
| 265 | setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() + sgn(rot)*dt*Radian(6)); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | if (std::abs(velocity.x) < maxvelocity_x) { |
|---|
| 270 | velocity.x -= speedAddedPerTick; |
|---|
| 271 | } |
|---|
| 272 | } else { |
|---|
| 273 | velocity.x /= 1.1; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | //Again another EndOfLevel behavior |
|---|
| 278 | if (reachedLvlEndState_ == 1) |
|---|
| 279 | velocity.x = -2; |
|---|
| 280 | if (reachedLvlEndState_ == 2) |
|---|
| 281 | velocity.x = 30; |
|---|
| 282 | if (reachedLvlEndState_ == 3) { |
|---|
| 283 | velocity.x = 0; |
|---|
| 284 | velocity.y = 20; |
|---|
| 285 | } |
|---|
| 286 | if (reachedLvlEndState_ == 4) { |
|---|
| 287 | lvlEnded_ = true; |
|---|
| 288 | dead_ = true; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | //velocity = acc. * time |
|---|
| 292 | velocity.z -= gravityAcceleration_*dt; |
|---|
| 293 | setVelocity(velocity); |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | //Camera operation - the camera should always follow the player in a specific region |
|---|
| 297 | Camera* cam = getCamera(); |
|---|
| 298 | Vector3 campos = cam->getPosition(); |
|---|
| 299 | |
|---|
| 300 | if (campos.x + camMaxOffset < position.x) { |
|---|
| 301 | campos.x = position.x - camMaxOffset; |
|---|
| 302 | cam->setPosition(campos); |
|---|
| 303 | } |
|---|
| 304 | if (campos.x - camMaxOffset > position.x) { |
|---|
| 305 | campos.x = position.x + camMaxOffset; |
|---|
| 306 | cam->setPosition(campos); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | // Reset key variables |
|---|
| 317 | moveUpPressed_ = false; |
|---|
| 318 | moveDownPressed_ = false; |
|---|
| 319 | moveLeftPressed_ = false; |
|---|
| 320 | moveRightPressed_ = false; |
|---|
| 321 | |
|---|
| 322 | isColliding_ = false; |
|---|
| 323 | collDisZ_ = 0; |
|---|
| 324 | |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | //The following functions read the input of the player and then set the bools for the movement |
|---|
| 332 | void SOBFigure::moveFrontBack(const Vector2& value) |
|---|
| 333 | { |
|---|
| 334 | if (value.x > 0) |
|---|
| 335 | { |
|---|
| 336 | moveUpPressed_ = true; |
|---|
| 337 | moveDownPressed_ = false; |
|---|
| 338 | } |
|---|
| 339 | else |
|---|
| 340 | { |
|---|
| 341 | moveUpPressed_ = false; |
|---|
| 342 | moveDownPressed_ = true; |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | void SOBFigure::moveRightLeft(const Vector2& value) |
|---|
| 347 | { |
|---|
| 348 | if (value.x > 0) |
|---|
| 349 | { |
|---|
| 350 | moveLeftPressed_ = false; |
|---|
| 351 | moveRightPressed_ = true; |
|---|
| 352 | } |
|---|
| 353 | else |
|---|
| 354 | { |
|---|
| 355 | moveLeftPressed_ = true; |
|---|
| 356 | moveRightPressed_ = false; |
|---|
| 357 | } |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | void SOBFigure::boost(bool boost) |
|---|
| 361 | { |
|---|
| 362 | firePressed_ = boost; |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | } |
|---|