Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.cc @ 11799

Last change on this file since 11799 was 11631, checked in by merholzl, 7 years ago

level preview

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