implemented jump and small changes

This commit is contained in:
2018-03-07 15:38:00 +01:00
parent 50f8424f1f
commit 51840ceee5
3 changed files with 58 additions and 13 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 908 KiB

View File

@@ -3,7 +3,7 @@ import be.kuleuven.cs.som.annotate.*;
/** /**
* A tuple implementation * A tuple (pair) implementation
* *
* @param <T1> first element type * @param <T1> first element type
* @param <T2> second element type * @param <T2> second element type

View File

@@ -6,7 +6,8 @@ public class Worm {
// Location // Location
private double[] location; private Tuple<Double, Double> location;
private Tuple<Double, Double> oldLocation;
// Orientation // Orientation
private double orientation; private double orientation;
@@ -22,7 +23,7 @@ public class Worm {
private int maxPoints; private int maxPoints;
// name // name
private final String name; private String name;
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -38,7 +39,7 @@ public class Worm {
* @param name ... * @param name ...
* @param radius ... * @param radius ...
*/ */
public Worm(double[] location, double orientation, String name, double radius) { public Worm(Tuple<Double, Double> location, double orientation, String name, double radius) {
this(location, orientation, name, radius, 0.25); this(location, orientation, name, radius, 0.25);
} }
@@ -52,7 +53,7 @@ public class Worm {
* @param minRadius ... * @param minRadius ...
* @throws IllegalArgumentException ... * @throws IllegalArgumentException ...
*/ */
public Worm(double[] location, double orientation, String name, double radius, double minRadius) public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minRadius)
throws IllegalArgumentException { throws IllegalArgumentException {
setLocation(location); setLocation(location);
@@ -81,13 +82,13 @@ public class Worm {
//=================================================================================== //===================================================================================
public double[] getLocation() { public Tuple<Double, Double> getLocation() {
return this.location; return this.location;
} }
private void setLocation(double[] location) throws IllegalArgumentException { private void setLocation(Tuple<Double, Double> 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 throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg
this.location = location; this.location = location;
} }
@@ -194,6 +195,16 @@ public class Worm {
public String getName() { public String getName() {
return this.name; 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 ... * @param name ...
* @return ... * @return ...
@@ -247,10 +258,8 @@ public class Worm {
double distanceX = this.radius * Math.cos(this.orientation); double distanceX = this.radius * Math.cos(this.orientation);
double distanceY = this.radius * Math.sin(this.orientation); double distanceY = this.radius * Math.sin(this.orientation);
this.location = new double[] { this.location = Tuple.create(this.location.item1 + numberSteps * distanceX,
this.location[0] + numberSteps * distanceX, this.location.item2 + numberSteps * distanceY);
this.location[1] + numberSteps * distanceY
};
subtractPoints(cost); subtractPoints(cost);
} }
@@ -288,10 +297,46 @@ public class Worm {
// region Jump // 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 // endregion
} }