This commit is contained in:
2018-04-13 21:41:31 +02:00

View File

@@ -653,12 +653,13 @@ public class Worm extends GameObject {
if (!canJump()) if (!canJump())
throw new IllegalStateException(); 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())); if (getWorld().isAdjacent(newLocation, getRadius()) && canHaveAsJumpDistance(distance)) {
if (!isValidLocation(newLocation)) terminate();
setLocation(newLocation);
setActionPoints(0); setActionPoints(0);
if (!getWorld().isAdjacent(getLocation(), getRadius())) {
fall();
} }
} }
@@ -726,6 +727,16 @@ public class Worm extends GameObject {
return (pow(v, 2) * sin(2 * getOrientation())) / G; 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 * Return the velocity of the jump
* *
@@ -741,7 +752,15 @@ public class Worm extends GameObject {
return force / getMass() * FORCE_TIME; return force / getMass() * FORCE_TIME;
} }
public void fight(Worm worm1, Worm... worm2) { /**
*
* @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) { for (Worm wormB: worm2) {
Worm attackedWorm; Worm attackedWorm;
Worm attacker; Worm attacker;
@@ -757,9 +776,9 @@ public class Worm extends GameObject {
} }
double N = 10 * ceil((attacker.getRadius())/(attackedWorm.getRadius())); double N = 10 * ceil((attacker.getRadius())/(attackedWorm.getRadius()));
long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1;
attackedWorm.incrementHitPoints(loseAttackedWorm); attackedWorm.decreaseHitPoints(loseAttackedWorm);
}
} }
} }
@@ -792,7 +811,7 @@ public class Worm extends GameObject {
*/ */
@Raw @Raw
private void setHitPoints(long hitPoints) { private void setHitPoints(long hitPoints) {
if (hitPoints < 0) if (hitPoints <= 0)
terminate(); terminate();
this.hitPoints = hitPoints; this.hitPoints = hitPoints;
} }
@@ -831,7 +850,7 @@ public class Worm extends GameObject {
for (double y = oldLocation.getY(); y <= heigth; y = y - 0.01) { for (double y = oldLocation.getY(); y <= heigth; y = y - 0.01) {
Coordinate newLoc = Coordinate.create(oldLocation.getX(), y); Coordinate newLoc = Coordinate.create(oldLocation.getX(), y);
if (y - radius < 0) { if (!isValidLocation(newLoc)) {
terminate(); terminate();
break; break;
} }
@@ -841,14 +860,6 @@ public class Worm extends GameObject {
} else { } else {
break; 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;
// }
// }
} }
} }