Fixed status bar counts for asteroids and shots

I implmented counters, and simply count the number of objects every
frame.

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-05 11:10:46 -04:00
parent 77561aa328
commit 7310444065
2 changed files with 20 additions and 14 deletions

View File

@@ -154,7 +154,7 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable {
*/ */
public boolean shotCollision(Shot shot) { public boolean shotCollision(Shot shot) {
if( Math.pow(mRadius, 2) > Math.pow(shot.getX() - mX, 2) + Math.pow(shot.getY() - mY, 2)){ if( Math.pow(mRadius, 2) > Math.pow(shot.getX() - mX, 2) + Math.pow(shot.getY() - mY, 2)){
/** /*
* remove the asteroid and the shot * remove the asteroid and the shot
*/ */
mGame.removeElement(this); mGame.removeElement(this);

View File

@@ -51,12 +51,6 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
public synchronized void addElement(Object o) { public synchronized void addElement(Object o) {
if(o instanceof Shot)
mGameFrame.getStatusBar().incrementShotCount();
if(o instanceof Asteroid)
mGameFrame.getStatusBar().incrementAsteroidCount();
mWorld.add(o); mWorld.add(o);
} }
@@ -164,11 +158,7 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
public synchronized void removeElement(Object o) { public synchronized void removeElement(Object o) {
if(o instanceof Shot)
mGameFrame.getStatusBar().decrementShotCount();
if(o instanceof Asteroid) { if(o instanceof Asteroid) {
mGameFrame.getStatusBar().decrementAsteroidCount();
mGameFrame.getStatusBar().incrementScore(2); mGameFrame.getStatusBar().incrementScore(2);
} }
mWorld.remove(o); mWorld.remove(o);
@@ -182,9 +172,9 @@ public class AsteroidGame extends Thread {
@Override @Override
public void run() { public void run() {
boolean hasOneUped = false; boolean hasOneUped = false;
int asteroidCount = 0, shotCount = 0;
while (true){ while (true){
if(isStarted) { if(isStarted) {
boolean isThereAsteroids = false;
/* /*
* update the display and stats * update the display and stats
@@ -201,7 +191,7 @@ public class AsteroidGame extends Thread {
for (int i = 0; i < wolrd.size(); i++){ for (int i = 0; i < wolrd.size(); i++){
o = wolrd.get(i); o = wolrd.get(i);
if(o instanceof Collider){ if(o instanceof Collider){
isThereAsteroids = true; asteroidCount++;
c = (Collider) o; c = (Collider) o;
for(int index = 0; index < wolrd.size(); index++) for(int index = 0; index < wolrd.size(); index++)
if(c.checkForCollision(wolrd.get(index))) if(c.checkForCollision(wolrd.get(index)))
@@ -209,12 +199,16 @@ public class AsteroidGame extends Thread {
if(wolrd.get(index) instanceof Ship) if(wolrd.get(index) instanceof Ship)
downShip(); downShip();
} }
//coutn the shots
if(o instanceof Shot)
shotCount++;
} }
/* /*
* if there are no more asteroids, then increment the level * if there are no more asteroids, then increment the level
*/ */
if(!isThereAsteroids){ if(asteroidCount == 0){
mGameFrame.getStatusBar().incrementLevel(); mGameFrame.getStatusBar().incrementLevel();
initLevel(); initLevel();
} }
@@ -229,6 +223,18 @@ public class AsteroidGame extends Thread {
} }
} else } else
hasOneUped = false; hasOneUped = false;
/*
* update the status bar with the new counts
*/
mGameFrame.getStatusBar().setShotCount(shotCount);
mGameFrame.getStatusBar().setAsteroidCount(asteroidCount);
/*
* reset counters
*/
asteroidCount = 0;
shotCount = 0;
} }
/* /*
* sleep till next time * sleep till next time