diff --git a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java index 3df698d..c3f848c 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java @@ -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)); + + } } \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java index 3b8c1fc..d737073 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java @@ -60,6 +60,7 @@ public class AsteroidGame extends Thread { public void createGame() { mWorld = new Vector(); 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 getWorld() { diff --git a/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java index f25cf27..bab4786 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java +++ b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java @@ -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 diff --git a/Asteroids/src/com/RickBarrette/asteroids/Ship.java b/Asteroids/src/com/RickBarrette/asteroids/Ship.java index cde6396..91467d3 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Ship.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Ship.java @@ -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; } } \ No newline at end of file