Documentatie GameObject
This commit is contained in:
@@ -33,15 +33,17 @@ public abstract class GameObject {
|
||||
* @param radius
|
||||
*
|
||||
* @post ...
|
||||
* |setWorld(world)
|
||||
* |new.getLocation() == location
|
||||
* @post ...
|
||||
* |world.add(this)
|
||||
* |new.getRadius() == radius
|
||||
* @post ...
|
||||
* |setLocation(location)
|
||||
* |if (isValidWorld(world))
|
||||
* | world.add(this)
|
||||
* @post ...
|
||||
* |setRadius(radius)
|
||||
* |new.getWorld() == world
|
||||
*
|
||||
* @throws todo?
|
||||
* @throws IllegalArgumentException ...
|
||||
* |!isValidLocation(location)
|
||||
*/
|
||||
protected GameObject(World world, Coordinate location, double radius) {
|
||||
if (!isValidLocation(location)) throw new IllegalArgumentException("Illegal location");
|
||||
@@ -69,7 +71,9 @@ public abstract class GameObject {
|
||||
|
||||
/**
|
||||
* @param world
|
||||
* @return ... |result == !world.hasActiveGame() && !world.isTerminated()
|
||||
*
|
||||
* @return ...
|
||||
* |result == world != null && !world.hasActiveGame() && !world.isTerminated()
|
||||
*/
|
||||
public static boolean isValidWorld(World world) {
|
||||
return world != null && !world.hasActiveGame() && !world.isTerminated();
|
||||
@@ -78,11 +82,16 @@ public abstract class GameObject {
|
||||
|
||||
/**
|
||||
* @param world
|
||||
*
|
||||
* @post ...
|
||||
* |if world == null
|
||||
* |if (world == null)
|
||||
* | this.world = null
|
||||
* |else:
|
||||
* | this.world = world
|
||||
* | return
|
||||
* @post ...
|
||||
* |new.getWorld() == world
|
||||
*
|
||||
* @throws IllegalArgumentException ...
|
||||
* |!isValidWorld(world)
|
||||
*/
|
||||
public void setWorld(World world) throws IllegalArgumentException {
|
||||
if (world == null) {
|
||||
@@ -93,9 +102,6 @@ public abstract class GameObject {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected World world;
|
||||
|
||||
// ===================================================================================
|
||||
@@ -106,16 +112,16 @@ public abstract class GameObject {
|
||||
//===================================================================================
|
||||
|
||||
/**
|
||||
* Return the location of the game object
|
||||
* the location of the worm expresses the place of the game object
|
||||
* in the play area
|
||||
* @return ...
|
||||
* |result == this.location
|
||||
*/
|
||||
public Coordinate getLocation() {
|
||||
return this.location;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ... |result == this.location.toArray()
|
||||
* @return ...
|
||||
* |result == this.location.toArray()
|
||||
*/
|
||||
public double[] getLocationArray() {
|
||||
return this.location.toArray();
|
||||
@@ -123,10 +129,21 @@ public abstract class GameObject {
|
||||
|
||||
/**
|
||||
* @param location
|
||||
* @throws IllegalArgumentException ...
|
||||
* |!isValidLocation(location)
|
||||
*
|
||||
* @post ...
|
||||
* this.location = location
|
||||
* |if (!isValidLocation(location)
|
||||
* | if (!(location.getX() - radius < 0) || !(location.getX() + radius > getWorld().getWidth()) ||
|
||||
* | !(location.getY() + radius > getWorld().getHeight()) || !(location.getY() - radius < 0))
|
||||
* | world.remove(this)
|
||||
* @post ...
|
||||
* |if (isValidLocation(location)
|
||||
* | new.getLocation() == location
|
||||
*
|
||||
* @throws IllegalArgumentException ...
|
||||
* |!isValidLocation(location) && (location.getX() - radius < 0 ||
|
||||
* |location.getX() + radius > getWorld().getWidth() ||
|
||||
* |location.getY() + radius > getWorld().getHeight() ||
|
||||
* |location.getY() - radius < 0)
|
||||
*/
|
||||
protected void setLocation(Coordinate location) throws IllegalArgumentException {
|
||||
|
||||
@@ -142,13 +159,10 @@ public abstract class GameObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* set the location of the worm to the given location
|
||||
* @param location
|
||||
*
|
||||
* @param location the new location for the game object
|
||||
* @throws IllegalArgumentException the given location is not a valid location for a game object
|
||||
* |! isValidLocation(location)
|
||||
* @post the new location of the game object is equal to the given location
|
||||
* |new.getLocation() == location
|
||||
* @post ...
|
||||
* |new.getLocation() == Coordinate.create(location)
|
||||
*/
|
||||
protected void setLocation(double[] location) throws IllegalArgumentException {
|
||||
|
||||
@@ -158,15 +172,14 @@ public abstract class GameObject {
|
||||
|
||||
/**
|
||||
* @param location
|
||||
* @return ... |if (world == null)
|
||||
*
|
||||
* @return ...
|
||||
* |if (world == null)
|
||||
* | result == !Double.isNaN(location.getX()) && !Double.isNaN(location.getY())
|
||||
* |result == !Double.isNaN(location.getX()) &&
|
||||
* | !Double.isNaN(location.getY()) &&
|
||||
* | !(location.getX() - radius < 0) &&
|
||||
* | !(location.getX() + radius > getWorld().getWidth()) &&
|
||||
* | !(location.getY() + radius > getWorld().getHeight()) &&
|
||||
* | !(location.getY() - radius < 0 &&
|
||||
* | !getWorld().isPassable(location))
|
||||
* |result == !Double.isNaN(location.getX()) && !Double.isNaN(location.getY()) &&
|
||||
* | !(location.getX() - getRadius() < 0) && !(location.getX() + getRadius() > getWorld().getWidth()) &&
|
||||
* | !(location.getY() + getRadius() > getWorld().getHeight()) && !(location.getY() - getRadius() < 0) &&
|
||||
* | getWorld().isPassable(location)
|
||||
*/
|
||||
protected boolean isValidLocation(Coordinate location) {
|
||||
double radius = getRadius();
|
||||
@@ -183,9 +196,6 @@ public abstract class GameObject {
|
||||
!(location.getY() - radius < 0) && getWorld().isPassable(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* this variable contains the location of the GameObject (a Coordinate)
|
||||
*/
|
||||
protected Coordinate location;
|
||||
|
||||
// ===================================================================================
|
||||
@@ -196,9 +206,8 @@ public abstract class GameObject {
|
||||
//===================================================================================
|
||||
|
||||
/**
|
||||
* Return the radius of the game object
|
||||
* the radius of the game object expresses half of the
|
||||
* width of the game object
|
||||
* @return ...
|
||||
* |result == this.radius
|
||||
*/
|
||||
public double getRadius() {
|
||||
return this.radius;
|
||||
@@ -206,26 +215,25 @@ public abstract class GameObject {
|
||||
|
||||
/**
|
||||
* @param radius
|
||||
*
|
||||
* @post ...
|
||||
* |this.radius = radius
|
||||
* |new.getRadius() == radius
|
||||
*
|
||||
* @throws IllegalArgumentException ...
|
||||
* |!canHaveAsRadius(radius)
|
||||
*/
|
||||
protected void setRadius(double radius) {
|
||||
if (!canHaveAsRadius(radius)) throw new IllegalArgumentException();
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* This variable contains the radius of the game object
|
||||
*/
|
||||
protected double radius;
|
||||
|
||||
/**
|
||||
* Check whether the given radius is a valid radius for the worm
|
||||
* @param radius
|
||||
*
|
||||
* @param radius the radius to check
|
||||
* @return True if and only if the radius is bigger then the minimum radius
|
||||
* (or equal) and the radius is a number
|
||||
* |result == (radius >= getMinRadius() && !Double.isNaN(radius))
|
||||
* @return ...
|
||||
* |result == !Double.isNaN(radius) && radius > 0
|
||||
*/
|
||||
@Raw
|
||||
private boolean canHaveAsRadius(double radius) {
|
||||
@@ -240,28 +248,25 @@ public abstract class GameObject {
|
||||
//===================================================================================
|
||||
|
||||
/**
|
||||
* @return ... |result == this.mass
|
||||
* @return ...
|
||||
* |result == this.mass
|
||||
*/
|
||||
public double getMass() {
|
||||
return this.mass;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the mass of the GameObject to the given mass (dependent on the radius)
|
||||
* @param radius
|
||||
* @param rho
|
||||
*
|
||||
* @param radius part of the formula to calculate the mass
|
||||
* @post the new mass of the worm is equal to
|
||||
* rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
|
||||
* |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
|
||||
* @post ...
|
||||
* |new.getMass() == rho * (4.0 / 3.0 * PI * pow(radius, 3.0)
|
||||
*/
|
||||
protected void setMass(double radius, double rho) {
|
||||
|
||||
this.mass = rho * (4.0 / 3.0 * PI * pow(radius, 3.0));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected double mass;
|
||||
|
||||
// ===================================================================================
|
||||
@@ -272,11 +277,25 @@ public abstract class GameObject {
|
||||
// region distance calc
|
||||
//===================================================================================
|
||||
|
||||
/**
|
||||
* @param o
|
||||
*
|
||||
* @return ...
|
||||
* |result == getDistance(o.getLocation(), o.getRadius())
|
||||
*/
|
||||
public double getDistance(GameObject o) {
|
||||
|
||||
return getDistance(o.getLocation(), o.getRadius());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param otherLocation
|
||||
* @param radius
|
||||
*
|
||||
* @return ...
|
||||
* |result == Math.round((Math.sqrt(Math.pow((otherLocation.getX() - this.location.getX()), 2) + Math.pow(
|
||||
* |(otherLocation.getY() - this.location.getY()), 2)) - radius - this.radius) * 10000.0) / 10000.0
|
||||
*/
|
||||
public double getDistance(Coordinate otherLocation, double radius) {
|
||||
|
||||
return Math.round((Math.sqrt(Math.pow((otherLocation.getX() - this.location.getX()), 2) +
|
||||
@@ -284,14 +303,12 @@ public abstract class GameObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the distance between two coordinates.
|
||||
* @param start
|
||||
* @param end
|
||||
*
|
||||
* @param start The start coordinate of the equation.
|
||||
* @param end The end coordinate of the equation.
|
||||
* @return the distance between the two given coordinates. The distance is equal to
|
||||
* the square root of the square of the displacement of the x coordinate plus the
|
||||
* square of the displacement of the y coordinate.
|
||||
* |result == sqrt(pow(start.getX()-end.getX())+pow(start.getY()-end.getY()))
|
||||
* @return ...
|
||||
* |result == Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) +
|
||||
* | Math.pow(Math.abs(start.getY() - end.getY()), 2))
|
||||
*/
|
||||
public static double getDistance(Coordinate start, Coordinate end) {
|
||||
return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) +
|
||||
@@ -299,10 +316,14 @@ public abstract class GameObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the angle between (dit?) this and the specified GameObject
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*
|
||||
* @return ...
|
||||
* |if (o.equals(this))
|
||||
* | result == Double.NaN
|
||||
* |Coordinate otherLoc = o.getLocation()
|
||||
* |result == Math.abs(Math.atan(Math.abs(this.location.getX() - otherLoc.getX()) /
|
||||
* | Math.abs(this.location.getY() - otherLoc.getY())) - Math.PI / 2.0)
|
||||
*/
|
||||
public double getAngle(GameObject o) {
|
||||
|
||||
@@ -331,8 +352,10 @@ public abstract class GameObject {
|
||||
|
||||
/**
|
||||
* @post ...
|
||||
* |this.terminated = true
|
||||
* |getWorld().remove(this)
|
||||
* |new.isTerminated() == true
|
||||
* @post ...
|
||||
* |if (this.world != null)
|
||||
* | this.world.remove(this)
|
||||
*/
|
||||
public void terminate() {
|
||||
this.terminated = true;
|
||||
@@ -341,9 +364,6 @@ public abstract class GameObject {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private boolean terminated = false;
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user