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
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        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)
74
75        circlesUsed=0;
76        setHUDTemplate("FlappyOrxHUD");
77    }
78   
79    void FlappyOrx::updatePlayerPos(float x){
80
81        //Spawn a new Tube when the spawn distance is reached
82        if(this->tubes.size()==0||x-tubes.back()+tubeOffsetX>tubeDistance){
83            spawnTube();
84            this->tubes.push(x+tubeOffsetX);
85        }
86        //Delete Tubes when we pass through them
87        if(this->tubes.size()!=0&&x>this->tubes.front()){
88            this->tubes.pop();
89            levelUp();
90        }
91        //Delete Asteroids which are not visible anymore
92        while((this->asteroids.front())->getPosition().x<x-tubeOffsetX){
93            MovableEntity* deleteMe = asteroids.front();
94            asteroids.pop();
95            deleteMe->destroy();
96        }
97    }
98
99    //Gets called when we pass through a Tube
100    void FlappyOrx::levelUp(){
101        point++;
102        tubeDistance = tubeDistanceBase-tubeDistanceIncrease*point;            //smaller spawn Distance
103        getPlayer()->setSpeed(speedBase+speedIncrease*point);    //increase speed
104    }
105
106    //Returns our Ship
107    FlappyOrxShip* FlappyOrx::getPlayer(){
108        if (player == nullptr){
109            for (FlappyOrxShip* ship : ObjectList<FlappyOrxShip>()) {
110                player = ship;
111            }
112        }
113        return player;
114    }
115
116    //Spawn new Tube
117    void FlappyOrx::spawnTube(){
118        if (getPlayer() == nullptr)
119            return;
120
121        float space = 130.0f;    //vertical space between top and bottom tube
122        float height = (rnd()-0.5f)*(280.0f-space);  //Randomize height
123           
124        Vector3 pos = player->getPosition();
125
126        //create the two Asteroid fields (Tubes)
127        asteroidField(pos.x+tubeOffsetX,height-space/2,0.8f);  //bottom
128        asteroidField(pos.x+tubeOffsetX,height+space/2,-0.8f); //top
129    }
130
131    //Creates a new asteroid Field
132    void FlappyOrx::asteroidField(float x, float y, float slope){
133        float r = 20.0f;     //Radius of added Asteroids
134        int noadd = 0;  //how many times we failed to add a new asteroid
135
136        clearCircles();   //Delete Circles (we use circles to make sure we don't spawn two asteroids on top of eachother)
137        Circle newAsteroid = Circle();
138        newAsteroid.x=x;
139        newAsteroid.y=y;
140        newAsteroid.r=r;
141        addIfPossible(newAsteroid); //Add Asteroid at peak
142
143        //Fill up triangle with asteroids
144        while(noadd<5&&circlesUsed<nCircles){
145            if(slope>0)
146                newAsteroid.y=rnd(150+y)-150;   //create asteroid on bottom
147            else
148                newAsteroid.y=rnd(150-y)+y;     //create asteroid on top
149           
150            newAsteroid.x=x+(rnd()-0.5f)*(y-newAsteroid.y)/slope;
151            newAsteroid.r=r;
152           
153            int i = addIfPossible(newAsteroid); //Add Asteroid if it doesn't collide
154            if(i==0)
155                noadd++;
156            else if(i==2)
157                noadd=5;
158        }
159    }
160
161    //Create a new Asteroid
162    void FlappyOrx::createAsteroid(Circle &c){
163        MovableEntity* newAsteroid = new MovableEntity(player->getContext());
164
165        //Add Model fitting the Size of the Asteroid
166        if(c.r<=5)
167            newAsteroid->addTemplate(Asteroid5[rand()%NUM_ASTEROIDS]);
168        else if(c.r<=10)
169            newAsteroid->addTemplate(Asteroid10[rand()%NUM_ASTEROIDS]);
170        else if(c.r<=15)
171            newAsteroid->addTemplate(Asteroid15[rand()%NUM_ASTEROIDS]);
172        else
173            newAsteroid->addTemplate(Asteroid20[rand()%NUM_ASTEROIDS]);
174       
175        //Set position
176        newAsteroid->setPosition(Vector3(c.x, 0, c.y));
177
178        //Randomize orientation
179        newAsteroid->setOrientation(Vector3::UNIT_Z, Degree(rnd(360)));
180        newAsteroid->setOrientation(Vector3::UNIT_Y, Degree(rnd(360)));
181
182        //add to Queue (so that we can delete it again)
183        asteroids.push(newAsteroid);
184    }
185
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;
198        float x = c1.x - c2.x;
199        float y = c1.y - c2.y;
200        float r = c1.r + c2.r;
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
224    bool FlappyOrx::isDead(){
225        return bIsDead;
226    }
227
228    void FlappyOrx::setDead(bool value){
229        bIsDead = value;
230        if(not value){
231            point = -1;
232            levelUp();
233        }
234    }
235
236    void FlappyOrx::start()
237    {
238        // Set variable to temporarily force the player to spawn.
239        this->bForceSpawn_ = true;
240
241        // Call start for the parent class.
242        Deathmatch::start();
243    }
244
245    //RIP
246    void FlappyOrx::death(){
247        bIsDead = true;
248        firstGame = false;
249       
250        //Set randomized deathmessages
251        if(point<7)         sDeathMessage = DeathMessage7[rand()%(DeathMessage7.size())];
252        else if(point<20)   sDeathMessage = DeathMessage20[rand()%(DeathMessage20.size())];
253        else if(point<30)   sDeathMessage = DeathMessage30[rand()%(DeathMessage30.size())];
254        else                sDeathMessage = DeathMessageover30[rand()%(DeathMessageover30.size())];
255       
256        //Update Highscore
257        if (Highscore::exists())
258        {
259            int score = this->getPoints();
260            Highscore::getInstance().storeScore("Flappy Orx", score, this->getPlayer()->getPlayer());
261        }
262
263        //Delete all Tubes and asteroids
264        while (!tubes.empty()){
265            tubes.pop();
266        }
267        while (!asteroids.empty()){
268            MovableEntity* deleteMe = asteroids.front();
269            asteroids.pop();
270            deleteMe->destroy();
271        }
272    }
273
274    void FlappyOrx::end()
275    {
276        GSLevel::startMainMenu();
277    }
278}
Note: See TracBrowser for help on using the repository browser.