Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

XML Ports and cleanUp

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