[Orxonox-commit 6977] r11598 - code/branches/FlappyOrx_HS17/src/modules/flappyorx

pascscha at orxonox.net pascscha at orxonox.net
Mon Nov 27 15:00:48 CET 2017


Author: pascscha
Date: 2017-11-27 15:00:47 +0100 (Mon, 27 Nov 2017)
New Revision: 11598

Modified:
   code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.cc
   code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.h
Log:
faster asteroid spawning

Modified: code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.cc
===================================================================
--- code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.cc	2017-11-27 13:21:11 UTC (rev 11597)
+++ code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.cc	2017-11-27 14:00:47 UTC (rev 11598)
@@ -65,6 +65,8 @@
 
         spawnDistance=200;                  //distance between tubes
         tubeOffsetX=500;                    //tube offset (so that we can't see them spawn)
+
+        circlesUsed=0;
         setHUDTemplate("FlappyOrxHUD");
     }
 
@@ -126,7 +128,7 @@
         int r = 20;     //Radius of added Asteroids
         int noadd = 0;  //how many times we failed to add a new asteroid
 
-        ClearAsteroids();   //Delete Asteroids
+        clearCircles();   //Delete Circles (we use circles to make sure we don't spawn two asteroids on top of eachother)
         Circle newAsteroid = Circle();
         newAsteroid.x=x;
         newAsteroid.y=y;
@@ -134,7 +136,7 @@
         addIfPossible(newAsteroid); //Add Asteroid at peak
 
         //Fill up triangle with asteroids
-        while(r>0){
+        while(noadd<5&&circlesUsed<nCircles){
             if(slope>0)
                 newAsteroid.y=float(rand())/RAND_MAX*(150+y)-150;   //create asteroid on bottom
             else
@@ -147,11 +149,7 @@
             if(i==0)
                 noadd++;
             else if(i==2)
-                r=0;
-            if(noadd>=5){
-                noadd=0;
-                r-=5;
-            }
+                noadd=5;
         }
     }
 
@@ -180,6 +178,44 @@
         asteroids.push(newAsteroid);
     }
 
+    //Deletes Asteroids array which stores all the circles used to make sure no asteroids collide when spawning
+    void FlappyOrx::clearCircles(){
+        circlesUsed=0;
+        for(int i = 0; i<this->nCircles; i++){
+            circles[i].r=0;
+        }
+    }
+
+    //checks if two circles collide
+    bool FlappyOrx::circleCollision(Circle &c1, Circle &c2){
+        if(c1.r<=0 || c2.r<=0)
+            return false;
+        int x = c1.x - c2.x;
+        int y = c1.y - c2.y;
+        int r = c1.r + c2.r;
+
+        return x*x+y*y<r*r*1.5;
+    }
+
+    //Adds a circle if its not colliding
+    int FlappyOrx::addIfPossible(Circle c){
+        int i;
+        for(i=0; i<this->nCircles && this->circles[i].r!=0 && c.r>0;i++){
+            while(circleCollision(c,this->circles[i])){
+                c.r-=5; //when it collides, try to make it smaller
+            }
+        }
+        if(c.r<=0){
+            return 0;
+        }
+        circlesUsed++;
+        this->circles[i].x=c.x;
+        this->circles[i].y=c.y;
+        this->circles[i].r=c.r;
+        createAsteroid(c);
+        return 1;
+    }
+
     void FlappyOrx::setCenterpoint(FlappyOrxCenterPoint* center){
         this->center_ = center;
     }

Modified: code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.h
===================================================================
--- code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.h	2017-11-27 13:21:11 UTC (rev 11597)
+++ code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.h	2017-11-27 14:00:47 UTC (rev 11598)
@@ -20,9 +20,8 @@
  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *
  *   Author:
- *      Florian Zinggeler
- *   Co-authors:
- *      ...
+ *      Leo Mehr Holz
+ *      Pascal Schärli
  *
  */
 
@@ -38,7 +37,6 @@
 #include "flappyorx/FlappyOrxPrereqs.h"
 
 #include "gametypes/Deathmatch.h"
-#include "tools/Timer.h"
 #include <vector>
 
 namespace orxonox
@@ -76,26 +74,16 @@
             
             void setCenterpoint(FlappyOrxCenterPoint* center);
 
-            int getLives(){return this->lives;}
-            int getLevel(){return this->level;}
             int getPoints(){return this->point;}
-            int getMultiplier(){return this->multiplier;}
-
-            void costLife();
+            
             void levelUp();
-            void addPoints(int numPoints);
-            // checks if multiplier should be reset.
-            void comboControll();
-
+            
             bool isDead();
             void setDead(bool value);
 
             FlappyOrxShip* getPlayer();
             
-            int lives;
-            int multiplier;
             bool bEndGame;
-            bool bShowLevel;
             bool bIsDead;
             bool firstGame;
             std::string sDeathMessage;
@@ -105,53 +93,18 @@
             int tubeOffsetX;
             
         private:
-            void toggleShowLevel(){bShowLevel = !bShowLevel;}
-            
-            const static int nAst = 7;
-            Circle Asteroids[nAst];
+             
+            const static int nCircles = 6;
+            int circlesUsed;
+            Circle circles[nCircles];
 
-            void ClearAsteroids(){
-                for(int i = 0; i<this->nAst; i++){
-                    Asteroids[i].r=0;
-                }
-            }
+            void clearCircles();
+            bool circleCollision(Circle &c1, Circle &c2);
+            int addIfPossible(Circle c);
 
-            
-            bool Collision(Circle &c1, Circle &c2){
-                if(c1.r==0 || c2.r==0)
-                    return false;
-                int x = c1.x - c2.x;
-                int y = c1.y - c2.y;
-                int r = c1.r + c2.r;
-
-                return x*x+y*y<r*r*1.5;
-            }
-
-            int addIfPossible(Circle c){
-                int i;
-                for(i=0; i<this->nAst && this->Asteroids[i].r!=0;i++){
-                    if(Collision(c,this->Asteroids[i])){
-                        return 0;
-                    }
-                }
-                if(i==nAst)
-                    return 2;
-                this->Asteroids[i].x=c.x;
-                this->Asteroids[i].y=c.y;
-                this->Asteroids[i].r=c.r;
-                createAsteroid(c);
-                return 1;
-            }
-            
-            
-
             WeakPtr<FlappyOrxCenterPoint> center_;
             WeakPtr<FlappyOrxShip> player;
 
-            Timer enemySpawnTimer;
-            Timer comboTimer;
-            Timer showLevelTimer;
-            //Context* context;
             int level;
             int point;
             bool b_combo;



More information about the Orxonox-commit mailing list