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