Fixed space wrap for the asteroids
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -23,9 +23,9 @@ package com.RickBarrette.asteroids;
|
|||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class will be used to make astroids, to destroy the ship!
|
* This class will be used to make astroids, to destroy the ship!
|
||||||
|
*
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public class Asteroid extends MovingSpaceObject implements Drawable {
|
public class Asteroid extends MovingSpaceObject implements Drawable {
|
||||||
@@ -37,23 +37,27 @@ public class Asteroid extends MovingSpaceObject implements Drawable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Asteroid
|
* Creates a new Asteroid
|
||||||
* @param x
|
*
|
||||||
|
* @param x
|
||||||
* @param y
|
* @param y
|
||||||
* @param xVelocity
|
* @param xVelocity
|
||||||
* @param yVelocity
|
* @param yVelocity
|
||||||
* @param numberSplit number of smaller asteroids to create after being blown up
|
* @param numberSplit
|
||||||
* @param hitsLeft number of hits left
|
* number of smaller asteroids to create after being blown up
|
||||||
|
* @param hitsLeft
|
||||||
|
* number of hits left
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public Asteroid(double x, double y, double minVelocity, double maxVelocity, int radius, int numberSplit, int hitsLeft, AsteroidGame game) {
|
public Asteroid(double x, double y, double minVelocity, double maxVelocity,
|
||||||
|
int radius, int numberSplit, int hitsLeft, AsteroidGame game) {
|
||||||
mGame = game;
|
mGame = game;
|
||||||
mColor = Color.GRAY;
|
mColor = Color.GRAY;
|
||||||
mX = x;
|
mX = x;
|
||||||
mY = y;
|
mY = y;
|
||||||
double vel=minVelocity + Math.random()*(maxVelocity-minVelocity);
|
double vel = minVelocity + Math.random() * (maxVelocity - minVelocity);
|
||||||
mAngle = 2*Math.PI*Math.random(); // random direction
|
mAngle = 2 * Math.PI * Math.random(); // random direction
|
||||||
mXVelocity=vel*Math.cos(mAngle);
|
mXVelocity = vel * Math.cos(mAngle);
|
||||||
mYVelocity=vel*Math.sin(mAngle);
|
mYVelocity = vel * Math.sin(mAngle);
|
||||||
|
|
||||||
mNumberSplit = numberSplit;
|
mNumberSplit = numberSplit;
|
||||||
mHitsLeft = hitsLeft;
|
mHitsLeft = hitsLeft;
|
||||||
@@ -64,8 +68,26 @@ public class Asteroid extends MovingSpaceObject implements Drawable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
g.setColor(mColor);
|
g.setColor(mColor);
|
||||||
g.fillOval((int)(mX-mRadius+.5),(int)(mY-mRadius+.5), (int)(2*mRadius),(int)(2*mRadius));
|
g.fillOval((int) (mX - mRadius + .5), (int) (mY - mRadius + .5),
|
||||||
|
(int) (2 * mRadius), (int) (2 * mRadius));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see com.RickBarrette.asteroids.MovingSpaceObject#wrapSpace(int, int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void wrapSpace(int scrnHeight, int scrnWidth) {
|
||||||
|
if (mX < 0 - mRadius)
|
||||||
|
mX += scrnHeight + 2 * mRadius;
|
||||||
|
else if (mX > scrnHeight + mRadius)
|
||||||
|
mX -= scrnHeight + 2 * mRadius;
|
||||||
|
|
||||||
|
if (mY < 0 - mRadius)
|
||||||
|
mY += scrnWidth + 2 * mRadius;
|
||||||
|
else if (mY > scrnWidth + mRadius)
|
||||||
|
mY -= scrnWidth + 2 * mRadius;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -123,20 +123,9 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
|
|||||||
mXVelocity *= mVelocityDecay;
|
mXVelocity *= mVelocityDecay;
|
||||||
mYVelocity *= mVelocityDecay;
|
mYVelocity *= mVelocityDecay;
|
||||||
|
|
||||||
/*
|
wrapSpace(scrnHeight, scrnWidth);
|
||||||
* wrap the ship around to the opposite side of the screen
|
|
||||||
* when it goes out of the screen's bounds
|
|
||||||
*/
|
|
||||||
if (mX < 0)
|
|
||||||
mX += scrnHeight;
|
|
||||||
else if (mX > scrnHeight)
|
|
||||||
mX -= scrnHeight;
|
|
||||||
if (mY < 0)
|
|
||||||
mY += scrnWidth;
|
|
||||||
else if (mY > scrnWidth)
|
|
||||||
mY -= scrnWidth;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets wether or not this space object is accelerating
|
* Sets wether or not this space object is accelerating
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
@@ -176,4 +165,19 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
|
|||||||
this.isTurningRight = turningRight;
|
this.isTurningRight = turningRight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* wraps the space object around to the opposite side of the screen
|
||||||
|
* when it goes out of the screen's bounds
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public void wrapSpace(int scrnHeight, int scrnWidth) {
|
||||||
|
if (mX < 0)
|
||||||
|
mX += scrnHeight;
|
||||||
|
else if (mX > scrnHeight)
|
||||||
|
mX -= scrnHeight;
|
||||||
|
if (mY < 0)
|
||||||
|
mY += scrnWidth;
|
||||||
|
else if (mY > scrnWidth)
|
||||||
|
mY -= scrnWidth;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user