diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 24d39ed..21dc85f 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.*; @@ -58,7 +58,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); } @@ -100,7 +100,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)) @@ -141,7 +141,7 @@ public class Worm { * in the play area */ @Basic @Immutable @Raw - public Tuple getLocation() { + public Coordinate getLocation() { return this.location; } @@ -152,18 +152,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 @@ -177,7 +176,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"); @@ -562,13 +561,13 @@ public class Worm { * 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 + * |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 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 @@ -589,8 +588,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); } @@ -665,8 +664,8 @@ public class Worm { * let the worm jump * * @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)) + * with the jump velocity + * |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 @@ -679,7 +678,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); } @@ -705,22 +704,22 @@ public class Worm { * * @param deltaTime * the total time the worm should jump - * @return Tuple with the new location of the worm. The new x-coordinate is the old x-coordinate plus the jump velocity + * @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; + } +}