Added a workign asteroid, next stp is collision detection

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-02 11:39:21 -04:00
parent 6117a5c369
commit 0556735e64
4 changed files with 49 additions and 15 deletions

View File

@@ -20,19 +20,52 @@
*/
package com.RickBarrette.asteroids;
import java.awt.Color;
import java.awt.Graphics;
/**
* This class will be used to make astroids, to destroy the ship!
* @author ricky barrette
*/
public class Asteroid extends MovingSpaceObject {
public class Asteroid extends MovingSpaceObject implements Drawable {
private final int mNumberSplit;
private final int mHitsLeft;
private final int mRadius;
private AsteroidGame mGame;
/**
* Creates a new Asteroid
* @param x
* @param y
* @param xVelocity
* @param yVelocity
* @param numberSplit number of smaller asteroids to create after being blown up
* @param hitsLeft number of hits left
* @author ricky barrette
*/
public Asteroid() {
// TODO Asteroid stuff
public Asteroid(double x, double y, double minVelocity, double maxVelocity, int radius, int numberSplit, int hitsLeft, AsteroidGame game) {
mGame = game;
mColor = Color.GRAY;
mX = x;
mY = y;
double vel=minVelocity + Math.random()*(maxVelocity-minVelocity);
mAngle = 2*Math.PI*Math.random(); // random direction
mXVelocity=vel*Math.cos(mAngle);
mYVelocity=vel*Math.sin(mAngle);
mNumberSplit = numberSplit;
mHitsLeft = hitsLeft;
mRadius = radius;
mVelocityDecay = 1;
isActive = true;
}
@Override
public void draw(Graphics g) {
g.setColor(mColor);
g.fillOval((int)(mX-mRadius+.5),(int)(mY-mRadius+.5), (int)(2*mRadius),(int)(2*mRadius));
}
}

View File

@@ -60,6 +60,7 @@ public class AsteroidGame extends Thread {
public void createGame() {
mWorld = new Vector<Object>();
mWorld.add(new Ship(100,100,0,.35,.98,.4,1));
mWorld.add(new Asteroid(500, 500, 1, 10, 50, 3, 3, this));
}
public Vector<Object> getWorld() {

View File

@@ -29,8 +29,8 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
protected double mAngle;
protected double mVelocityDecay;
protected double mRotationalSpeed;
protected double xVelocity = 0;
protected double yVelocity = 0;
protected double mXVelocity = 0;
protected double mYVelocity = 0;
protected double mAcceleration;
protected boolean isTurningLeft = false, isTurningRight = false, isAccelerating = false, isActive;
@@ -77,8 +77,8 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
if(Main.DEBUG){
System.out.println("Move "+ scrnWidth +" x "+ scrnHeight);
System.out.println("angle "+mAngle);
System.out.println("xVelocity = "+xVelocity);
System.out.println("yVelocity = "+yVelocity);
System.out.println("xVelocity = "+mXVelocity);
System.out.println("yVelocity = "+mYVelocity);
System.out.println("yX = "+mX);
System.out.println("yY = "+mY);
}
@@ -108,20 +108,20 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
System.out.println("accelerating by "+mAcceleration);
// calculates components of accel and adds them to velocity
xVelocity += mAcceleration * Math.cos(mAngle);
yVelocity += mAcceleration * Math.sin(mAngle);
mXVelocity += mAcceleration * Math.cos(mAngle);
mYVelocity += mAcceleration * Math.sin(mAngle);
}
// move the space object by adding velocity to position
mX += xVelocity;
mY += yVelocity;
mX += mXVelocity;
mY += mYVelocity;
/*
* slows ship down by percentages (velDecay
* should be a decimal between 0 and 1
*/
xVelocity *= mVelocityDecay;
yVelocity *= mVelocityDecay;
mXVelocity *= mVelocityDecay;
mYVelocity *= mVelocityDecay;
/*
* wrap the ship around to the opposite side of the screen

View File

@@ -161,10 +161,10 @@ public class Ship extends MovingSpaceObject implements Drawable {
}
public double getXVelocity() {
return this.xVelocity;
return this.mXVelocity;
}
public double getYVelocity() {
return this.yVelocity;
return this.mYVelocity;
}
}