From d8981def95f55b0b6101aa56d2c5bd04cdd0989a Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Fri, 9 Mar 2018 23:46:57 +0100 Subject: [PATCH] lots of changes --- OGP1718-Worms/src/worms/model/Worm.java | 90 ++++++++++--------------- 1 file changed, 37 insertions(+), 53 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 9e8cdb7..0fe4b2d 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -53,7 +53,7 @@ public class Worm { if (!isValidMinRadius(minRadius)) throw new IllegalArgumentException("Invalid min radius"); // TODO add decent exception msg - if (!isValidRadius(radius)) + if (!canHaveAsMinRadius(radius)) throw new IllegalArgumentException("Invalid radius"); setRadius(radius); @@ -199,10 +199,10 @@ public class Worm { * |new.getRadius() == radius * @throws IllegalArgumentException * the given radius is not a valid radius for any worm - * |! isValidRadius(radius) + * |! canHaveAsMinRadius(radius) */ public void setRadius(double radius) throws IllegalArgumentException { - if (!isValidRadius(radius)) + if (!canHaveAsMinRadius(radius)) throw new IllegalArgumentException("Invalid radius"); this.radius = radius; @@ -240,8 +240,8 @@ public class Worm { * (or equal) and the radius is a number * |result == (radius >= this.minRadius && !Double.isNaN(radius)) */ - private boolean isValidRadius(double radius) { - return radius >= this.minRadius && !Double.isNaN(radius); + private boolean canHaveAsMinRadius(double radius) { + return radius >= getMinRadius() && !Double.isNaN(radius); } @@ -316,16 +316,21 @@ public class Worm { * |this.actionPoints = actionPoints */ public void setActionPoints(long actionPoints) { - this.actionPoints = toValidActionPoints(actionPoints); + if (actionPoints > getMaxActionPoints()) + actionPoints = getMaxActionPoints(); + else if (actionPoints < 0) + actionPoints = 0; + + this.actionPoints = actionPoints; } /** * - * @param delta + * @param delta ... */ public void decreaseActionPoints (long delta) { - this.actionPoints = toValidActionPoints(this.actionPoints - delta); + setActionPoints(getActionPoints() - delta); } /** @@ -338,38 +343,19 @@ public class Worm { */ private long maxActionPoints; - private void setMaxActionPoints(double maxActionPoints) { - this.maxActionPoints = round(maxActionPoints); - setActionPoints(this.actionPoints); - } - - /** - * - * @param actionPoints - * @return - */ - private long toValidActionPoints(long actionPoints) { - if (actionPoints > this.maxActionPoints) - actionPoints = this.maxActionPoints; - else if (actionPoints < 0) - actionPoints = 0; - - return actionPoints; - } - /** * set the maximum of points to the given maximum of points - * - * @param maxPoints + * + * @param maxActionPoints * the new maximum of points for the worm * @post the new maximum points is set to the given maximum points (as an integer) * |this.maxPoints = (int) ceil(maxPoints) * @post when the maximum points change, the current points should change too * |setPoints(this.points) */ - private void setMaxPoints(double maxPoints) { - this.maxActionPoints = (int) round(maxPoints); - setActionPoints(this.actionPoints); + private void setMaxActionPoints(double maxActionPoints) { + this.maxActionPoints = round(maxActionPoints); + setActionPoints(getActionPoints()); } /** @@ -382,7 +368,7 @@ public class Worm { */ private void subtractActionPoints (long value) { - setActionPoints(this.actionPoints - value); + setActionPoints(getActionPoints() - value); } /** @@ -396,7 +382,7 @@ public class Worm { */ private void subtractActionPoints (double angle) { - setActionPoints(this.actionPoints - (long) ceil(toDegrees(angle) / 6)); + setActionPoints(getActionPoints() - (long) ceil(toDegrees(angle) / 6)); } //=================================================================================== @@ -510,15 +496,15 @@ public class Worm { if (numberSteps < 0) throw new IllegalArgumentException(); // TODO add decent exception msg - long cost = (long) ceil(abs(cos(this.orientation)) + abs(4 * sin(this.orientation))); + long cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))); - if (cost > this.actionPoints) + if (cost > getActionPoints()) throw new IllegalArgumentException(); // TODO add decent exception msg - 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); + double distanceX = this.radius * cos(getOrientation()); + double distanceY = this.radius * sin(getOrientation()); + setLocation(Tuple.create(getLocation().item1 + numberSteps * distanceX, + getLocation().item2 + numberSteps * distanceY)); subtractActionPoints(cost); } @@ -558,7 +544,7 @@ public class Worm { private boolean canTurn(double angle) { return 0 <= angle && angle < (2 * PI) && !Double.isNaN(angle) && - this.actionPoints - (long) ceil(toDegrees(angle) / 6) >= 0; + getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0; } //=================================================================================== @@ -594,8 +580,8 @@ public class Worm { if (!canJump()) throw new IllegalStateException(); - this.location = Tuple.create(location.item1 + jumpDistance(this.jumpVelocity()), location.item2); - this.actionPoints = 0; + setLocation(Tuple.create(getLocation().item1 + jumpDistance(this.jumpVelocity()), getLocation().item2)); + setActionPoints(0); } /** @@ -607,10 +593,10 @@ public class Worm { */ public double jumpTime() { - if (this.orientation >= PI || this.actionPoints == 0) + if (getOrientation() >= PI || getActionPoints() == 0) throw new IllegalStateException(); - return (jumpDistance(jumpVelocity()) / (jumpVelocity() * cos(this.orientation))); + return (jumpDistance(jumpVelocity()) / (jumpVelocity() * cos(getOrientation()))); } /** @@ -622,8 +608,8 @@ public class Worm { if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) throw new IllegalArgumentException(); - return Tuple.create(this.location.item1 + this.jumpVelocity() * cos(this.orientation) * deltaTime, - this.location.item2 + this.jumpVelocity() * sin(this.orientation) * deltaTime - 0.5 * G * pow(deltaTime, 2)); + return Tuple.create(getLocation().item1 + this.jumpVelocity() * cos(getOrientation()) * deltaTime, + getLocation().item2 + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)); } /** @@ -634,7 +620,7 @@ public class Worm { * |result == this.actionPoints > 0 && this.orientation < Math.PI */ private boolean canJump() { - return this.actionPoints > 0 && this.orientation < PI; + return getActionPoints() > 0 && getOrientation() < PI; } /** @@ -647,7 +633,7 @@ public class Worm { * |(Math.pow(v, 2) * Math.sin(2 * this.orientation)) / this.G */ private double jumpDistance(double v) { - return (pow(v, 2) * sin(2 * this.orientation)) / G; + return (pow(v, 2) * sin(2 * getOrientation())) / G; } /** @@ -661,13 +647,11 @@ public class Worm { */ private double jumpVelocity() { - double force = 5 * this.actionPoints + this.mass * G; - return force / this.mass * FORCE_TIME; + double force = 5 * getActionPoints() + getMass() * G; + return force / getMass() * FORCE_TIME; } //=================================================================================== // endregion - - }