Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Presentation_HS17_merge/src/modules/flappyorx/FlappyOrx.cc @ 11759

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

[FlappyOrx_HS17] initialize all variables

File size: 8.4 KB
RevLine 
[11481]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:
[11624]23 *      Leo Merholz
[11595]24 *      Pascal Schärli
[11624]25 *   Co-authors:
26 *      ...
[11481]27 *
28 */
29
30/**
31    @file FlappyOrx.cc
[11503]32    @brief Implementation of the FlappyOrx class.
[11481]33*/
34
[11503]35#include "FlappyOrx.h"
36#include "Highscore.h"
37#include "core/CoreIncludes.h"
[11620]38
39
[11503]40#include "core/EventIncludes.h"
41#include "core/command/Executor.h"
42#include "core/config/ConfigValueIncludes.h"
[11600]43#include "core/XMLPort.h"
[11503]44#include "gamestates/GSLevel.h"
45#include "chat/ChatManager.h"
46
47// ! HACK
48#include "infos/PlayerInfo.h"
49
50#include "FlappyOrxShip.h"
51
52#include "core/command/ConsoleCommand.h"
53#include "worldentities/ExplosionPart.h"
[11580]54#include <vector>
[11503]55
[11481]56namespace orxonox
57{
58    RegisterUnloadableClass(FlappyOrx);
59
60    FlappyOrx::FlappyOrx(Context* context) : Deathmatch(context)
61    {
62        RegisterObject(FlappyOrx);
[11757]63
[11595]64        point = 0;                          //number of cleared tubes
[11565]65        bIsDead = true;
[11595]66        firstGame = true;                   //needed for the HUD
[11620]67
[11759]68        speedBase = 0;
69        speedIncrease = 0;
70        tubeDistanceBase = 0;
71        tubeDistanceIncrease = 0;
72        tubeDistance = 200.0f;                  //distance between tubes
73        tubeOffsetX = 500.0f;                    //tube offset (so that we can't see them spawn)
[11598]74
75        circlesUsed=0;
[11595]76        setHUDTemplate("FlappyOrxHUD");
[11481]77    }
[11600]78   
[11755]79    void FlappyOrx::updatePlayerPos(float x){
[11537]80
[11595]81        //Spawn a new Tube when the spawn distance is reached
[11620]82        if(this->tubes.size()==0||x-tubes.back()+tubeOffsetX>tubeDistance){
[11529]83            spawnTube();
84            this->tubes.push(x+tubeOffsetX);
85        }
[11595]86        //Delete Tubes when we pass through them
[11529]87        if(this->tubes.size()!=0&&x>this->tubes.front()){
88            this->tubes.pop();
89            levelUp();
90        }
[11595]91        //Delete Asteroids which are not visible anymore
92        while((this->asteroids.front())->getPosition().x<x-tubeOffsetX){
[11537]93            MovableEntity* deleteMe = asteroids.front();
94            asteroids.pop();
95            deleteMe->destroy();
96        }
[11529]97    }
98
[11595]99    //Gets called when we pass through a Tube
100    void FlappyOrx::levelUp(){
[11554]101        point++;
[11620]102        tubeDistance = tubeDistanceBase-tubeDistanceIncrease*point;            //smaller spawn Distance
103        getPlayer()->setSpeed(speedBase+speedIncrease*point);    //increase speed
[11503]104    }
105
[11595]106    //Returns our Ship
107    FlappyOrxShip* FlappyOrx::getPlayer(){
108        if (player == nullptr){
[11514]109            for (FlappyOrxShip* ship : ObjectList<FlappyOrxShip>()) {
[11503]110                player = ship;
[11514]111            }
[11503]112        }
113        return player;
114    }
115
[11595]116    //Spawn new Tube
117    void FlappyOrx::spawnTube(){
[11529]118        if (getPlayer() == nullptr)
119            return;
[11503]120
[11755]121        float space = 130.0f;    //vertical space between top and bottom tube
122        float height = (rnd()-0.5f)*(280.0f-space);  //Randomize height
[11595]123           
[11529]124        Vector3 pos = player->getPosition();
[11505]125
[11595]126        //create the two Asteroid fields (Tubes)
[11755]127        asteroidField(pos.x+tubeOffsetX,height-space/2,0.8f);  //bottom
128        asteroidField(pos.x+tubeOffsetX,height+space/2,-0.8f); //top
[11529]129    }
130
[11595]131    //Creates a new asteroid Field
[11755]132    void FlappyOrx::asteroidField(float x, float y, float slope){
133        float r = 20.0f;     //Radius of added Asteroids
[11595]134        int noadd = 0;  //how many times we failed to add a new asteroid
[11529]135
[11598]136        clearCircles();   //Delete Circles (we use circles to make sure we don't spawn two asteroids on top of eachother)
[11529]137        Circle newAsteroid = Circle();
[11554]138        newAsteroid.x=x;
139        newAsteroid.y=y;
140        newAsteroid.r=r;
[11595]141        addIfPossible(newAsteroid); //Add Asteroid at peak
142
143        //Fill up triangle with asteroids
[11598]144        while(noadd<5&&circlesUsed<nCircles){
[11529]145            if(slope>0)
[11755]146                newAsteroid.y=rnd(150+y)-150;   //create asteroid on bottom
[11529]147            else
[11755]148                newAsteroid.y=rnd(150-y)+y;     //create asteroid on top
[11529]149           
[11755]150            newAsteroid.x=x+(rnd()-0.5f)*(y-newAsteroid.y)/slope;
[11529]151            newAsteroid.r=r;
152           
[11595]153            int i = addIfPossible(newAsteroid); //Add Asteroid if it doesn't collide
[11529]154            if(i==0)
155                noadd++;
156            else if(i==2)
[11598]157                noadd=5;
[11529]158        }
159    }
160
[11595]161    //Create a new Asteroid
[11529]162    void FlappyOrx::createAsteroid(Circle &c){
[11757]163        MovableEntity* newAsteroid = new MovableEntity(player->getContext());
[11537]164
[11595]165        //Add Model fitting the Size of the Asteroid
[11529]166        if(c.r<=5)
[11580]167            newAsteroid->addTemplate(Asteroid5[rand()%NUM_ASTEROIDS]);
[11529]168        else if(c.r<=10)
[11580]169            newAsteroid->addTemplate(Asteroid10[rand()%NUM_ASTEROIDS]);
[11529]170        else if(c.r<=15)
[11580]171            newAsteroid->addTemplate(Asteroid15[rand()%NUM_ASTEROIDS]);
[11529]172        else
[11580]173            newAsteroid->addTemplate(Asteroid20[rand()%NUM_ASTEROIDS]);
[11529]174       
[11595]175        //Set position
[11529]176        newAsteroid->setPosition(Vector3(c.x, 0, c.y));
[11595]177
178        //Randomize orientation
[11755]179        newAsteroid->setOrientation(Vector3::UNIT_Z, Degree(rnd(360)));
180        newAsteroid->setOrientation(Vector3::UNIT_Y, Degree(rnd(360)));
[11529]181
[11595]182        //add to Queue (so that we can delete it again)
[11537]183        asteroids.push(newAsteroid);
[11529]184    }
185
[11598]186    //Deletes Asteroids array which stores all the circles used to make sure no asteroids collide when spawning
187    void FlappyOrx::clearCircles(){
188        circlesUsed=0;
189        for(int i = 0; i<this->nCircles; i++){
190            circles[i].r=0;
191        }
192    }
193
194    //checks if two circles collide
195    bool FlappyOrx::circleCollision(Circle &c1, Circle &c2){
196        if(c1.r<=0 || c2.r<=0)
197            return false;
[11755]198        float x = c1.x - c2.x;
199        float y = c1.y - c2.y;
200        float r = c1.r + c2.r;
[11598]201
202        return x*x+y*y<r*r*1.5;
203    }
204
205    //Adds a circle if its not colliding
206    int FlappyOrx::addIfPossible(Circle c){
207        int i;
208        for(i=0; i<this->nCircles && this->circles[i].r!=0 && c.r>0;i++){
209            while(circleCollision(c,this->circles[i])){
210                c.r-=5; //when it collides, try to make it smaller
211            }
212        }
213        if(c.r<=0){
214            return 0;
215        }
216        circlesUsed++;
217        this->circles[i].x=c.x;
218        this->circles[i].y=c.y;
219        this->circles[i].r=c.r;
220        createAsteroid(c);
221        return 1;
222    }
223
[11563]224    bool FlappyOrx::isDead(){
[11565]225        return bIsDead;
[11563]226    }
227
[11576]228    void FlappyOrx::setDead(bool value){
229        bIsDead = value;
230        if(not value){
231            point = -1;
232            levelUp();
233        }
[11563]234    }
235
[11481]236    void FlappyOrx::start()
237    {
[11503]238        // Set variable to temporarily force the player to spawn.
239        this->bForceSpawn_ = true;
240
241        // Call start for the parent class.
[11481]242        Deathmatch::start();
243    }
244
[11595]245    //RIP
[11537]246    void FlappyOrx::death(){
[11565]247        bIsDead = true;
[11589]248        firstGame = false;
[11576]249       
[11595]250        //Set randomized deathmessages
[11630]251        if(point<7)         sDeathMessage = DeathMessage7[rand()%(DeathMessage7.size())];
252        else if(point<20)   sDeathMessage = DeathMessage20[rand()%(DeathMessage20.size())];
[11580]253        else if(point<30)   sDeathMessage = DeathMessage30[rand()%(DeathMessage30.size())];
[11630]254        else                sDeathMessage = DeathMessageover30[rand()%(DeathMessageover30.size())];
[11576]255       
[11595]256        //Update Highscore
[11754]257        if (Highscore::exists())
258        {
259            int score = this->getPoints();
260            Highscore::getInstance().storeScore("Flappy Orx", score, this->getPlayer()->getPlayer());
[11537]261        }
[11595]262
263        //Delete all Tubes and asteroids
264        while (!tubes.empty()){
[11537]265            tubes.pop();
266        }
[11595]267        while (!asteroids.empty()){
[11537]268            MovableEntity* deleteMe = asteroids.front();
269            asteroids.pop();
270            deleteMe->destroy();
271        }
272    }
273
[11481]274    void FlappyOrx::end()
275    {
276        GSLevel::startMainMenu();
277    }
278}
Note: See TracBrowser for help on using the repository browser.