Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11769 was 11769, checked in by landauf, 7 years ago

[SOBv2_HS17] removed unused variables, added some initializations

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