From 47ae1f58c18b21ec6fdbea83bbc70be9e4c87173 Mon Sep 17 00:00:00 2001 From: Leen Dereu Date: Thu, 24 May 2018 11:15:11 +0200 Subject: [PATCH] Documentatie GameObject --- OGP1718-Worms/src/worms/model/GameObject.java | 174 ++++++++++-------- 1 file changed, 97 insertions(+), 77 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index 53f823d..4ce6e60 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -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"); @@ -60,7 +62,7 @@ public abstract class GameObject { /** * @return ... - * |result == this.world + * |result == this.world */ public World getWorld() { return this.world; @@ -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 - * | this.world = null - * |else: - * | this.world = world + * |if (world == null) + * | this.world = null + * | 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) - * | 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)) + * + * @return ... + * |if (world == null) + * | result == !Double.isNaN(location.getX()) && !Double.isNaN(location.getY()) + * |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) { @@ -322,8 +343,8 @@ public abstract class GameObject { //=================================================================================== /** - * @return ... - * |result == this.terminated + * @return ... + * |result == this.terminated */ public boolean isTerminated() { return this.terminated; @@ -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;