From ed3843fc1eabfd5a78f21e75f423569c0c675355 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Sat, 7 Apr 2018 21:35:09 +0200 Subject: [PATCH] finished abstract class --- OGP1718-Worms/src/worms/model/GameObject.java | 75 +++++++++++--- OGP1718-Worms/src/worms/model/World.java | 14 +-- OGP1718-Worms/src/worms/model/Worm.java | 97 ++----------------- OGP1718-Worms/src/worms/util/Coordinate.java | 6 ++ 4 files changed, 79 insertions(+), 113 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index f6a823b..90aeb4c 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -1,5 +1,6 @@ package worms.model; +import be.kuleuven.cs.som.annotate.Raw; import worms.util.Coordinate; import static java.lang.Math.PI; @@ -8,12 +9,12 @@ import static java.lang.Math.round; public abstract class GameObject { - public GameObject(World world, Coordinate location, double radius) { - - this.world = world; - this.location = location; - this.radius = radius; - } +// public GameObject(World world, Coordinate location, double radius) { +// +// this.world = world; +// this.location = location; +// this.radius = radius; +// } private World world; @@ -28,6 +29,7 @@ public abstract class GameObject { public boolean isTerminated() { return this.terminated; } + public void terminate() { this.terminated = true; } @@ -35,29 +37,72 @@ public abstract class GameObject { + + /** + * this variable contains the location of the worm (a Coordinate) + */ private Coordinate location; - public double[] getLocation() { - return null; + + /** + * Return the location of the game object + * the location of the worm expresses the place of the game object + * in the play area + */ + Coordinate getLocation() { + return this.location; + } + public double[] getLocationArray() { + return this.location.toArray(); } - private void setLocation(Coordinate location) { + /** + * set the location of the worm to the given location + * + * @param location + * the new location for the game object + * @post the new location of the game object is equal to the given location + * |new.getLocation() == location + * @throws IllegalArgumentException + * the given location is not a valid location for a game object + * |! isValidLocation(location) + */ + void setLocation(Coordinate location) throws IllegalArgumentException { + if (!isValidLocation(location)) throw new IllegalArgumentException(); this.location = location; } - private double radius; + + /** + * This variable contains the radius of the game object + */ + double radius; + + /** + * Return the radius of the game object + * the radius of the game object expresses half of the + * width of the game object + */ public double getRadius(){ return this.radius; } - abstract void setRadius(); + abstract void setRadius(double radius); + boolean isValidLocation(Coordinate location) { + double radius = getRadius(); + return !Double.isNaN(location.getCoordinateX()) && + !Double.isNaN(location.getCoordinateY()) && + !(location.getCoordinateX() - radius < 0) && + !(location.getCoordinateX() + radius > getWorld().getWidth()) && + !(location.getCoordinateY() + radius > getWorld().getHeight()) && + !(location.getCoordinateY() - radius < 0); + } private double mass; public double getMass() { return this.mass; } - private void setMass() { - this.mass = (double) round(rho * (4.0 / 3.0 * PI * pow(radius, 3))); - } - private final double rho = 0; + + @Raw + abstract void setMass(double radius); } \ No newline at end of file diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 8655299..f0cd562 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -217,13 +217,13 @@ public class World { return all; } -// public Set getAllTeams() { -// -// Set teams = new HashSet(); -// for(Worm worm: getWormList()) { -// teams.add(worm.getTeam()); -// } -// } + public Set getAllTeams() { + + Set teams = new HashSet(); + for(Worm worm: getWormList()) { + teams.add(worm.getTeam()); + } + } diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index c3afa9b..277c276 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -32,7 +32,7 @@ import java.util.Random; * * URL: https://github.com/KUL-ogp/ogp1718-project-bols-dereu */ -public class Worm { +public class Worm extends GameObject { // region constructor //=================================================================================== @@ -129,61 +129,6 @@ public class Worm { setHitPoints(startHitPoints); } - //=================================================================================== - // endregion - - // region location - //=================================================================================== - - /** - * Return the location of the worm - * the location of the worm expresses the place of the worm - * in the play area - */ - @Basic @Immutable @Raw - public Coordinate getLocation() { - return this.location; - } - - /** - * check whether the given location is a valid location for the worm - * - * @param location - * the location to check - * @return True if and only if the location is not equal to null and the coordinates of - * the worm are numbers - * |result == ( (location != null) && (location.getCoordinateX() != null) && (location.getCoordinateY() != null) - * |&& (!Double.isNaN(location.getCoordinateX())) && (!Double.isNaN(location.getCoordinateY())) ) - */ - public static boolean isValidLocation(Coordinate location) { - return location != null && !Double.isNaN(location.getCoordinateX()) && !Double.isNaN(location.getCoordinateY()); - } - - /** - * this variable contains the location of the worm (a Coordinate) - */ - private Coordinate location; - - /** - * set the location of the worm to the given location - * - * @param location - * the new location for the worm - * @post the new location of the worm is equal to the given location - * |new.getLocation() == location - * @throws IllegalArgumentException - * the given location is not a valid location for a worm - * |! isValidLocation(location) - */ - @Raw - private void setLocation(Coordinate location) throws IllegalArgumentException { - - if (!isValidLocation(location)) - throw new IllegalArgumentException("Illegal value for location"); - this.location = location; - } - - //=================================================================================== // endregion @@ -241,15 +186,7 @@ public class Worm { // region Shape mass/radius //=================================================================================== - /** - * Return the radius of the worm - * the radius of the worm expresses half of the - * width of the worm - */ - @Basic @Raw - public double getRadius() { - return this.radius; - } + /** * check whether the given radius is a valid minimum radius for the worm @@ -274,7 +211,7 @@ public class Worm { * the given radius is not a valid radius for any worm * |! canHaveAsMinRadius(radius) */ - @Raw + @Raw @Override public void setRadius(double radius) throws IllegalArgumentException { if (!canHaveAsRadius(radius)) throw new IllegalArgumentException("Invalid radius"); @@ -293,12 +230,6 @@ public class Worm { return this.minRadius; } - - /** - * This variable contains the radius of the worm - */ - private double radius; - /** * This variable contains the minimum value of the radius */ @@ -341,8 +272,8 @@ public class Worm { * rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) * |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) */ - @Raw - private void setMass(double radius) { + @Raw @Override + protected void setMass(double radius) { final double rho = 1062.0; double mass = round(rho * (4.0 / 3.0 * PI * pow(radius, 3))); this.mass = mass; @@ -823,23 +754,7 @@ public class Worm { public void incrementHitPoints(long value) { setHitPoints(getHitPoints() - value); } - - /** - * terminate the worm - */ - public void terminate() { - terminate = true; - setLocation(Coordinate.create(Double.NaN, Double.NaN)); - } - - public boolean isAlive() { - if (terminate == false) { - return true; - } - return false; - } - - private boolean terminate = false; + //=================================================================================== // endregion diff --git a/OGP1718-Worms/src/worms/util/Coordinate.java b/OGP1718-Worms/src/worms/util/Coordinate.java index 9139c9c..6cbfb80 100644 --- a/OGP1718-Worms/src/worms/util/Coordinate.java +++ b/OGP1718-Worms/src/worms/util/Coordinate.java @@ -49,6 +49,12 @@ public class Coordinate extends Tuple { throw new IllegalArgumentException("Invalid parameter arr"); return new Coordinate(arr[0], arr[1]); } + + public double[] toArray() { + + return new double[]{getCoordinateX(), getCoordinateY()}; + } + @Basic @Immutable public double getCoordinateX() { return this.item1;