overall improvements
This commit is contained in:
@@ -4,6 +4,8 @@ import be.kuleuven.cs.som.annotate.*;
|
||||
import worms.util.Tuple;
|
||||
import worms.util.IllegalNameException;
|
||||
|
||||
import static java.lang.Math.*;
|
||||
|
||||
public class Worm {
|
||||
/**
|
||||
* a class with the specifications of the worm
|
||||
@@ -94,14 +96,18 @@ public class Worm {
|
||||
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minimumRadius)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
setLocation(location);
|
||||
if (!isValidLocation(location))
|
||||
throw new IllegalArgumentException("Illegal value for location");
|
||||
this.location = location;
|
||||
|
||||
setOrientation(orientation);
|
||||
|
||||
if (!isValidMininmumRadius(minimumRadius))
|
||||
if (!isValidMinimumRadius(minimumRadius))
|
||||
throw new IllegalArgumentException("Invalid minimum radius"); // TODO add decent exception msg
|
||||
if (!isValidRadius(radius))
|
||||
throw new IllegalArgumentException("Invalid radius");
|
||||
|
||||
setRadius(radius);
|
||||
this.radius = radius;
|
||||
this.minimumRadius = minimumRadius;
|
||||
this.points = this.maxPoints;
|
||||
|
||||
@@ -141,11 +147,15 @@ public class Worm {
|
||||
*/
|
||||
private void setLocation(Tuple<Double, Double> location) throws IllegalArgumentException {
|
||||
|
||||
if (location == null || Double.isNaN(location.item1) || Double.isNaN(location.item2))
|
||||
if (!isValidLocation(location))
|
||||
throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
private boolean isValidLocation(Tuple<Double, Double>location) {
|
||||
return location != null && !Double.isNaN(location.item1) && !Double.isNaN(location.item2);
|
||||
}
|
||||
|
||||
//===================================================================================
|
||||
// endregion
|
||||
|
||||
@@ -209,13 +219,17 @@ public class Worm {
|
||||
* |! isValidRadius(radius)
|
||||
*/
|
||||
public void setRadius(double radius) throws IllegalArgumentException {
|
||||
if (radius < this.minimumRadius || Double.isNaN(radius))
|
||||
if (!isValidRadius(radius))
|
||||
throw new IllegalArgumentException("Invalid radius");
|
||||
|
||||
this.radius = radius;
|
||||
setMass(radius);
|
||||
}
|
||||
|
||||
private boolean isValidRadius(double radius) {
|
||||
return radius >= this.minimumRadius && !Double.isNaN(radius);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the minimum radius the worm can have
|
||||
* the minimum radius of the worm expresses the minimum length
|
||||
@@ -225,7 +239,7 @@ public class Worm {
|
||||
return this.minimumRadius;
|
||||
}
|
||||
|
||||
private boolean isValidMininmumRadius(double radius) {
|
||||
private boolean isValidMinimumRadius(double radius) {
|
||||
return !Double.isNaN(radius) && radius > 0;
|
||||
}
|
||||
|
||||
@@ -249,7 +263,7 @@ public class Worm {
|
||||
private void setMass(double radius) {
|
||||
|
||||
final int rho = 1062;
|
||||
double mass = rho * (4 / 3 * Math.PI * Math.pow(radius, 3));
|
||||
double mass = rho * (4 / 3 * PI * pow(radius, 3));
|
||||
|
||||
this.mass = mass;
|
||||
setMaxPoints(mass);
|
||||
@@ -276,7 +290,7 @@ public class Worm {
|
||||
this.points = points;
|
||||
}
|
||||
private void setMaxPoints(double maxPoints) {
|
||||
this.maxPoints = (int) Math.ceil(maxPoints);
|
||||
this.maxPoints = (int) ceil(maxPoints);
|
||||
setPoints(this.points);
|
||||
}
|
||||
private void subtractPoints (int value) {
|
||||
@@ -285,7 +299,7 @@ public class Worm {
|
||||
}
|
||||
private void subtractPoints (double angle) {
|
||||
|
||||
setPoints(this.points - (int) Math.ceil(Math.toDegrees(angle) / 6));
|
||||
setPoints(this.points - (int) ceil(toDegrees(angle) / 6));
|
||||
}
|
||||
|
||||
//===================================================================================
|
||||
@@ -392,12 +406,12 @@ public class Worm {
|
||||
if (numberSteps < 0)
|
||||
throw new IllegalArgumentException(); // TODO add decent exception msg
|
||||
|
||||
int cost = (int) Math.ceil(Math.abs(Math.cos(this.orientation)) + Math.abs(4 * Math.sin(this.orientation)));
|
||||
int cost = (int) ceil(abs(cos(this.orientation)) + abs(4 * sin(this.orientation)));
|
||||
if (cost > this.points)
|
||||
throw new IllegalArgumentException(); // TODO add decent exception msg
|
||||
|
||||
double distanceX = this.radius * Math.cos(this.orientation);
|
||||
double distanceY = this.radius * Math.sin(this.orientation);
|
||||
double distanceX = this.radius * cos(this.orientation);
|
||||
double distanceY = this.radius * sin(this.orientation);
|
||||
this.location = Tuple.create(this.location.item1 + numberSteps * distanceX,
|
||||
this.location.item2 + numberSteps * distanceY);
|
||||
subtractPoints(cost);
|
||||
@@ -427,9 +441,9 @@ public class Worm {
|
||||
* |substractPoints(angle)
|
||||
*/
|
||||
public void turn(double angle) {
|
||||
assert 0 <= angle && angle < (2 * Math.PI);
|
||||
assert 0 <= angle && angle < (2 * PI);
|
||||
|
||||
setOrientation((this.orientation + angle) % (2 * Math.PI));
|
||||
setOrientation((this.orientation + angle) % (2 * PI));
|
||||
subtractPoints(angle);
|
||||
}
|
||||
|
||||
@@ -482,7 +496,7 @@ public class Worm {
|
||||
* |result == this.points > 0 && this.orientation < Math.PI
|
||||
*/
|
||||
private boolean canJump() {
|
||||
return this.points > 0 && this.orientation < Math.PI;
|
||||
return this.points > 0 && this.orientation < PI;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -494,7 +508,7 @@ public class Worm {
|
||||
*/
|
||||
public double jumpTime() {
|
||||
|
||||
return jumpDistance(this.jumpVelocity) / (this.jumpVelocity * Math.cos(this.orientation));
|
||||
return jumpDistance(this.jumpVelocity) / (this.jumpVelocity * cos(this.orientation));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -507,8 +521,8 @@ public class Worm {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
return Tuple.create(oldLocation.item1 + this.jumpVelocity * Math.cos(this.orientation) * deltaTime,
|
||||
oldLocation.item2 + this.jumpVelocity * Math.sin(this.orientation) * deltaTime - 0.5 * G * Math.pow(deltaTime, 2));
|
||||
return Tuple.create(oldLocation.item1 + this.jumpVelocity * cos(this.orientation) * deltaTime,
|
||||
oldLocation.item2 + this.jumpVelocity * sin(this.orientation) * deltaTime - 0.5 * G * pow(deltaTime, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -521,7 +535,7 @@ public class Worm {
|
||||
* |(Math.pow(v, 2) * Math.sin(2 * this.orientation)) / this.G
|
||||
*/
|
||||
private double jumpDistance(double v) {
|
||||
return (Math.pow(v, 2) * Math.sin(2 * this.orientation)) / this.G;
|
||||
return (pow(v, 2) * sin(2 * this.orientation)) / this.G;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -536,7 +550,7 @@ public class Worm {
|
||||
private double jumpVelocity() {
|
||||
|
||||
double force = 5 * this.points + this.mass * G;
|
||||
return (force / this.mass) * FORCE_TIME;
|
||||
return force / this.mass * FORCE_TIME;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user