Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SOBv2_HS17/src/modules/superorxobros/SOBFigure.cc @ 11628

Last change on this file since 11628 was 11628, checked in by varxth, 7 years ago

more improvements

File size: 14.8 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        moveDownPressed_    = false;
72        firePressed_        = false;
73        collDisZ_           = 0;
74
75        //Times and turning
76        timeSinceLastFire_  = 0.0;
77        lastSpeed_z         = 0.0;
78        pitch_              = 0.0;
79        timeCounter_        = 0;
80
81        //Properties of player
82
83        isColliding_        = true;
84        particlespawner_    = NULL;
85
86        //Properties of players life
87        predead_            = false;
88        dead_               = false;
89        lvlEnded_           = false;
90        reachedLvlEndState_ = 0;
91
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
100       
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
103    }
104
105
106
107    bool SOBFigure::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) {
108
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
110        isColliding_ = true;
111        collDisZ_ = getPosition().z - contactPoint.getPositionWorldOnB().getZ();
112
113
114        //Orxocast returns object with casted type if otherObject has that class, and if not a nullptr
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()); 
122
123
124        //Check if otherObject is a powerup-mushroom
125        if (mush != nullptr && !(mush->hasCollided_)) {
126            otherObject->destroyLater();
127
128            PowerUpCounter_++;
129            if(PowerUpCounter_ > maxPowerUp_)   PowerUpCounter_ = maxPowerUp_; // you had already the max
130            else                                this->changeClothes();
131
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
134           
135           
136        }
137
138       
139
140        //Check if otherObject is a Gumba (that walking enemies)
141
142         else if (gumba != nullptr && gumbaBoss == nullptr && !(gumba->hasCollided_)) {
143
144            //If player jumps on its head, kill the Gumba, else, kill the player
145            if (getVelocity().z >= -20) {
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();
153
154                    gumba->destroyLater();
155                    gumba->hasCollided_ = true;
156                }
157
158          } else {
159            gumba->destroyLater();
160            gumba->hasCollided_ = true;
161            SOBGame->addGumba();
162
163
164            }
165        }
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            }
171
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
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;
191
192        SOBGame->setDone(true);
193        SOBGame->addPoints(flagstone->getPoints());
194       
195
196    }
197    if (castlestone != nullptr && !(castlestone->hasCollided_)) {
198        castlestone->hasCollided_ = true;
199        reachedLvlEndState_++;
200
201    }
202
203    return true;
204}
205
206
207//Self implemented sign function that returns either 1 or -1 (and never 0)
208int SOBFigure::sgn(float x) {
209    if (x < 0.0) return -1;
210    return 1;
211}
212
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
239//For those of you who don't have an idea: the tick function is called about 50 times/sec
240void SOBFigure::tick(float dt)
241{
242    SUPER(SOBFigure, tick, dt);
243
244
245    bool inputAllowed = true;
246    //SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
247
248    //the particle spawner that generates the fire from the backpack when pressed
249    if (particlespawner_ == NULL) {
250        for (WorldEntity* object : this->getAttachedObjects())
251        {
252           if (object->isA(Class(ParticleSpawner)))
253            particlespawner_ = object;
254        }
255
256    }
257   
258
259
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    }
269
270
271    //if input blocked, then cancel every movement operation
272    if (!inputAllowed) {
273        moveUpPressed_      = false;
274        moveDownPressed_    = false;
275        moveLeftPressed_    = false;
276        moveRightPressed_   = false;
277    }
278
279    //set the gravityto standard 350
280    if (firePressed_ == false) {
281        gravityAcceleration_ = 350.0;
282
283    }
284
285    if (hasLocalController())
286    {
287        SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
288        Vector3 velocity = getVelocity();
289        Vector3 position = getPosition();
290
291        if (!predead_)
292            velocity.y = 0;
293        //If player falls in a hole
294        if (position.z < -100) {
295            dead_ = true;
296            SOBGame->setDone(true);
297        }
298
299
300        if (dead_) {
301            velocity.x = 0;
302            velocity.z = 0;
303            setVelocity(velocity);
304           
305            if (firePressed_)
306                SOBGame->restart();
307            return;
308        }
309
310
311        int maxvelocity_x = 100;
312        int speedAddedPerTick = 5;
313        int camMaxOffset = 25;
314
315        timeSinceLastFire_ += dt;
316        lastSpeed_z = velocity.z;
317
318
319
320        //Handle the rocket fire from the jetpack
321        if (velocity.z > 40)
322            particlespawner_->setVisible(true); 
323        else
324            particlespawner_->setVisible(false); 
325       
326
327        //If player hits space and collides against an object under him then jump
328        if (inputAllowed && firePressed_ && isColliding_ && (collDisZ_ >= 0 && collDisZ_ <+ 10)) {
329            gravityAcceleration_ = 350;
330            velocity.z = 175; 
331        }
332
333
334        //Left-right movement with acceleration and rotation
335        float rot = getOrientation().getRoll().valueDegrees();
336        if (moveRightPressed_) {
337            if (!(rot < 5.0 && -5.0 < rot))
338                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() - sgn(rot)*dt*Radian(6));
339
340            if (std::abs(velocity.x) < maxvelocity_x) {
341                velocity.x += speedAddedPerTick;
342
343            }
344        } else if (moveLeftPressed_) {
345            if (!(abs(rot) > 175.0 ))
346                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() + sgn(rot)*dt*Radian(6));
347
348
349
350            if (std::abs(velocity.x) < maxvelocity_x) {
351                velocity.x -= speedAddedPerTick;
352            }
353        } else {
354            velocity.x /= 1.1;
355        }
356
357        //If moveUp pressed, fire a fireball
358        if(moveUpPressed_ && (PowerUpCounter_ >= FireballPower) && fireallowed_)
359        {
360            spawnFireball();
361            fireallowed_  = false;
362            firecooldown_ = 0;
363        }
364
365        //Increase the firecooldown
366        if(firecooldown_> 0.5)
367        {
368            fireallowed_ = true;
369        }
370        if(!fireallowed_)
371        {
372            firecooldown_ += dt;
373        }
374
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;
383            setOrientation(Vector3::UNIT_Z, Degree(90));
384        }
385        if (reachedLvlEndState_ == 4) {
386            //Highscore
387            if (Highscore::exists()){
388                    int score = SOBGame->getPoints();
389                    if(score > Highscore::getInstance().getHighestScoreOfGame("Super Orxo Bros.")) 
390                        Highscore::getInstance().storeHighscore("Super Orxo Bros.",score);
391                        SOBGame->newHighscore=1;
392
393            }
394            lvlEnded_ = true;
395            dead_ = true;
396        }
397
398        //velocity = acc. * time
399        velocity.z -= gravityAcceleration_*dt;
400        setVelocity(velocity);
401
402
403        //Camera operation - the camera should always follow the player in a specific region
404        Camera* cam = getCamera();
405        Vector3 campos = cam->getPosition();
406
407        if (campos.x + camMaxOffset < position.x) {
408            campos.x = position.x - camMaxOffset;
409            cam->setPosition(campos);
410        }
411        if (campos.x - camMaxOffset > position.x) {
412            campos.x = position.x + camMaxOffset;
413            cam->setPosition(campos);
414        }
415
416    }
417
418    // Reset key variables
419    moveUpPressed_      = false;
420    moveDownPressed_    = false;
421    moveLeftPressed_    = false;
422    moveRightPressed_   = false;
423
424    isColliding_ = false;
425    collDisZ_ = 0;
426
427}
428
429
430
431
432
433//The following functions read the input of the player and then set the bools for the movement
434void SOBFigure::moveFrontBack(const Vector2& value)
435{
436    if (value.x > 0)
437    {
438        moveUpPressed_ = true;
439        moveDownPressed_ = false;
440    }
441    else
442    {
443        moveUpPressed_ = false;
444        moveDownPressed_ = true;
445    }
446}
447
448void SOBFigure::moveRightLeft(const Vector2& value)
449{
450    if (value.x > 0)
451    {
452        moveLeftPressed_ = false;
453        moveRightPressed_ = true;
454    }
455    else
456    {
457        moveLeftPressed_ = true;
458        moveRightPressed_ = false;
459    }
460}
461
462void SOBFigure::boost(bool boost)
463{
464    firePressed_ = boost;
465}
466
467
468
469// PRE: name is an existing name of a material. Example orxo_material for orxo_material.material in data_extern/materials
470//      !!! PowerUpCounter_ has to be modified before changing the clothes!!!
471// POST: clothes of body of player are changed to name
472void SOBFigure::changeClothes(){
473            // clothes: white (basic), red (one PowerUp), orange (Fireball enabled)
474            std::string clothes[] = {"orxo_material", "orxo_material_gross", "orxo_material_fire"};
475
476            std::set<WorldEntity*> attachedObjects = this->getAttachedObjects();
477            std::set<WorldEntity*>::iterator it;
478            for (it = attachedObjects.begin(); it != attachedObjects.end(); ++it)
479            {
480                Model* FiguresModel = orxonox_cast<Model*>(*it);
481                if (FiguresModel != nullptr)
482                {
483                    FiguresModel->setSubMaterial(clothes[PowerUpCounter_] , 4); // 4 is the body
484                }
485            }   
486}
487// PRE:
488// POST: Player jumps out of the game, game is finished and can be restarted.
489void SOBFigure::die(){
490    Vector3 vel = getVelocity();
491    vel.y = -80;
492    vel.z = 200;
493    setVelocity(vel);
494    predead_= true; 
495    SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
496    SOBGame->setDone(true);
497}
498
499}
Note: See TracBrowser for help on using the repository browser.