Addded in / de crement methods to the Status

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-03 13:24:45 -04:00
parent 738ff31d1e
commit 2152cc8213
3 changed files with 141 additions and 45 deletions

View File

@@ -20,6 +20,7 @@
*/ */
package com.RickBarrette.asteroids; package com.RickBarrette.asteroids;
import java.util.ArrayList;
import java.util.Vector; import java.util.Vector;
/** /**
@@ -29,7 +30,7 @@ import java.util.Vector;
public class AsteroidGame extends Thread { public class AsteroidGame extends Thread {
private static final int DELAY_IN_MSEC = 50; private static final int DELAY_IN_MSEC = 50;
private Vector<Object> mWorld; private ArrayList<Object> mWorld;
private GameFrame mGameFrame; private GameFrame mGameFrame;
public boolean isStarted = false; public boolean isStarted = false;
@@ -50,12 +51,12 @@ public class AsteroidGame extends Thread {
*/ */
public void addElement(Object o) { public void addElement(Object o) {
if(o instanceof Shot) if(o instanceof Shot)
mGameFrame.getStatusBar().setShotCount(mGameFrame.getStatusBar().getShotCount()+1); mGameFrame.getStatusBar().incrementShotCount();
if(o instanceof Asteroid) if(o instanceof Asteroid)
mGameFrame.getStatusBar().setAsteroidCount(mGameFrame.getStatusBar().getAsteroidCount()+1); mGameFrame.getStatusBar().incrementAsteroidCount();
mWorld.addElement(o); mWorld.add(o);
} }
/** /**
@@ -63,13 +64,21 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
public void createGame() { public void createGame() {
mWorld = new Vector<Object>(); mWorld = new ArrayList<Object>();
mWorld.add(new Ship(100,100,0,.35,.98,.4,1)); mWorld.add(new Ship(100,100,0,.35,.98,.4,1));
addElement(new Asteroid(500, 500, 1, 10, 50, 3, 3, this)); addElement(new Asteroid(500, 500, 1, 10, 50, 3, 3, this));
mGameFrame.getStatusBar().setShipCount(3); mGameFrame.getStatusBar().setShipCount(3);
mGameFrame.getStatusBar().setScore(0);
mGameFrame.getStatusBar().setAsteroidCount(0);
mGameFrame.getStatusBar().setTime(0);
mGameFrame.getStatusBar().setShotCount(0);
} }
public Vector<Object> getWorld() { /**
* @return the world
* @author ricky barrette
*/
public ArrayList<Object> getWorld() {
return mWorld; return mWorld;
} }
@@ -89,6 +98,7 @@ public class AsteroidGame extends Thread {
mWorld.clear(); mWorld.clear();
mGameFrame.setDisplayText(null); mGameFrame.setDisplayText(null);
createGame(); createGame();
startGame();
} }
/** /**
@@ -108,11 +118,13 @@ public class AsteroidGame extends Thread {
*/ */
public void removeElement(Object o) { public void removeElement(Object o) {
if(o instanceof Shot) if(o instanceof Shot)
mGameFrame.getStatusBar().setShotCount(mGameFrame.getStatusBar().getShotCount()-1); mGameFrame.getStatusBar().decrementShotCount();
if(o instanceof Asteroid) if(o instanceof Asteroid) {
mGameFrame.getStatusBar().setAsteroidCount(mGameFrame.getStatusBar().getAsteroidCount()-1); mGameFrame.getStatusBar().decrementAsteroidCount();
mWorld.removeElement(o); mGameFrame.getStatusBar().incrementScore(2);
}
mWorld.remove(o);
} }
/** /**
@@ -177,7 +189,7 @@ public class AsteroidGame extends Thread {
mWorld.remove(i); mWorld.remove(i);
} }
mGameFrame.getStatusBar().setShipCount(mGameFrame.getStatusBar().getShipCount() -1); mGameFrame.getStatusBar().decrementShipCount();
if(mGameFrame.getStatusBar().getShipCount() > 0){ if(mGameFrame.getStatusBar().getShipCount() > 0){
pauseGame(); pauseGame();

View File

@@ -233,24 +233,24 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
break; break;
/* /*
* [H] Hyoer jump * [H] Hyoer jump
*/ */
case KeyEvent.VK_H: case KeyEvent.VK_H:
if(isKeyPressed){ if(isKeyPressed){
Random myRNG = new Random(); Random myRNG = new Random();
ship.setLocation(myRNG.nextInt(mDisplay.getHeight()), myRNG.nextInt(mDisplay.getWidth())); ship.setLocation(myRNG.nextInt(mDisplay.getHeight()), myRNG.nextInt(mDisplay.getWidth()));
} }
break; break;
/* /*
* [Space] Pew Pew Pew!!! * [Space] Pew Pew Pew!!!
*/ */
case KeyEvent.VK_SPACE: case KeyEvent.VK_SPACE:
if(isKeyPressed) if(isKeyPressed)
mGame.addElement(new Shot(ship.getX(), ship.getY(), ship.getAngle(), ship.getXVelocity(), ship.getYVelocity(), mGame)); mGame.addElement(new Shot(ship.getX(), ship.getY(), ship.getAngle(), ship.getXVelocity(), ship.getYVelocity(), mGame));
break; break;
} }
} }
@Override @Override

