Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

[FlappyOrx_HS17] removed FlappyOrxCenterPoint, it doesn't seem to be neede

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