[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 |
---|
[11538] | 115 | std::string name = "orxo_material_gross"; |
---|
| 116 | this->changeClothes(name); |
---|
| 117 | |
---|
[11491] | 118 | } |
---|
[11538] | 119 | |
---|
[11416] | 120 | //Check if otherObject is a Gumba (that walking enemies) |
---|
[11412] | 121 | |
---|
[11491] | 122 | else if (gumba != nullptr && !(gumba->hasCollided_)) { |
---|
| 123 | |
---|
[11556] | 124 | //If player jumps on its head, kill the Gumba, else, kill the player |
---|
[11412] | 125 | if (getVelocity().z >= -20) { |
---|
[11491] | 126 | // If player hasn't a power up, he dies. Else he shrinks and the gumba dies. |
---|
| 127 | if(!gotPowerUp_){ |
---|
| 128 | Vector3 vel = getVelocity(); |
---|
| 129 | vel.y = -80; |
---|
| 130 | vel.z = 200; |
---|
| 131 | setVelocity(vel); |
---|
| 132 | predead_=true; |
---|
| 133 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
---|
| 134 | SOBGame->setDone(true); |
---|
| 135 | } else{ |
---|
| 136 | gotPowerUp_ = false; |
---|
| 137 | |
---|
[11531] | 138 | |
---|
[11538] | 139 | // now, change the clothes of the Figure to old ones |
---|
| 140 | std::string name = "orxo_material"; |
---|
| 141 | this->changeClothes(name); |
---|
[11491] | 142 | gumba->destroyLater(); |
---|
| 143 | gumba->hasCollided_ = true; |
---|
| 144 | } |
---|
| 145 | |
---|
[11412] | 146 | } else { |
---|
| 147 | gumba->destroyLater(); |
---|
| 148 | gumba->hasCollided_ = true; |
---|
| 149 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
---|
| 150 | SOBGame->addGumba(); |
---|
| 151 | |
---|
| 152 | |
---|
[11402] | 153 | } |
---|
[11400] | 154 | } |
---|
| 155 | |
---|
[11416] | 156 | //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 |
---|
| 157 | if (reachedLvlEndState_ == 0 && flagstone != nullptr && !(flagstone->hasCollided_)) { |
---|
| 158 | flagstone->hasCollided_ = true; |
---|
| 159 | reachedLvlEndState_ = 1; |
---|
| 160 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
---|
[11418] | 161 | SOBGame->setDone(true); |
---|
[11416] | 162 | SOBGame->addPoints(flagstone->getPoints()); |
---|
[11418] | 163 | |
---|
[11400] | 164 | |
---|
[11416] | 165 | } |
---|
| 166 | if (castlestone != nullptr && !(castlestone->hasCollided_)) { |
---|
| 167 | castlestone->hasCollided_ = true; |
---|
| 168 | reachedLvlEndState_++; |
---|
[11400] | 169 | |
---|
[11416] | 170 | } |
---|
| 171 | |
---|
[11412] | 172 | return true; |
---|
| 173 | } |
---|
[11383] | 174 | |
---|
[11379] | 175 | |
---|
[11416] | 176 | //Self implemented sign function that returns either 1 or -1 (and never 0) |
---|
[11412] | 177 | int SOBFigure::sgn(float x) { |
---|
| 178 | if (x < 0.0) return -1; |
---|
| 179 | return 1; |
---|
| 180 | } |
---|
| 181 | |
---|
[11416] | 182 | //For those of you who don't have an idea: the tick function is called about 50 times/sec |
---|
[11412] | 183 | void SOBFigure::tick(float dt) |
---|
| 184 | { |
---|
| 185 | SUPER(SOBFigure, tick, dt); |
---|
| 186 | |
---|
[11416] | 187 | |
---|
| 188 | bool inputAllowed = true; |
---|
| 189 | |
---|
| 190 | //the particle spawner that generates the fire from the backpack when pressed |
---|
[11412] | 191 | if (particlespawner_ == NULL) { |
---|
| 192 | for (WorldEntity* object : this->getAttachedObjects()) |
---|
| 193 | { |
---|
[11416] | 194 | if (object->isA(Class(ParticleSpawner))) |
---|
[11412] | 195 | particlespawner_ = object; |
---|
[11416] | 196 | } |
---|
[11405] | 197 | } |
---|
[11400] | 198 | |
---|
| 199 | |
---|
[11416] | 200 | //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 |
---|
| 201 | if (reachedLvlEndState_ != 0) { |
---|
| 202 | timeCounter_+= dt; |
---|
| 203 | inputAllowed = false; |
---|
| 204 | } |
---|
| 205 | if (reachedLvlEndState_ == 1 && timeCounter_ >= 1.5) { |
---|
| 206 | timeCounter_ = 0; |
---|
| 207 | reachedLvlEndState_ = 2; |
---|
| 208 | } |
---|
[11400] | 209 | |
---|
[11402] | 210 | |
---|
[11416] | 211 | //if input blocked, then cancel every movement operation |
---|
| 212 | if (!inputAllowed) { |
---|
| 213 | moveUpPressed_ = false; |
---|
| 214 | moveDownPressed_ = false; |
---|
| 215 | moveLeftPressed_ = false; |
---|
| 216 | moveRightPressed_ = false; |
---|
| 217 | } |
---|
[11400] | 218 | |
---|
[11416] | 219 | //set the gravityto standard 350 |
---|
| 220 | if (firePressed_ == false) { |
---|
| 221 | gravityAcceleration_ = 350.0; |
---|
[11400] | 222 | |
---|
[11416] | 223 | } |
---|
[11379] | 224 | |
---|
[11416] | 225 | if (hasLocalController()) |
---|
| 226 | { |
---|
| 227 | Vector3 velocity = getVelocity(); |
---|
| 228 | Vector3 position = getPosition(); |
---|
[11402] | 229 | |
---|
[11416] | 230 | if (!predead_) |
---|
| 231 | velocity.y = 0; |
---|
[11418] | 232 | //If player falls in a hole |
---|
[11416] | 233 | if (position.z < -100) { |
---|
| 234 | dead_ = true; |
---|
[11418] | 235 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
---|
| 236 | SOBGame->setDone(true); |
---|
[11416] | 237 | } |
---|
[11402] | 238 | |
---|
[11381] | 239 | |
---|
[11416] | 240 | if (dead_) { |
---|
| 241 | velocity.x = 0; |
---|
| 242 | velocity.z = 0; |
---|
| 243 | setVelocity(velocity); |
---|
| 244 | SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); |
---|
| 245 | if (firePressed_) |
---|
| 246 | SOBGame->restart(); |
---|
| 247 | return; |
---|
| 248 | } |
---|
[11379] | 249 | |
---|
| 250 | |
---|
[11416] | 251 | int maxvelocity_x = 100; |
---|
| 252 | int speedAddedPerTick = 5; |
---|
| 253 | int camMaxOffset = 25; |
---|
[11381] | 254 | |
---|
[11416] | 255 | timeSinceLastFire_ += dt; |
---|
| 256 | lastSpeed_z = velocity.z; |
---|
[11381] | 257 | |
---|
| 258 | |
---|
[11405] | 259 | |
---|
[11416] | 260 | //Handle the rocket fire from the jetpack |
---|
| 261 | if (velocity.z > 40) |
---|
| 262 | particlespawner_->setVisible(true); |
---|
| 263 | else |
---|
| 264 | particlespawner_->setVisible(false); |
---|
[11405] | 265 | |
---|
[11412] | 266 | |
---|
[11418] | 267 | //If player hits space and collides against an object under him then jump |
---|
[11517] | 268 | if (inputAllowed && firePressed_ && isColliding_ && (collDisZ_ >= 0 && collDisZ_ <+ 10)) { |
---|
| 269 | //if (inputAllowed && firePressed_ && isColliding_ ) { |
---|
| 270 | |
---|
[11416] | 271 | gravityAcceleration_ = 100.0; |
---|
[11418] | 272 | velocity.z = 110; |
---|
[11392] | 273 | } |
---|
[11381] | 274 | |
---|
[11392] | 275 | |
---|
[11420] | 276 | //Left-right movement with acceleration and rotation |
---|
[11400] | 277 | float rot = getOrientation().getRoll().valueDegrees(); |
---|
[11383] | 278 | if (moveRightPressed_) { |
---|
[11412] | 279 | if (!(rot < 5.0 && -5.0 < rot)) |
---|
| 280 | setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() - sgn(rot)*dt*Radian(6)); |
---|
[11400] | 281 | |
---|
[11383] | 282 | if (std::abs(velocity.x) < maxvelocity_x) { |
---|
| 283 | velocity.x += speedAddedPerTick; |
---|
[11400] | 284 | |
---|
[11379] | 285 | } |
---|
[11383] | 286 | } else if (moveLeftPressed_) { |
---|
[11412] | 287 | if (!(abs(rot) > 175.0 )) |
---|
| 288 | setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() + sgn(rot)*dt*Radian(6)); |
---|
[11400] | 289 | |
---|
[11412] | 290 | |
---|
| 291 | |
---|
[11383] | 292 | if (std::abs(velocity.x) < maxvelocity_x) { |
---|
| 293 | velocity.x -= speedAddedPerTick; |
---|
[11379] | 294 | } |
---|
[11383] | 295 | } else { |
---|
| 296 | velocity.x /= 1.1; |
---|
[11392] | 297 | } |
---|
[11379] | 298 | |
---|
[11392] | 299 | |
---|
[11416] | 300 | //Again another EndOfLevel behavior |
---|
| 301 | if (reachedLvlEndState_ == 1) |
---|
| 302 | velocity.x = -2; |
---|
| 303 | if (reachedLvlEndState_ == 2) |
---|
| 304 | velocity.x = 30; |
---|
| 305 | if (reachedLvlEndState_ == 3) { |
---|
| 306 | velocity.x = 0; |
---|
| 307 | velocity.y = 20; |
---|
| 308 | } |
---|
| 309 | if (reachedLvlEndState_ == 4) { |
---|
| 310 | lvlEnded_ = true; |
---|
| 311 | dead_ = true; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | //velocity = acc. * time |
---|
[11383] | 315 | velocity.z -= gravityAcceleration_*dt; |
---|
| 316 | setVelocity(velocity); |
---|
[11379] | 317 | |
---|
| 318 | |
---|
[11416] | 319 | //Camera operation - the camera should always follow the player in a specific region |
---|
[11383] | 320 | Camera* cam = getCamera(); |
---|
| 321 | Vector3 campos = cam->getPosition(); |
---|
[11379] | 322 | |
---|
[11383] | 323 | if (campos.x + camMaxOffset < position.x) { |
---|
| 324 | campos.x = position.x - camMaxOffset; |
---|
| 325 | cam->setPosition(campos); |
---|
[11379] | 326 | } |
---|
[11405] | 327 | if (campos.x - camMaxOffset > position.x) { |
---|
[11383] | 328 | campos.x = position.x + camMaxOffset; |
---|
| 329 | cam->setPosition(campos); |
---|
| 330 | } |
---|
[11379] | 331 | |
---|
[11383] | 332 | |
---|
| 333 | |
---|
[11392] | 334 | |
---|
[11383] | 335 | } |
---|
| 336 | |
---|
| 337 | |
---|
[11379] | 338 | |
---|
[11416] | 339 | // Reset key variables |
---|
[11383] | 340 | moveUpPressed_ = false; |
---|
| 341 | moveDownPressed_ = false; |
---|
| 342 | moveLeftPressed_ = false; |
---|
| 343 | moveRightPressed_ = false; |
---|
[11416] | 344 | |
---|
[11400] | 345 | isColliding_ = false; |
---|
[11418] | 346 | collDisZ_ = 0; |
---|
[11379] | 347 | |
---|
[11383] | 348 | } |
---|
[11379] | 349 | |
---|
| 350 | |
---|
[11383] | 351 | |
---|
| 352 | |
---|
| 353 | |
---|
[11416] | 354 | //The following functions read the input of the player and then set the bools for the movement |
---|
[11383] | 355 | void SOBFigure::moveFrontBack(const Vector2& value) |
---|
| 356 | { |
---|
| 357 | if (value.x > 0) |
---|
[11379] | 358 | { |
---|
[11383] | 359 | moveUpPressed_ = true; |
---|
| 360 | moveDownPressed_ = false; |
---|
[11379] | 361 | } |
---|
[11383] | 362 | else |
---|
| 363 | { |
---|
| 364 | moveUpPressed_ = false; |
---|
| 365 | moveDownPressed_ = true; |
---|
| 366 | } |
---|
| 367 | } |
---|
[11379] | 368 | |
---|
[11383] | 369 | void SOBFigure::moveRightLeft(const Vector2& value) |
---|
| 370 | { |
---|
| 371 | if (value.x > 0) |
---|
[11379] | 372 | { |
---|
[11383] | 373 | moveLeftPressed_ = false; |
---|
| 374 | moveRightPressed_ = true; |
---|
[11379] | 375 | } |
---|
[11383] | 376 | else |
---|
| 377 | { |
---|
| 378 | moveLeftPressed_ = true; |
---|
| 379 | moveRightPressed_ = false; |
---|
| 380 | } |
---|
| 381 | } |
---|
[11379] | 382 | |
---|
[11383] | 383 | void SOBFigure::boost(bool boost) |
---|
| 384 | { |
---|
[11400] | 385 | firePressed_ = boost; |
---|
[11379] | 386 | } |
---|
[11416] | 387 | |
---|
| 388 | |
---|
[11538] | 389 | |
---|
| 390 | // PRE: name is an existing name of a material. Example orxo_material for orxo_material.material in data_extern/materials |
---|
| 391 | // POST: clothes of body of player are changed to name |
---|
| 392 | void SOBFigure::changeClothes(std::string& name){ |
---|
| 393 | std::set<WorldEntity*> attachedObjects = this->getAttachedObjects(); |
---|
| 394 | std::set<WorldEntity*>::iterator it; |
---|
| 395 | for (it = attachedObjects.begin(); it != attachedObjects.end(); ++it) |
---|
| 396 | { |
---|
| 397 | Model* FiguresModel = orxonox_cast<Model*>(*it); |
---|
| 398 | if (FiguresModel != nullptr) |
---|
| 399 | { |
---|
| 400 | |
---|
| 401 | FiguresModel->setSubMaterial(name, 4); // 4 is the body |
---|
| 402 | |
---|
| 403 | } |
---|
| 404 | } |
---|
[11383] | 405 | } |
---|
[11538] | 406 | |
---|
| 407 | } |
---|