Documentatie GameObject

This commit is contained in:
Leen Dereu
2018-05-24 11:15:11 +02:00
parent f21a01e1d3
commit 47ae1f58c1

View File

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