diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 144ef63..a736834 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -239,7 +239,7 @@ public class Worm extends GameObject { /** * Check whether the given radius is a valid radius for the worm - * + * * @param radius * the radius to check * @return True if and only if the radius is bigger then the minimum radius @@ -653,12 +653,13 @@ public class Worm extends GameObject { if (!canJump()) throw new IllegalStateException(); + double distance = jumpDistance(this.jumpVelocity()); + Coordinate newLocation = Coordinate.create( getLocation().getX() + distance, getLocation().getY()); - setLocation(Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY())); - setActionPoints(0); - - if (!getWorld().isAdjacent(getLocation(), getRadius())) { - fall(); + if (getWorld().isAdjacent(newLocation, getRadius()) && canHaveAsJumpDistance(distance)) { + if (!isValidLocation(newLocation)) terminate(); + setLocation(newLocation); + setActionPoints(0); } } @@ -726,6 +727,16 @@ public class Worm extends GameObject { return (pow(v, 2) * sin(2 * getOrientation())) / G; } + /** + * + * @param distance + * @return + */ + public boolean canHaveAsJumpDistance(double distance) { + + return Math.abs(distance) >= getRadius(); + } + /** * Return the velocity of the jump * @@ -741,25 +752,33 @@ public class Worm extends GameObject { return force / getMass() * FORCE_TIME; } - public void fight(Worm worm1, Worm... worm2) { - for (Worm wormB: worm2) { - Worm attackedWorm; - Worm attacker; - Random rand = new Random(); - int coin = rand.nextInt(2); - if (coin == 1) { - attackedWorm = worm1; - attacker = wormB; + /** + * + * @param newLocation + * @param oldLocation + * @param worm1 + * @param worm2 + */ + public void fight(Coordinate newLocation, Coordinate oldLocation, Worm worm1, Worm... worm2) { + if (! worm1.isTerminated() && newLocation != oldLocation) { + for (Worm wormB: worm2) { + Worm attackedWorm; + Worm attacker; + Random rand = new Random(); + int coin = rand.nextInt(2); + if (coin == 1) { + attackedWorm = worm1; + attacker = wormB; + } + else { + attackedWorm = wormB; + attacker = worm1; + } + double N = 10 * ceil((attacker.getRadius())/(attackedWorm.getRadius())); + long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; + attackedWorm.decreaseHitPoints(loseAttackedWorm); } - else { - attackedWorm = wormB; - attacker = worm1; - } - double N = 10 * ceil((attacker.getRadius())/(attackedWorm.getRadius())); - long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; - attackedWorm.incrementHitPoints(loseAttackedWorm); } - } @@ -792,7 +811,7 @@ public class Worm extends GameObject { */ @Raw private void setHitPoints(long hitPoints) { - if (hitPoints < 0) + if (hitPoints <= 0) terminate(); this.hitPoints = hitPoints; } @@ -831,7 +850,7 @@ public class Worm extends GameObject { for (double y = oldLocation.getY(); y <= heigth; y = y - 0.01) { Coordinate newLoc = Coordinate.create(oldLocation.getX(), y); - if (y - radius < 0) { + if (!isValidLocation(newLoc)) { terminate(); break; } @@ -841,14 +860,6 @@ public class Worm extends GameObject { } else { break; } -// for (double i = y; i >= y - 1; i = i - 0.1) { -// double[] newLocation = {oldLocation.getX(), i}; -// if (! getWorld().isPassable(newLocation, getRadius())) { -// setLocation(newLocation); -// endY = i; -// break; -// } -// } } }