Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 27, 2017, 3:00:47 PM (8 years ago)
Author:
pascscha
Message:

faster asteroid spawning

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FlappyOrx_HS17/src/modules/flappyorx/FlappyOrx.cc

    r11595 r11598  
    6666        spawnDistance=200;                  //distance between tubes
    6767        tubeOffsetX=500;                    //tube offset (so that we can't see them spawn)
     68
     69        circlesUsed=0;
    6870        setHUDTemplate("FlappyOrxHUD");
    6971    }
     
    127129        int noadd = 0;  //how many times we failed to add a new asteroid
    128130
    129         ClearAsteroids();   //Delete Asteroids
     131        clearCircles();   //Delete Circles (we use circles to make sure we don't spawn two asteroids on top of eachother)
    130132        Circle newAsteroid = Circle();
    131133        newAsteroid.x=x;
     
    135137
    136138        //Fill up triangle with asteroids
    137         while(r>0){
     139        while(noadd<5&&circlesUsed<nCircles){
    138140            if(slope>0)
    139141                newAsteroid.y=float(rand())/RAND_MAX*(150+y)-150;   //create asteroid on bottom
     
    148150                noadd++;
    149151            else if(i==2)
    150                 r=0;
    151             if(noadd>=5){
    152                 noadd=0;
    153                 r-=5;
    154             }
     152                noadd=5;
    155153        }
    156154    }
     
    179177        //add to Queue (so that we can delete it again)
    180178        asteroids.push(newAsteroid);
     179    }
     180
     181    //Deletes Asteroids array which stores all the circles used to make sure no asteroids collide when spawning
     182    void FlappyOrx::clearCircles(){
     183        circlesUsed=0;
     184        for(int i = 0; i<this->nCircles; i++){
     185            circles[i].r=0;
     186        }
     187    }
     188
     189    //checks if two circles collide
     190    bool FlappyOrx::circleCollision(Circle &c1, Circle &c2){
     191        if(c1.r<=0 || c2.r<=0)
     192            return false;
     193        int x = c1.x - c2.x;
     194        int y = c1.y - c2.y;
     195        int r = c1.r + c2.r;
     196
     197        return x*x+y*y<r*r*1.5;
     198    }
     199
     200    //Adds a circle if its not colliding
     201    int FlappyOrx::addIfPossible(Circle c){
     202        int i;
     203        for(i=0; i<this->nCircles && this->circles[i].r!=0 && c.r>0;i++){
     204            while(circleCollision(c,this->circles[i])){
     205                c.r-=5; //when it collides, try to make it smaller
     206            }
     207        }
     208        if(c.r<=0){
     209            return 0;
     210        }
     211        circlesUsed++;
     212        this->circles[i].x=c.x;
     213        this->circles[i].y=c.y;
     214        this->circles[i].r=c.r;
     215        createAsteroid(c);
     216        return 1;
    181217    }
    182218
Note: See TracChangeset for help on using the changeset viewer.