diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 2422b12..26660a2 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -487,6 +487,8 @@ public class Worm extends GameObject { * the absolute value of the distance multiplied with the cosinus of the new direction plus the absolute * value of 4 multiplied with the distance and the sinus of the new direction. * |new.getActionPoints() == old.getActionPoints() - (abs(distance * cos(new.getOrientation()) + abs(4 * distance * sin(new.getOrientation()))) + * @post Let the worm eat if necessary. + * |checkEat() * * @throws IllegalArgumentException * The cost of the step is bigger then the worms current action points. @@ -608,7 +610,11 @@ public class Worm extends GameObject { * The worms who has clashed with the basicworm. * * @post All the worms that clashed their hitpoints are reduced. There is a random integer between - * 1 and 10 (= N). The lose of the smalles worm is equal to N divided by the orientation o + * 1 and 10 (= N). The lose of the smalles worm is equal to N divided by the orientation of the largest + * worm that is divided by the orientation of the smallest worm plus the orientatie of the largest one. + * |new.getHitPoints() == old.getHitPoints - (N/(largest.getOrientation()/(smallest.getOrientation() + largest.getOrientation())) + * @post The lose of the largest worm is N minus the lose of the smallest one. + * |new.getHitPoints() == old.getHitPoints - loseSmallest */ public void collision(Worm basicWorm, Worm... worm) { Worm largestWorm; @@ -638,18 +644,18 @@ public class Worm extends GameObject { //=================================================================================== /** - * turns the worm with the given angle + * Turns the worm with the given angle. * * @param angle - * the angle that must be added to the orientation - * @pre the angle to add must be between 0 and 2pi (including 0) + * The angle that must be added to the orientation. + * @pre The angle to add must be between 0 and 2pi (including 0). * |0 <= angle < 2pi - * @post the new orientation is the old orientation plus the given angle + * @post The new orientation is the old orientation plus the given angle. * |new.getOrientation() = this.getOrientation() + angle - * @post the resulting angle (= the new orientation) must be between 0 and 2pi (including 0) + * @post The resulting angle (= the new orientation) must be between 0 and 2pi (including 0). * |0 <= new.getOrientation() < 2pi - * @post current actionPoints of action actionPoints should be reduced, for this there is another - * method with as parameter the given angle + * @post Current actionPoints of action actionPoints should be reduced, for this there is another + * method with as parameter the given angle. * |substractActionPoints(angle) */ public void turn(double angle) { @@ -659,12 +665,12 @@ public class Worm extends GameObject { } /** - *return a boolean reflecting whether the worm can accept the given angle to turn + * Return a boolean reflecting whether the worm can accept the given angle to turn. * * @param angle - * the angle to be checked - * @return true if and only if the given angle is between 0 (including) and 2pi, - * the angle is a number and the current number of action points is high enough to turn + * The angle to be checked. + * @return True if and only if the given angle is between 0 (including) and 2pi, + * the angle is a number and the current number of action points is high enough to turn. * |result == ( (0 <= angle) && (angle < (2 * PI)) && | (!Double.isNaN(angle)) && | (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) ) @@ -684,23 +690,33 @@ public class Worm extends GameObject { //=================================================================================== /** - * this constant contains the gravity + * This constant contains the gravity. * */ public static final double G = 5.0; /** - * this constant contains the duration of the force + * This constant contains the duration of the force. */ public static final double FORCE_TIME = 0.5; /** - * let the worm jump + * Let the worm jump. * - * @post the worm jumps to his new place. The new place is the x-coordinate plus the jump distance - * with the jump velocity - * |setLocation(Coordinate.create(getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY())) + * @post the new x coordinate is equal to the old x coordinate plus the jump velocity multiplied with the + * jump time multiplied with the cosinus of the orientation + * |new.getLocation().getX() = old.getLocation.getX() + jumpVelocity * jumpTime * cos(getOrientation()) + * @post the new y coordinate is equal to the old y coordinate plus the jump velocity multiplied with the + * jump time multiplied with the sinus of the orientation minus the constant of gravity multiplied with + * the square of the jump time divided with 2. + * |new.getLocation().getY() = old.getLocation().getY() + jumpVelocity * jumpTime * sin(getOrientation()) - (G * jumpTime^2) / 2.0 * @post the current action actionPoints should be 0 after a jump * |setActionPoints(0) + * @post If the new location is no valid location, the worm has to be terminated. + * |if (! isValidLocation(new.getLocation()): + * | terminate(); + * @post Let the worm eat if necessary. + * |checkEat() + * * @throws IllegalStateException * if the current action actionPoints is equal to 0 or the orientation is more then * pi the worm can not jump @@ -729,13 +745,13 @@ public class Worm extends GameObject { } /** - * Return the time the worm will jump + * Return the time the worm will jump. * - * @return the time the worm will jump. The distance divided by the velocity multiplied - * with the cosinus of the orientation + * @return The time the worm will jump. The distance divided by the velocity multiplied + * with the cosinus of the orientation. * |jumpDistance(this.jumpVelocity) / (this.jumpVelocity * Math.cos(this.orientation)) * @throws IllegalStateException - * Orientation is bigger then pi or action points is equal to 0 + * Orientation is bigger then pi or action points is equal to 0. * |getOrientation() >= PI || getActionPoints() == 0 */ public double jumpTime() { @@ -744,17 +760,18 @@ public class Worm extends GameObject { } /** + * Gives the location after a jump. * * @param deltaTime - * the total time the worm should jump + * The total time the worm should jump. * @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 * 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. * |Coordinate.create(getLocation().getX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime, |getLocation().getY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)) * @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) */ public Coordinate jumpStep(double deltaTime) { @@ -767,7 +784,23 @@ public class Worm extends GameObject { getLocation().getY() + velocity * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)); } - private double calcJumpTime(){ + /** + * Gives the time the worm should jump. + * + * @return If the furthest location for the jump is founded the time is founded. Every step takes + * 0.05 seconds. The furthest location is where the world is passable. + * |v = jumpVelocity; t = 0; a = getoriantation() + * |while (x = loc.getX() + v * t * cos(a)) && (y = loc.getY() + v * t * sin(a) - (G * t * t) / 2.0) + * | t += 0.05 + * | while (! new.Loc.isPassable()) + * | t -= 0.01 + * | newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0) + * | if (! new.Loc.isValidLocation) + * | break + * | break + * |result == t + */ + private double calcJumpTime() { double v = jumpVelocity(); double t = 0; @@ -828,10 +861,10 @@ public class Worm extends GameObject { // } /** - * Return a boolean whether the worm can jump or not + * Return a boolean whether the worm can jump or not. * * @return True if and only if the action actionPoints is bigger then 0 and the orientation - * is lower then pi + * is lower then pi. * |result == getActionPoints() > 0 && getOrientation() < PI */ private boolean canJump() { @@ -839,12 +872,12 @@ public class Worm extends GameObject { } /** - * Return the distance the worm will jump + * Return the distance the worm will jump. * * @param v - * The velocity of the jump + * The velocity of the jump. * @return The distance the worm will jump. The distance is equal to the velocity powered by 2 multiplied - * with the sinus of 2 times the orientation en this divided with the gravity + * with the sinus of 2 times the orientation en this divided with the gravity. * |(pow(v, 2) * sin(2 * getOrientation())) / G */ private double jumpDistance(double v) { @@ -852,9 +885,14 @@ public class Worm extends GameObject { } /** - * + * Returns of the jump distance valid is. + * * @param distance - * @return + * The distance of the jump. + * + * @return True if and only if the absolute value of the distance bigger or equal is + * to the radius. + * |result == (abs(distance) >= getRadius()) */ public boolean canHaveAsJumpDistance(double distance) { @@ -862,11 +900,11 @@ public class Worm extends GameObject { } /** - * Return the velocity of the jump + * Return the velocity of the jump. * - * @return the velocity of the jump. The force of the jump is equal to 5 multiplied with the + * @return The velocity of the jump. The force of the jump is equal to 5 multiplied with the * current action actionPoints plus the mass multiplied with the gravity. Therefrom the velocity - * is equal to the force divided with the mass, multiplied with the time te force takes + * is equal to the force divided with the mass, multiplied with the time te force takes. * |force = 5 * getActionPoints() + getMass() * G * |velocity = (force / getMass()) * FORCE_TIME */ @@ -875,13 +913,22 @@ public class Worm extends GameObject { double force = 5 * getActionPoints() + getMass() * G; return force / getMass() * FORCE_TIME; } - + /** - * + * Decreases the hit points of the attacked worm (chosen by a coin). + * * @param newLocation + * The new location of the jumped worm. * @param oldLocation + * The old location of the jumped worm. * @param worm1 + * The jumped worm. * @param worm2 + * The worms who stand on the same place (or parial) of the jumped worm. + * + * @post the attacked worms hit points are decreased with random value between 1 and 10 multiplied + * with the rate of the radius of the attacker and the radius of the attacked worm. + * |new.attackedWorm.getHitPoints() == old.attackedWorm.getHitPoints() - 10 * ((attacker.getRadius())/(attackedWorm.getRadius())) */ public void fight(Coordinate newLocation, Coordinate oldLocation, Worm worm1, Worm... worm2) { if (! worm1.isTerminated() && newLocation != oldLocation) { @@ -913,8 +960,8 @@ public class Worm extends GameObject { //=================================================================================== /** - * Return the current hit points of the worm - * the hit points identifies the strength of the worm + * Return the current hit points of the worm. + * The hit points identifies the strength of the worm. */ @Basic @Raw public long getHitPoints() { @@ -922,11 +969,11 @@ public class Worm extends GameObject { } /** - * set the current hit points of the worm to the given points + * Set the current hit points of the worm to the given points. * * @param hitPoints - * the new points for the worm - * @post if the given points are equal or lower then zero, the worm should be dead. + * The new points for the worm. + * @post If the given points are equal or lower then zero, the worm should be dead. * Otherwise the current hit points are set to the given points. * |if (hitpoints <= 0) * | terminate(); @@ -941,22 +988,30 @@ public class Worm extends GameObject { } /** - * this variable contains the current hitpoints of the worm + * This variable contains the current hitpoints of the worm. */ private long hitPoints; /** - * increment the hit points with the given value + * Decrease the hit points with the given value. * * @param value - * the value that should be substracted - * @post the current hit points should be substracted with the given value. + * The value that should be decreased. + * @post The current hit points should be decreased with the given value. * |new.getHitPoints() == old.getHitPoints() - value */ public void decreaseHitPoints(long value) { setHitPoints(getHitPoints() - value); } + /** + * Increment the hit points with the given value. + * + * @param value + * The value that schould be added. + * @post The current hit points should be increaseed with the given value. + * |new.getHitPoints() == old.getHitPoints() + value + */ public void incrementHitPoints(long value) { setHitPoints(getHitPoints() + value); } @@ -967,6 +1022,20 @@ public class Worm extends GameObject { // region falling //=================================================================================== + /** + * Set the location to the first position just above impassable domain + * + * @post The location is set to the first location beneath the old location just above impassable domain. + * If the worm is the partial out of the world, the worm should be terminated. + * |while canFall() (y -= 0.01): + * | new.getLocation() == (oldLocation.getX(), (y * 100.0) / 100.0) + * | if (y - radius < 0): + * | terminate() + * @post The hit points of the worm schould be decreased with 3 times the displacement of the worm. + * |new.getHitPoints() == old.getHitPoints() - (3 * (oldLocation.getY() - getLocation().getY())) + * @post The worm has to eat when that is needed. + * |checkEat(); + */ public void fall() { double height = getWorld().getHeight() - getRadius(); Coordinate oldLocation = getLocation(); @@ -993,11 +1062,31 @@ public class Worm extends GameObject { checkEat(); } + /** + * Returns of the worm can fall or not. + * + * @return True if and only if the domain just beneath the worm is passable. + * |result == ! getWorld().isAdjacent(center, getRadius()) + */ public boolean canFall() { double[] center = {getLocation().getX(), getLocation().getY()}; return ! getWorld().isAdjacent(center, getRadius()); } + /** + * Changes the hit points of the clashing worms. + * + * @param fallingWorm + * The worm who has fallen. + * @param worm + * The worm on which the falling worm has fallen. + * + * @post the falling worm his hit points are incremented with half of the hit points of the + * stationary worm. + * |new.getHitPoints() == old.getHitPoints() + stationaryWorm.getHitPoints()/2 + * @post the stationary worm his hit points are decreased with half of his hit points. + * |new.getHitPoints() == old.getHitPoints() - old.getHitPoints()/2 + */ public void collisionFall(Worm fallingWorm, Worm... worm) { for (Worm stationaryWorm: worm) { long value = stationaryWorm.getHitPoints()/2; @@ -1012,20 +1101,55 @@ public class Worm extends GameObject { // region team //=================================================================================== - public Team getTeam() { + /** + * Return the current team of the worm. + * The team identifies the partners of the worm. + */ + public Team getTeam() { return this.team; } + /** + * Set the current team of the worm to the given team. + * + * @param team + * The new team for the worm. + * @post The current team of the worm is set to the given team. + * |new.getTeam() == team + */ public void setTeam(Team team) { this.team = team; } + /** + * This variable contains the current team of the worm. + */ private Team team; // =================================================================================== // endregion - + /** + * The worm eats food and grows. + * + * @param food + * The food the worm has eaten. + * + * @post The eaten food is terminated. + * |food.terminate(); + * @post The worm's radius is increased with 10%. + * |new.getRadius() = 0,10 * old.getRadius() + old.getRadius() + * @post The worm has moved so that the worm stands full on a valid location (passable). + * |new.getLocation() == isPassable(Location, radius) + * @post If there is no valid new location, the worm has explode. + * |for each new location; (new.getLocation() != isPassable): + * | terminate() + * @post If the circular area of the worm is not long full in the world, the worm leaves the world. + * |if (new.getLocation != isValidLocation()): + * | terminate() + * @post Let the worm eat if necessary. + * |checkEat() + */ public void eat(Food food) { food.terminate(); @@ -1080,8 +1204,15 @@ public class Worm extends GameObject { } } setLocation(location); + checkEat(); } + /** + * If the worm can eat food (depends on his location), the worms eats the food. + * + * @post The worm has eaten the food. + * |eat(food) + */ public void checkEat() { World world = getWorld();