From 2c517b0cf8484f919e03de42786731379ee81904 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Fri, 23 Mar 2018 13:29:05 +0100 Subject: [PATCH] added coordinate type --- OGP1718-Worms/src/worms/model/Worm.java | 59 ++++++++++---------- OGP1718-Worms/src/worms/util/Coordinate.java | 42 ++++++++++++++ 2 files changed, 71 insertions(+), 30 deletions(-) create mode 100644 OGP1718-Worms/src/worms/util/Coordinate.java diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 63f8b2b..9a901b9 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -1,7 +1,7 @@ package worms.model; import be.kuleuven.cs.som.annotate.*; -import worms.util.Tuple; +import worms.util.Coordinate; import worms.util.IllegalNameException; import static java.lang.Math.*; @@ -56,7 +56,7 @@ public class Worm { * |new.getRadius() == radius */ @Raw - public Worm(Tuple location, double orientation, String name, double radius) { + public Worm(Coordinate location, double orientation, String name, double radius) { this(location, orientation, name, radius, 0.25); } @@ -98,7 +98,7 @@ public class Worm { * |isValidName(name) */ @Raw - public Worm(Tuple location, double orientation, String name, double radius, double minRadius) + public Worm(Coordinate location, double orientation, String name, double radius, double minRadius) throws IllegalArgumentException { if (!isValidLocation(location)) @@ -136,7 +136,7 @@ public class Worm { * in the play area */ @Basic @Immutable @Raw - public Tuple getLocation() { + public Coordinate getLocation() { return this.location; } @@ -147,18 +147,17 @@ public class Worm { * 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.item1 != null) && (location.item2 != null) - * |&& (!Double.isNaN(location.item1)) && (!Double.isNaN(location.item2)) ) + * |result == ( (location != null) && (location.getCoordinateX() != null) && (location.getCoordinateY() != null) + * |&& (!Double.isNaN(location.getCoordinateX())) && (!Double.isNaN(location.getCoordinateY())) ) */ - public static boolean isValidLocation(Tuplelocation) { - return location != null && location.item1 != null && location.item2 != null - && !Double.isNaN(location.item1) && !Double.isNaN(location.item2); + 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 tuple) + * this variable contains the location of the worm (a Coordinate) */ - private Tuple location; + private Coordinate location; /** * set the location of the worm to the given location @@ -172,7 +171,7 @@ public class Worm { * |! isValidLocation(location) */ @Raw - private void setLocation(Tuple location) throws IllegalArgumentException { + private void setLocation(Coordinate location) throws IllegalArgumentException { if (!isValidLocation(location)) throw new IllegalArgumentException("Illegal value for location"); @@ -552,18 +551,18 @@ public class Worm { * @param numberSteps * the number of steps the worm should take * - * @post the x-coordinate of the new location of the worm schould be the location of + * @post the x-coordinate of the new location of the worm should be the location of * the old x-coordinate plus the number of steps multiplied with the distance - * that the x-coordinate schould move (distance is equal to the radius multiplied + * that the x-coordinate should move (distance is equal to the radius multiplied * with the cosinus of the orientation) * |distanceX = this.radius * cos(getOrientation()) - * |new.xCoordinate = getLocation().item1 + numberSteps * distanceX - * @post the y-coordinate of the new location of the worm schould be the location of + * |new.xCoordinate = getLocation().getCoordinateX() + numberSteps * distanceX + * @post the y-coordinate of the new location of the worm should be the location of * the old y-coordinate plus the number of steps multiplied with the distance - * that the y-coordinate schould move (distance is equal to the radius multiplied + * that the y-coordinate should move (distance is equal to the radius multiplied * with the sinus of the orientation) * |distanceY = this.radius * sin(getOrientation()) - * |new.yCoordinate = getLocation().item2 + numberSteps * distanceY + * |new.yCoordinate = getLocation().getCoordinateY() + numberSteps * distanceY * @post the current value of action actionPoints has changed. The current value of action actionPoints * minus the cost of moving (abs(cos(theta)) + abs(4 sin(theta))) * |cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))) * numberSteps @@ -584,8 +583,8 @@ public class Worm { double distanceX = getRadius() * cos(getOrientation()); double distanceY = getRadius() * sin(getOrientation()); - setLocation(Tuple.create(getLocation().item1 + numberSteps * distanceX, - getLocation().item2 + numberSteps * distanceY)); + setLocation(Coordinate.create(getLocation().getCoordinateX() + numberSteps * distanceX, + getLocation().getCoordinateY() + numberSteps * distanceY)); subtractActionPoints(cost); } @@ -657,8 +656,8 @@ public class Worm { * * @post the worm jumps to his new place. The new place is the x-coordinate plus the jump distance * with the jump velocity - * |setLocation(Tuple.create(getLocation().item1 + jumpDistance(this.jumpVelocity()), getLocation().item2)) - * @post the current action actionPoints schould be 0 after a jump + * |setLocation(Coordinate.create(getLocation().getCoordinateX() + jumpDistance(this.jumpVelocity()), getLocation().getCoordinateY())) + * @post the current action actionPoints should be 0 after a jump * |setActionPoints(0) * @throws IllegalStateException * if the current action actionPoints is equal to 0 or the orientation is more then @@ -670,7 +669,7 @@ public class Worm { if (!canJump()) throw new IllegalStateException(); - setLocation(Tuple.create(getLocation().item1 + jumpDistance(this.jumpVelocity()), getLocation().item2)); + setLocation(Coordinate.create( getLocation().getCoordinateX() + jumpDistance(this.jumpVelocity()), getLocation().getCoordinateY())); setActionPoints(0); } @@ -695,23 +694,23 @@ public class Worm { /** * * @param deltaTime - * the total time the worm schould jump - * @return Tuple with the new location of the worm. The new x-coordinate is the old x-coordinate plus the jump velocity + * the total time the worm should jump + * @return Coordinate with the new location of the worm. The new x-coordinate is the old x-coordinate plus the jump velocity * multiplied with the cosinus of the orientation multiplied with delta time. The new y-coordinate is the old y-coordinate plus * the jump velocity multiplied with the sinus of the orientation multiplied with delta time minus 0,5 times the gravity multiplied * with the second power of delta time - * |Tuple.create(getLocation().item1 + this.jumpVelocity() * cos(getOrientation()) * deltaTime, - |getLocation().item2 + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)) + * |Coordinate.create(getLocation().getCoordinateX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime, + |getLocation().getCoordinateY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)) * @throws IllegalArgumentException() * if the deltaTime is not a number or bigger then jumpTime or smaller then 0 * |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) */ - public Tuple jumpStep(double deltaTime) { + public Coordinate jumpStep(double deltaTime) { if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) throw new IllegalArgumentException(); - return Tuple.create(getLocation().item1 + this.jumpVelocity() * cos(getOrientation()) * deltaTime, - getLocation().item2 + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)); + return Coordinate.create(getLocation().getCoordinateX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime, + getLocation().getCoordinateY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)); } /** diff --git a/OGP1718-Worms/src/worms/util/Coordinate.java b/OGP1718-Worms/src/worms/util/Coordinate.java new file mode 100644 index 0000000..f9a9656 --- /dev/null +++ b/OGP1718-Worms/src/worms/util/Coordinate.java @@ -0,0 +1,42 @@ +package worms.util; + +import be.kuleuven.cs.som.annotate.Basic; +import be.kuleuven.cs.som.annotate.Immutable; +import be.kuleuven.cs.som.annotate.Value; + +@Value +public class Coordinate extends Tuple { + /** + * Initialize this new Tuple as a Tuple with the given items. + * + * @param coordinateX The x-coordinate for this Coordinate + * @param coordinateY The y-coordinate for this Coordinate + */ + public Coordinate(double coordinateX, double coordinateY) throws IllegalArgumentException { + super(coordinateX, coordinateY); + } + + /** + * + * @param coordinateX + * The value for the x-coordinate. + * @param coordinateY + * The value for the y-coordinate. + * @return + * The coordinate created from coordinateX and coordinateY. + * | result == new Coordinate(coordinateX, coordinateY); + */ + @Immutable + public static Coordinate create(double coordinateX, double coordinateY){ + return new Coordinate(coordinateX, coordinateY); + } + + @Basic @Immutable + public double getCoordinateX() { + return this.item1; + } + @Basic @Immutable + public double getCoordinateY() { + return this.item2; + } +}