Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Presentation_HS17_merge/src/modules/superorxobros/SOBFigure.cc @ 11768

Last change on this file since 11768 was 11768, checked in by landauf, 6 years ago

[SOBv2_HS17] fixed build (Highscopre API has changed)

File size: 14.8 KB
RevLine 
[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:
[11766]25 *      Noah Zarro
26 *      Theo von Arx
[11416]27 *     
[11379]28 *
29 */
30
31/**
32    @file SOBFigure.cc
33    @brief This class represents your figure when you play the minigame. Here the movement of the figure, activating items, ... are handled.
34*/
35
36#include "SOBFigure.h"
37
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40#include "graphics/Model.h"
[11383]41#include "graphics/Camera.h"
[11400]42#include "graphics/ParticleSpawner.h"
[11766]43#include <OgreMath.h>
[11379]44
[11402]45#include "SOBMushroom.h"
[11412]46#include "SOBGumba.h"
[11766]47#include "SOBGumbaBoss.h"
48#include "SOBFireball.h"
[11405]49#include "SOB.h"
[11412]50#include "SOBFlagstone.h"
[11416]51#include "SOBCastlestone.h"
[11766]52#include "SOBFireball.h"
53#include "Highscore.h"
[11418]54#include <BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
[11400]55
[11379]56namespace orxonox
57{
58    RegisterClass(SOBFigure);
59
60    SOBFigure::SOBFigure(Context* context) : ControllableEntity(context)
61    {
62        RegisterObject(SOBFigure);
63
64        // initialize variables
[11416]65        gravityAcceleration_ = 350.0;
[11383]66
[11416]67        //Vars for movement of player
[11766]68        moveUpPressed_      = false;
69        moveDownPressed_    = false;
70        moveLeftPressed_    = false;
71        moveDownPressed_    = false;
72        firePressed_        = false;
73        collDisZ_           = 0;
74
[11416]75        //Times and turning
[11766]76        timeSinceLastFire_  = 0.0;
77        lastSpeed_z         = 0.0;
78        pitch_              = 0.0;
79        timeCounter_        = 0;
[11416]80
81        //Properties of player
[11383]82
[11766]83        isColliding_        = true;
84        particlespawner_    = NULL;
85
[11416]86        //Properties of players life
[11766]87        predead_            = false;
88        dead_               = false;
89        lvlEnded_           = false;
[11416]90        reachedLvlEndState_ = 0;
91
[11766]92        // Properties concerning PowerUps and items
93        PowerUpCounter_     = 0;
94        maxPowerUp_         = 2;
95        FireballPower       = 2;
96        //Properties of fireing Fireballs, NOTE! fireballs are fired with the moveUP Key, not with the fire key
97        fireallowed_        = true;
98        firecooldown_       = 0;
99
[11405]100       
[11416]101        setAngularFactor(0.0); //Means player doesn't turn on collision, so he doesn't fall over while walking over the ground
102        this->enableCollisionCallback(); // Turns on that on every collision function collidesAgainst is executed
[11379]103    }
104
[11400]105
106
107    bool SOBFigure::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) {
108
[11418]109        //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]110        isColliding_ = true;
[11418]111        collDisZ_ = getPosition().z - contactPoint.getPositionWorldOnB().getZ();
[11416]112
[11418]113
[11416]114        //Orxocast returns object with casted type if otherObject has that class, and if not a nullptr
[11766]115        SOBMushroom*    mush        = orxonox_cast<SOBMushroom*>    (otherObject);
116        SOBGumba*       gumba       = orxonox_cast<SOBGumba*>       (otherObject);
117        SOBGumbaBoss*   gumbaBoss   = orxonox_cast<SOBGumbaBoss*>   (otherObject);
118        SOBFlagstone*   flagstone   = orxonox_cast<SOBFlagstone*>   (otherObject);
119        SOBCastlestone* castlestone = orxonox_cast<SOBCastlestone*> (otherObject);
120        SOBFireball*    fireball    = orxonox_cast<SOBFireball*>    (otherObject);
121        SOB* SOBGame                = orxonox_cast<SOB*>            (getGametype()); 
[11412]122
[11766]123
124        //Check if otherObject is a powerup-mushroom
[11405]125        if (mush != nullptr && !(mush->hasCollided_)) {
126            otherObject->destroyLater();
[11766]127
128            PowerUpCounter_++;
129            if(PowerUpCounter_ > maxPowerUp_)   PowerUpCounter_ = maxPowerUp_; // you had already the max
130            else                                this->changeClothes();
131
[11416]132            SOBGame->addMushroom(); // Tell the gametype to increase points
133            mush->hasCollided_ = true; // needed because of destroyLater takes some time and player should receive points only once
[11766]134           
135           
136        }
[11405]137
[11766]138       
139
[11416]140        //Check if otherObject is a Gumba (that walking enemies)
[11412]141
[11766]142         else if (gumba != nullptr && gumbaBoss == nullptr && !(gumba->hasCollided_)) {
143
144            //If player jumps on its head, kill the Gumba, else, kill the player
[11412]145            if (getVelocity().z >= -20) {
[11766]146                // If player hasn't a power up, he dies. Else he shrinks and the gumba dies.
147                if(PowerUpCounter_ == 0){
148                    this->die();
149                } 
150                else{
151                    PowerUpCounter_--;
152                    this->changeClothes();
[11416]153
[11766]154                    gumba->destroyLater();
155                    gumba->hasCollided_ = true;
156                }
157
[11412]158          } else {
159            gumba->destroyLater();
160            gumba->hasCollided_ = true;
161            SOBGame->addGumba();
162
163
[11766]164            }
[11402]165        }
[11766]166        else if (gumbaBoss != nullptr && !(gumbaBoss->hasCollided_)) {
167            if (getVelocity().z >= -20) {
168                // If player hasn't a power up, he dies. Else he dies directly.
169                this->die(); 
170            }
[11400]171
[11766]172            else {
173                gumbaBoss->destroyLater();
174                gumbaBoss->hasCollided_ = true;
175                SOBGame->addGumbaBoss();
176                }
177        }
178        else if (fireball != nullptr && !(fireball->hasCollided_)){
179            if(PowerUpCounter_ == 0){
180                    this->die();
181                } 
182            PowerUpCounter_--;
183            this->changeClothes();
184            fireball->destroyLater();
185        }
186
[11416]187    //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
188    if (reachedLvlEndState_ == 0 && flagstone != nullptr && !(flagstone->hasCollided_)) {
189        flagstone->hasCollided_ = true;
190        reachedLvlEndState_ = 1;
[11766]191
[11418]192        SOBGame->setDone(true);
[11416]193        SOBGame->addPoints(flagstone->getPoints());
[11418]194       
[11400]195
[11416]196    }
197    if (castlestone != nullptr && !(castlestone->hasCollided_)) {
198        castlestone->hasCollided_ = true;
199        reachedLvlEndState_++;
[11400]200
[11416]201    }
202
[11412]203    return true;
204}
[11383]205
[11379]206
[11416]207//Self implemented sign function that returns either 1 or -1 (and never 0)
[11412]208int SOBFigure::sgn(float x) {
209    if (x < 0.0) return -1;
210    return 1;
211}
212
[11766]213//Function to spawn the Fireball
214void SOBFigure::spawnFireball() {
215        SOBCenterpoint* center_ = ((SOB*)getGametype())->center_;
216
217         SOBFireball* ball = new SOBFireball(center_->getContext());
218         Vector3 spawnpos = this->getWorldPosition();
219         spawnpos.z += 0;
220
221        if (ball != nullptr && center_ != nullptr)
222        {
223            ball->addTemplate("fireball");
224            bool direction = ((this->getWorldOrientation().getRoll().valueRadians())>-1.6&&(this->getWorldOrientation().getRoll().valueRadians()<1.6));
225            ball->setDirection(direction);
226            if(direction)
227            {
228                spawnpos.x+=10;
229            }
230            else
231            {
232                spawnpos.x-=10;
233            }
234            ball->setPosition(spawnpos);
235
236        }
237     }
238
[11416]239//For those of you who don't have an idea: the tick function is called about 50 times/sec
[11412]240void SOBFigure::tick(float dt)
241{
242    SUPER(SOBFigure, tick, dt);
243
[11416]244
245    bool inputAllowed = true;
[11766]246    //SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
[11416]247
248    //the particle spawner that generates the fire from the backpack when pressed
[11412]249    if (particlespawner_ == NULL) {
250        for (WorldEntity* object : this->getAttachedObjects())
251        {
[11416]252           if (object->isA(Class(ParticleSpawner)))
[11412]253            particlespawner_ = object;
[11416]254        }
[11766]255
[11405]256    }
[11766]257   
[11400]258
259
[11416]260    //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
261    if (reachedLvlEndState_ != 0) {
262        timeCounter_+= dt;
263        inputAllowed = false;
264    }
265    if (reachedLvlEndState_ == 1 && timeCounter_ >= 1.5) {
266        timeCounter_ = 0;
267        reachedLvlEndState_ = 2;
268    }
[11400]269
[11402]270
[11416]271    //if input blocked, then cancel every movement operation
272    if (!inputAllowed) {
[11766]273        moveUpPressed_      = false;
274        moveDownPressed_    = false;
275        moveLeftPressed_    = false;
276        moveRightPressed_   = false;
[11416]277    }
[11400]278
[11416]279    //set the gravityto standard 350
280    if (firePressed_ == false) {
281        gravityAcceleration_ = 350.0;
[11400]282
[11416]283    }
[11379]284
[11416]285    if (hasLocalController())
286    {
[11766]287        SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
[11416]288        Vector3 velocity = getVelocity();
289        Vector3 position = getPosition();
[11402]290
[11416]291        if (!predead_)
292            velocity.y = 0;
[11418]293        //If player falls in a hole
[11416]294        if (position.z < -100) {
295            dead_ = true;
[11418]296            SOBGame->setDone(true);
[11416]297        }
[11402]298
[11381]299
[11416]300        if (dead_) {
301            velocity.x = 0;
302            velocity.z = 0;
303            setVelocity(velocity);
[11766]304           
[11416]305            if (firePressed_)
306                SOBGame->restart();
307            return;
308        }
[11379]309
310
[11416]311        int maxvelocity_x = 100;
312        int speedAddedPerTick = 5;
313        int camMaxOffset = 25;
[11381]314
[11416]315        timeSinceLastFire_ += dt;
316        lastSpeed_z = velocity.z;
[11381]317
318
[11405]319
[11416]320        //Handle the rocket fire from the jetpack
321        if (velocity.z > 40)
322            particlespawner_->setVisible(true); 
323        else
324            particlespawner_->setVisible(false); 
[11766]325       
[11405]326
[11418]327        //If player hits space and collides against an object under him then jump
[11766]328        if (inputAllowed && firePressed_ && isColliding_ && (collDisZ_ >= 0 && collDisZ_ <+ 10)) {
329            gravityAcceleration_ = 350;
330            velocity.z = 175; 
[11392]331        }
[11381]332
[11392]333
[11420]334        //Left-right movement with acceleration and rotation
[11400]335        float rot = getOrientation().getRoll().valueDegrees();
[11383]336        if (moveRightPressed_) {
[11412]337            if (!(rot < 5.0 && -5.0 < rot))
338                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() - sgn(rot)*dt*Radian(6));
[11400]339
[11383]340            if (std::abs(velocity.x) < maxvelocity_x) {
341                velocity.x += speedAddedPerTick;
[11400]342
[11379]343            }
[11383]344        } else if (moveLeftPressed_) {
[11412]345            if (!(abs(rot) > 175.0 ))
346                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() + sgn(rot)*dt*Radian(6));
[11400]347
[11412]348
349
[11383]350            if (std::abs(velocity.x) < maxvelocity_x) {
351                velocity.x -= speedAddedPerTick;
[11379]352            }
[11383]353        } else {
354            velocity.x /= 1.1;
[11392]355        }
[11379]356
[11766]357        //If moveUp pressed, fire a fireball
358        if(moveUpPressed_ && (PowerUpCounter_ >= FireballPower) && fireallowed_)
359        {
360            spawnFireball();
361            fireallowed_  = false;
362            firecooldown_ = 0;
363        }
[11392]364
[11766]365        //Increase the firecooldown
366        if(firecooldown_> 0.5)
367        {
368            fireallowed_ = true;
369        }
370        if(!fireallowed_)
371        {
372            firecooldown_ += dt;
373        }
374
[11416]375        //Again another EndOfLevel behavior
376        if (reachedLvlEndState_ == 1)
377            velocity.x = -2;
378        if (reachedLvlEndState_ == 2)
379            velocity.x = 30;
380        if (reachedLvlEndState_ == 3) {
381            velocity.x = 0;
382            velocity.y = 20;
[11766]383            setOrientation(Vector3::UNIT_Z, Degree(90));
[11416]384        }
385        if (reachedLvlEndState_ == 4) {
[11766]386            //Highscore
[11768]387            if (Highscore::exists())
388            {
389                int score = SOBGame->getPoints();
390                bool isHighScore = Highscore::getInstance().storeScore("Super Orxo Bros.", score, this->getPlayer());
391                SOBGame->newHighscore = isHighScore;
[11766]392            }
[11416]393            lvlEnded_ = true;
394            dead_ = true;
395        }
396
397        //velocity = acc. * time
[11383]398        velocity.z -= gravityAcceleration_*dt;
399        setVelocity(velocity);
[11379]400
401
[11416]402        //Camera operation - the camera should always follow the player in a specific region
[11383]403        Camera* cam = getCamera();
404        Vector3 campos = cam->getPosition();
[11379]405
[11383]406        if (campos.x + camMaxOffset < position.x) {
407            campos.x = position.x - camMaxOffset;
408            cam->setPosition(campos);
[11379]409        }
[11405]410        if (campos.x - camMaxOffset > position.x) {
[11383]411            campos.x = position.x + camMaxOffset;
412            cam->setPosition(campos);
413        }
[11379]414
[11383]415    }
416
[11416]417    // Reset key variables
[11766]418    moveUpPressed_      = false;
419    moveDownPressed_    = false;
420    moveLeftPressed_    = false;
421    moveRightPressed_   = false;
[11416]422
[11400]423    isColliding_ = false;
[11418]424    collDisZ_ = 0;
[11379]425
[11383]426}
[11379]427
428
[11383]429
430
431
[11416]432//The following functions read the input of the player and then set the bools for the movement
[11383]433void SOBFigure::moveFrontBack(const Vector2& value)
434{
435    if (value.x > 0)
[11379]436    {
[11383]437        moveUpPressed_ = true;
438        moveDownPressed_ = false;
[11379]439    }
[11383]440    else
441    {
442        moveUpPressed_ = false;
443        moveDownPressed_ = true;
444    }
445}
[11379]446
[11383]447void SOBFigure::moveRightLeft(const Vector2& value)
448{
449    if (value.x > 0)
[11379]450    {
[11383]451        moveLeftPressed_ = false;
452        moveRightPressed_ = true;
[11379]453    }
[11383]454    else
455    {
456        moveLeftPressed_ = true;
457        moveRightPressed_ = false;
458    }
459}
[11379]460
[11383]461void SOBFigure::boost(bool boost)
462{
[11400]463    firePressed_ = boost;
[11379]464}
[11416]465
466
[11766]467
468// PRE: name is an existing name of a material. Example orxo_material for orxo_material.material in data_extern/materials
469//      !!! PowerUpCounter_ has to be modified before changing the clothes!!!
470// POST: clothes of body of player are changed to name
471void SOBFigure::changeClothes(){
472            // clothes: white (basic), red (one PowerUp), orange (Fireball enabled)
473            std::string clothes[] = {"orxo_material", "orxo_material_gross", "orxo_material_fire"};
474
475            std::set<WorldEntity*> attachedObjects = this->getAttachedObjects();
476            std::set<WorldEntity*>::iterator it;
477            for (it = attachedObjects.begin(); it != attachedObjects.end(); ++it)
478            {
479                Model* FiguresModel = orxonox_cast<Model*>(*it);
480                if (FiguresModel != nullptr)
481                {
482                    FiguresModel->setSubMaterial(clothes[PowerUpCounter_] , 4); // 4 is the body
483                }
484            }   
[11383]485}
[11766]486// PRE:
487// POST: Player jumps out of the game, game is finished and can be restarted.
488void SOBFigure::die(){
489    Vector3 vel = getVelocity();
490    vel.y = -80;
491    vel.z = 200;
492    setVelocity(vel);
493    predead_= true; 
494    SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
495    SOBGame->setDone(true);
496}
497
[11768]498}
Note: See TracBrowser for help on using the repository browser.