diff --git a/docs/28641484_1576783732442687_76036637_o (1).jpg b/docs/28641484_1576783732442687_76036637_o (1).jpg new file mode 100644 index 0000000..168e9ad Binary files /dev/null and b/docs/28641484_1576783732442687_76036637_o (1).jpg differ diff --git a/src/Tuple.java b/src/Tuple.java index 8d29dfe..5cc444a 100644 --- a/src/Tuple.java +++ b/src/Tuple.java @@ -3,7 +3,7 @@ import be.kuleuven.cs.som.annotate.*; /** - * A tuple implementation + * A tuple (pair) implementation * * @param first element type * @param second element type diff --git a/src/Worm.java b/src/Worm.java index 1b43e7d..abf37de 100644 --- a/src/Worm.java +++ b/src/Worm.java @@ -6,7 +6,8 @@ public class Worm { // Location - private double[] location; + private Tuple location; + private Tuple oldLocation; // Orientation private double orientation; @@ -22,7 +23,7 @@ public class Worm { private int maxPoints; // name - private final String name; + private String name; //=================================================================================== // endregion @@ -38,7 +39,7 @@ public class Worm { * @param name ... * @param radius ... */ - public Worm(double[] location, double orientation, String name, double radius) { + public Worm(Tuple location, double orientation, String name, double radius) { this(location, orientation, name, radius, 0.25); } @@ -52,7 +53,7 @@ public class Worm { * @param minRadius ... * @throws IllegalArgumentException ... */ - public Worm(double[] location, double orientation, String name, double radius, double minRadius) + public Worm(Tuple location, double orientation, String name, double radius, double minRadius) throws IllegalArgumentException { setLocation(location); @@ -81,13 +82,13 @@ public class Worm { //=================================================================================== - public double[] getLocation() { + public Tuple getLocation() { return this.location; } - private void setLocation(double[] location) throws IllegalArgumentException { + private void setLocation(Tuple location) throws IllegalArgumentException { - if (location == null || location.length != 2) + if (location.equals(null)) throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg this.location = location; } @@ -194,6 +195,16 @@ public class Worm { public String getName() { return this.name; } + + public void setName(String name) { + + int validName = isValidName(name); + if (validName != -1) + throw new IllegalNameException(validName, name); + + this.name = name; + } + /** * @param name ... * @return ... @@ -247,10 +258,8 @@ public class Worm { double distanceX = this.radius * Math.cos(this.orientation); double distanceY = this.radius * Math.sin(this.orientation); - this.location = new double[] { - this.location[0] + numberSteps * distanceX, - this.location[1] + numberSteps * distanceY - }; + this.location = Tuple.create(this.location.item1 + numberSteps * distanceX, + this.location.item2 + numberSteps * distanceY); subtractPoints(cost); } @@ -288,10 +297,46 @@ public class Worm { // region Jump //=================================================================================== + private final double G = 5.0; + private final double FORCE_TIME = 0.5; - + public void jump() { + + if (!canJump()) + throw new IllegalStateException(); + + this.oldLocation = this.location; + this.location = Tuple.create(location.item1 + jumpDistance(jumpVelocity()), location.item2); + + this.points = 0; + } + + private boolean canJump() { + return this.points > 0 && this.orientation < Math.PI; + } + + public double jumpTime() { + + double v = jumpVelocity(); + return jumpDistance(v) / (v * Math.cos(this.orientation)); + } + public double jumpStep() { + + return 0.0; + } + private double jumpDistance(double v) { + return (Math.pow(v, 2) * Math.sin(2 * this.orientation)) / this.G; + } + + private double jumpVelocity() { + + double force = 5 * this.points + this.mass * G; + return (force / this.mass) * FORCE_TIME; + } //=================================================================================== // endregion + + }