Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Presentation_HS17/src/modules/flappyorx/FlappyOrx.cc @ 11682

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

level preview

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