added coordinate type
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package worms.model;
|
package worms.model;
|
||||||
|
|
||||||
import be.kuleuven.cs.som.annotate.*;
|
import be.kuleuven.cs.som.annotate.*;
|
||||||
import worms.util.Tuple;
|
import worms.util.Coordinate;
|
||||||
import worms.util.IllegalNameException;
|
import worms.util.IllegalNameException;
|
||||||
|
|
||||||
import static java.lang.Math.*;
|
import static java.lang.Math.*;
|
||||||
@@ -56,7 +56,7 @@ public class Worm {
|
|||||||
* |new.getRadius() == radius
|
* |new.getRadius() == radius
|
||||||
*/
|
*/
|
||||||
@Raw
|
@Raw
|
||||||
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius) {
|
public Worm(Coordinate location, double orientation, String name, double radius) {
|
||||||
|
|
||||||
this(location, orientation, name, radius, 0.25);
|
this(location, orientation, name, radius, 0.25);
|
||||||
}
|
}
|
||||||
@@ -98,7 +98,7 @@ public class Worm {
|
|||||||
* |isValidName(name)
|
* |isValidName(name)
|
||||||
*/
|
*/
|
||||||
@Raw
|
@Raw
|
||||||
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minRadius)
|
public Worm(Coordinate location, double orientation, String name, double radius, double minRadius)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
|
|
||||||
if (!isValidLocation(location))
|
if (!isValidLocation(location))
|
||||||
@@ -136,7 +136,7 @@ public class Worm {
|
|||||||
* in the play area
|
* in the play area
|
||||||
*/
|
*/
|
||||||
@Basic @Immutable @Raw
|
@Basic @Immutable @Raw
|
||||||
public Tuple<Double, Double> getLocation() {
|
public Coordinate getLocation() {
|
||||||
return this.location;
|
return this.location;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,18 +147,17 @@ public class Worm {
|
|||||||
* the location to check
|
* the location to check
|
||||||
* @return True if and only if the location is not equal to null and the coordinates of
|
* @return True if and only if the location is not equal to null and the coordinates of
|
||||||
* the worm are numbers
|
* the worm are numbers
|
||||||
* |result == ( (location != null) && (location.item1 != null) && (location.item2 != null)
|
* |result == ( (location != null) && (location.getCoordinateX() != null) && (location.getCoordinateY() != null)
|
||||||
* |&& (!Double.isNaN(location.item1)) && (!Double.isNaN(location.item2)) )
|
* |&& (!Double.isNaN(location.getCoordinateX())) && (!Double.isNaN(location.getCoordinateY())) )
|
||||||
*/
|
*/
|
||||||
public static boolean isValidLocation(Tuple<Double, Double>location) {
|
public static boolean isValidLocation(Coordinate location) {
|
||||||
return location != null && location.item1 != null && location.item2 != null
|
return location != null && !Double.isNaN(location.getCoordinateX()) && !Double.isNaN(location.getCoordinateY());
|
||||||
&& !Double.isNaN(location.item1) && !Double.isNaN(location.item2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this variable contains the location of the worm (a tuple)
|
* this variable contains the location of the worm (a Coordinate)
|
||||||
*/
|
*/
|
||||||
private Tuple<Double, Double> location;
|
private Coordinate location;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set the location of the worm to the given location
|
* set the location of the worm to the given location
|
||||||
@@ -172,7 +171,7 @@ public class Worm {
|
|||||||
* |! isValidLocation(location)
|
* |! isValidLocation(location)
|
||||||
*/
|
*/
|
||||||
@Raw
|
@Raw
|
||||||
private void setLocation(Tuple<Double, Double> location) throws IllegalArgumentException {
|
private void setLocation(Coordinate location) throws IllegalArgumentException {
|
||||||
|
|
||||||
if (!isValidLocation(location))
|
if (!isValidLocation(location))
|
||||||
throw new IllegalArgumentException("Illegal value for location");
|
throw new IllegalArgumentException("Illegal value for location");
|
||||||
@@ -552,18 +551,18 @@ public class Worm {
|
|||||||
* @param numberSteps
|
* @param numberSteps
|
||||||
* the number of steps the worm should take
|
* 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
|
* 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)
|
* with the cosinus of the orientation)
|
||||||
* |distanceX = this.radius * cos(getOrientation())
|
* |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 schould be the location of
|
* @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
|
* 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)
|
* with the sinus of the orientation)
|
||||||
* |distanceY = this.radius * sin(getOrientation())
|
* |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
|
* @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)))
|
* minus the cost of moving (abs(cos(theta)) + abs(4 sin(theta)))
|
||||||
* |cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))) * numberSteps
|
* |cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))) * numberSteps
|
||||||
@@ -584,8 +583,8 @@ public class Worm {
|
|||||||
|
|
||||||
double distanceX = getRadius() * cos(getOrientation());
|
double distanceX = getRadius() * cos(getOrientation());
|
||||||
double distanceY = getRadius() * sin(getOrientation());
|
double distanceY = getRadius() * sin(getOrientation());
|
||||||
setLocation(Tuple.create(getLocation().item1 + numberSteps * distanceX,
|
setLocation(Coordinate.create(getLocation().getCoordinateX() + numberSteps * distanceX,
|
||||||
getLocation().item2 + numberSteps * distanceY));
|
getLocation().getCoordinateY() + numberSteps * distanceY));
|
||||||
subtractActionPoints(cost);
|
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
|
* @post the worm jumps to his new place. The new place is the x-coordinate plus the jump distance
|
||||||
* with the jump velocity
|
* with the jump velocity
|
||||||
* |setLocation(Tuple.create(getLocation().item1 + jumpDistance(this.jumpVelocity()), getLocation().item2))
|
* |setLocation(Coordinate.create(getLocation().getCoordinateX() + jumpDistance(this.jumpVelocity()), getLocation().getCoordinateY()))
|
||||||
* @post the current action actionPoints schould be 0 after a jump
|
* @post the current action actionPoints should be 0 after a jump
|
||||||
* |setActionPoints(0)
|
* |setActionPoints(0)
|
||||||
* @throws IllegalStateException
|
* @throws IllegalStateException
|
||||||
* if the current action actionPoints is equal to 0 or the orientation is more then
|
* if the current action actionPoints is equal to 0 or the orientation is more then
|
||||||
@@ -670,7 +669,7 @@ public class Worm {
|
|||||||
if (!canJump())
|
if (!canJump())
|
||||||
throw new IllegalStateException();
|
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);
|
setActionPoints(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -695,23 +694,23 @@ public class Worm {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param deltaTime
|
* @param deltaTime
|
||||||
* the total time the worm schould jump
|
* the total time the worm should jump
|
||||||
* @return Tuple<Double, Double> 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
|
* 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
|
* 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
|
* with the second power of delta time
|
||||||
* |Tuple.create(getLocation().item1 + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
|
* |Coordinate.create(getLocation().getCoordinateX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
|
||||||
|getLocation().item2 + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2))
|
|getLocation().getCoordinateY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2))
|
||||||
* @throws IllegalArgumentException()
|
* @throws IllegalArgumentException()
|
||||||
* if the deltaTime is not a number or bigger then jumpTime or smaller then 0
|
* if the deltaTime is not a number or bigger then jumpTime or smaller then 0
|
||||||
* |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
|
* |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
|
||||||
*/
|
*/
|
||||||
public Tuple<Double, Double> jumpStep(double deltaTime) {
|
public Coordinate jumpStep(double deltaTime) {
|
||||||
if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
|
if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
|
||||||
return Tuple.create(getLocation().item1 + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
|
return Coordinate.create(getLocation().getCoordinateX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
|
||||||
getLocation().item2 + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2));
|
getLocation().getCoordinateY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
42
OGP1718-Worms/src/worms/util/Coordinate.java
Normal file
42
OGP1718-Worms/src/worms/util/Coordinate.java
Normal file
@@ -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<Double, Double> {
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user