View File

@@ -57,72 +57,156 @@ public class Status extends JPanel {
} }
/** /**
* @return the mAsteroidCount * decreases total asteroid count by 1
* @author ricky barrette
*/ */
public int getAsteroidCount() { public synchronized void decrementAsteroidCount() {
return mAsteroidCount; this.mAsteroidCount -= 1;
} }
/**
* reduces total score by x amount
* @param score
* @author ricky barrette
*/
public synchronized void decrementScore(int score) {
this.mScore -= mScore;
}
/**
* reduces total ships by 1
* @author ricky barrette
*/
public synchronized void decrementShipCount() {
this.mShipCount -= 1;
}
/**
* reduces total shots by 1
* @author ricky barrette
*/
public synchronized void decrementShotCount() {
this.mShotCount -= 1;
}
/**
* Reduces time be x amount
* @param mTime
* @author ricky barrette
*/
public synchronized void decrementTime(long mTime) {
this.mTime -= mTime;
}
/**
* @return the mAsteroidCount
*/
public synchronized int getAsteroidCount() {
return mAsteroidCount;
}
/** /**
* @return the mScore * @return the mScore
*/ */
public long getScore() { public synchronized long getScore() {
return mScore; return mScore;
} }
/** /**
* @return the mShipCount * @return the mShipCount
*/ */
public int getShipCount() { public synchronized int getShipCount() {
return mShipCount; return mShipCount;
} }
/** /**
* @return the mShotCount * @return the mShotCount
*/ */
public int getShotCount() { public synchronized int getShotCount() {
return mShotCount; return mShotCount;
} }
/** /**
* @return the mTime * @return the mTime
*/ */
public long getTime() { public synchronized long getTime() {
return mTime; return mTime;
} }
/**
* increases total asteroid count by 1
* @author ricky barrette
*/
public synchronized void incrementAsteroidCount() {
this.mAsteroidCount += 1;
}
/**
* increases total score by x amount
* @param score
* @author ricky barrette
*/
public synchronized void incrementScore(int score) {
this.mScore += score;
}
/**
* increases total ships by one
* @author ricky barrette
*/
public synchronized void incrementShipCount() {
this.mShipCount += 1;
}
/**
* increases total shots by one
* @author ricky barrette
*/
public synchronized void incrementShotCount() {
this.mShotCount += 1;
}
/**
* increates time by x amount
* @param mTime
* @author ricky barrette
*/
public synchronized void incrementTime(long mTime) {
this.mTime += mTime;
}
/** /**
* @param mAsteroidCount the mAsteroidCount to set * @param mAsteroidCount the mAsteroidCount to set
*/ */
public void setAsteroidCount(int mAsteroidCount) { public synchronized void setAsteroidCount(int mAsteroidCount) {
this.mAsteroidCount = mAsteroidCount; this.mAsteroidCount = mAsteroidCount;
} }
/** /**
* @param mScore the mScore to set * @param mScore the mScore to set
*/ */
public void setScore(long mScore) { public synchronized void setScore(long mScore) {
this.mScore = mScore; this.mScore = mScore;
} }
/** /**
* @param mShipCount the mShipCount to set * @param mShipCount the mShipCount to set
*/ */
public void setShipCount(int mShipCount) { public synchronized void setShipCount(int mShipCount) {
this.mShipCount = mShipCount; this.mShipCount = mShipCount;
} }
/** /**
* @param mShotCount the mShotCount to set * @param mShotCount the mShotCount to set
*/ */
public void setShotCount(int mShotCount) { public synchronized void setShotCount(int mShotCount) {
this.mShotCount = mShotCount; this.mShotCount = mShotCount;
} }
/** /**
* @param mTime the mTime to set * @param mTime the mTime to set
*/ */
public void setTime(long mTime) { public synchronized void setTime(long mTime) {
this.mTime = mTime; this.mTime = mTime;
} }