Implmented the shot delay
Also moved some methods around to clean up the classes Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -73,7 +73,7 @@ public class AsteroidGame extends Thread {
|
|||||||
|
|
||||||
if(s != null){
|
if(s != null){
|
||||||
s.allStop();
|
s.allStop();
|
||||||
hyperJump(s);
|
s.hyperJump();
|
||||||
}
|
}
|
||||||
|
|
||||||
mGameFrame.getStatusBar().decrementShipCount();
|
mGameFrame.getStatusBar().decrementShipCount();
|
||||||
@@ -89,6 +89,14 @@ public class AsteroidGame extends Thread {
|
|||||||
mGameFrame.repaint();
|
mGameFrame.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the game's frame
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public GameFrame getGameFrame() {
|
||||||
|
return this.mGameFrame;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the world
|
* @return the world
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
@@ -96,26 +104,7 @@ public class AsteroidGame extends Thread {
|
|||||||
public ArrayList<Object> getWorld() {
|
public ArrayList<Object> getWorld() {
|
||||||
return mWorld;
|
return mWorld;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Hyperjumps the sip
|
|
||||||
* @param ship
|
|
||||||
* @author ricky barrette
|
|
||||||
*/
|
|
||||||
public void hyperJump(final Ship ship) {
|
|
||||||
// boolean isSafe = true;
|
|
||||||
Random gen = new Random();
|
|
||||||
// do{
|
|
||||||
System.out.println("hyper jumping");
|
|
||||||
ship.setLocation(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()));
|
|
||||||
// for(int i = 0; i < mWorld.size(); i++)
|
|
||||||
// if(mWorld.get(i) instanceof Collider)
|
|
||||||
// if(((Collider) mWorld.get(i)).checkForCollision(ship))
|
|
||||||
// isSafe = false;
|
|
||||||
// } while (!isSafe);
|
|
||||||
mGameFrame.repaintDispaly();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* populates the world for a new game
|
* populates the world for a new game
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
@@ -155,7 +144,7 @@ public class AsteroidGame extends Thread {
|
|||||||
mGameFrame.getStatusBar().setShotCount(0);
|
mGameFrame.getStatusBar().setShotCount(0);
|
||||||
mGameFrame.getStatusBar().setLevel(1);
|
mGameFrame.getStatusBar().setLevel(1);
|
||||||
|
|
||||||
mWorld.add(new Ship(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()),0,.35,.98,.2,1));
|
mWorld.add(new Ship(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()), 0, .35, .98, .2, 1, this));
|
||||||
|
|
||||||
initLevel();
|
initLevel();
|
||||||
|
|
||||||
@@ -300,7 +289,7 @@ public class AsteroidGame extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the enabled state of Moving Space Objects
|
* Sets the enabled state of Moving Space Objects
|
||||||
* @param b
|
* @param b
|
||||||
@@ -311,7 +300,7 @@ public class AsteroidGame extends Thread {
|
|||||||
if(item instanceof MovingSpaceObject)
|
if(item instanceof MovingSpaceObject)
|
||||||
((MovingSpaceObject) item).setActive(b);
|
((MovingSpaceObject) item).setActive(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the number of objects in the world
|
* @return the number of objects in the world
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
|
|||||||
*/
|
*/
|
||||||
case KeyEvent.VK_H:
|
case KeyEvent.VK_H:
|
||||||
if(isKeyPressed){
|
if(isKeyPressed){
|
||||||
mGame.hyperJump(ship);
|
ship.hyperJump();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -188,7 +188,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
|
|||||||
*/
|
*/
|
||||||
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));
|
ship.shoot();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,30 @@ public abstract class MovingSpaceObject extends SpaceObject implements Moveable{
|
|||||||
isAccelerating = false;
|
isAccelerating = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the moving space object's angle
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public double getAngle() {
|
||||||
|
return mAngle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the moving space object's x volocity
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public double getXVelocity() {
|
||||||
|
return this.mXVelocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the moving space object's y velocity
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public double getYVelocity() {
|
||||||
|
return this.mYVelocity;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if the space object is accelerating
|
* @return true if the space object is accelerating
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
@@ -68,7 +92,7 @@ public abstract class MovingSpaceObject extends SpaceObject implements Moveable{
|
|||||||
public boolean isTurningLeft(){
|
public boolean isTurningLeft(){
|
||||||
return isTurningLeft;
|
return isTurningLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if the space object is turning right
|
* @return true if the space object is turning right
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
@@ -138,7 +162,7 @@ public abstract class MovingSpaceObject extends SpaceObject implements Moveable{
|
|||||||
public void setAccelerating(final boolean accelerating) {
|
public void setAccelerating(final boolean accelerating) {
|
||||||
this.isAccelerating = accelerating; // start or stop accelerating the ship
|
this.isAccelerating = accelerating; // start or stop accelerating the ship
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets wether or not this space object is active
|
* Sets wether or not this space object is active
|
||||||
* @param active
|
* @param active
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ package com.RickBarrette.asteroids;
|
|||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class will be the user's ship. I will be used to destroy the asteroids, with it's laser! Pew Pew Pew!!!
|
* This class will be the user's ship. I will be used to destroy the asteroids, with it's laser! Pew Pew Pew!!!
|
||||||
@@ -32,9 +33,10 @@ public class Ship extends MovingSpaceObject {
|
|||||||
private final int[] mOrigYpoints = { 0, -8, 0, 8 };
|
private final int[] mOrigYpoints = { 0, -8, 0, 8 };
|
||||||
private final int[] mOrigFlameXpoints = { -6, -23, -6 };
|
private final int[] mOrigFlameXpoints = { -6, -23, -6 };
|
||||||
private final int[] mOrigFlameYpoints = { -3, 0, 3 };
|
private final int[] mOrigFlameYpoints = { -3, 0, 3 };
|
||||||
private final int shotDelay;
|
private final int mShotDelay;
|
||||||
private int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3];
|
private int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3];
|
||||||
private int shotDelayLeft;
|
private int mShotDelayLeft;
|
||||||
|
private final AsteroidGame mGame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* radius of circle used to approximate the ship
|
* radius of circle used to approximate the ship
|
||||||
@@ -51,8 +53,9 @@ public class Ship extends MovingSpaceObject {
|
|||||||
* @param rotationalSpeed of the ship
|
* @param rotationalSpeed of the ship
|
||||||
* @param shotDelay of the ship
|
* @param shotDelay of the ship
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
|
* @param game
|
||||||
*/
|
*/
|
||||||
public Ship(final double x, final double y, final double angle, final double acceleration, final double velocityDecay, final double rotationalSpeed, final int shotDelay) {
|
public Ship(final double x, final double y, final double angle, final double acceleration, final double velocityDecay, final double rotationalSpeed, final int shotDelay, final AsteroidGame game) {
|
||||||
this.mX = x;
|
this.mX = x;
|
||||||
this.mY = y;
|
this.mY = y;
|
||||||
this.mAngle = angle;
|
this.mAngle = angle;
|
||||||
@@ -60,15 +63,16 @@ public class Ship extends MovingSpaceObject {
|
|||||||
this.mVelocityDecay = velocityDecay;
|
this.mVelocityDecay = velocityDecay;
|
||||||
this.mRotationalSpeed = rotationalSpeed;
|
this.mRotationalSpeed = rotationalSpeed;
|
||||||
this.mColor = Color.CYAN;
|
this.mColor = Color.CYAN;
|
||||||
|
mGame = game;
|
||||||
|
|
||||||
// start off paused
|
// start off paused
|
||||||
this.isActive = false;
|
this.isActive = false;
|
||||||
|
|
||||||
// # of frames between shots
|
// # of frames between shots
|
||||||
this.shotDelay = shotDelay;
|
this.mShotDelay = shotDelay;
|
||||||
|
|
||||||
// ready to shoot
|
// ready to shoot
|
||||||
this.shotDelayLeft = 0;
|
this.mShotDelayLeft = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,7 +83,7 @@ public class Ship extends MovingSpaceObject {
|
|||||||
/*
|
/*
|
||||||
* check if the shot delay has been satifiyed
|
* check if the shot delay has been satifiyed
|
||||||
*/
|
*/
|
||||||
if (shotDelayLeft > 0)
|
if (mShotDelayLeft > 0)
|
||||||
return false;
|
return false;
|
||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
@@ -137,14 +141,6 @@ public class Ship extends MovingSpaceObject {
|
|||||||
g.fillPolygon(mXpoints, mYpoints, 4);
|
g.fillPolygon(mXpoints, mYpoints, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the ship's current angle
|
|
||||||
* @author ricky barrette
|
|
||||||
*/
|
|
||||||
public double getAngle() {
|
|
||||||
return mAngle;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return radius of circle that approximates the ship
|
* @return radius of circle that approximates the ship
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
@@ -153,12 +149,23 @@ public class Ship extends MovingSpaceObject {
|
|||||||
return mRadius;
|
return mRadius;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getXVelocity() {
|
/**
|
||||||
return this.mXVelocity;
|
* Hyperjumps the sip
|
||||||
}
|
* @param ship
|
||||||
|
* @author ricky barrette
|
||||||
public double getYVelocity() {
|
*/
|
||||||
return this.mYVelocity;
|
public void hyperJump() {
|
||||||
|
// boolean isSafe = true;
|
||||||
|
Random gen = new Random();
|
||||||
|
// do{
|
||||||
|
System.out.println("hyper jumping");
|
||||||
|
setLocation(gen.nextInt(mGame.getGameFrame().getDisplayWidth()), gen.nextInt(mGame.getGameFrame().getDispalyHeight()));
|
||||||
|
// for(int i = 0; i < mWorld.size(); i++)
|
||||||
|
// if(mWorld.get(i) instanceof Collider)
|
||||||
|
// if(((Collider) mWorld.get(i)).checkForCollision(ship))
|
||||||
|
// isSafe = false;
|
||||||
|
// } while (!isSafe);
|
||||||
|
mGame.getGameFrame().repaintDispaly();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -172,9 +179,20 @@ public class Ship extends MovingSpaceObject {
|
|||||||
* move() is called every frame that the game
|
* move() is called every frame that the game
|
||||||
* is run, so this ticks down the shot delay
|
* is run, so this ticks down the shot delay
|
||||||
*/
|
*/
|
||||||
if (shotDelayLeft > 0)
|
if (mShotDelayLeft > 0)
|
||||||
shotDelayLeft--;
|
mShotDelayLeft--;
|
||||||
|
|
||||||
super.move(scrnWidth, scrnHeight);
|
super.move(scrnWidth, scrnHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fires a shot from the ship
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public void shoot() {
|
||||||
|
if(canShoot()) {
|
||||||
|
mGame.addElement(new Shot(mX, mY, mAngle, mXVelocity, mYVelocity, mGame));
|
||||||
|
mShotDelayLeft += mShotDelay;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user