Jump methods

This commit is contained in:
Leen Dereu
2018-04-13 20:22:15 +02:00
parent 5171f36916
commit 1fff8ee8b9

View File

@@ -666,8 +666,25 @@ public class Worm extends GameObject {
if (!canJump()) if (!canJump())
throw new IllegalStateException(); throw new IllegalStateException();
setLocation(Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY())); Coordinate newLocation = Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY());
setActionPoints(0);
isNewLocationInWorld(newLocation);
if (! getWorld().isPassable(newLocation) && validJumpDistance(jumpDistance(jumpVelocity())) && ! isTerminated()) {
setLocation(newLocation);
setActionPoints(0);
}
}
/**
*
* @param location
*/
public void isNewLocationInWorld (Coordinate location) {
if (location.getX() - radius < 0 || location.getX() + radius > getWorld().getWidth() ||
location.getY() - radius < 0 || location.getY() + radius > getWorld().getHeight()) {
terminate();
}
} }
/** /**
@@ -734,6 +751,18 @@ 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 validJumpDistance(double distance) {
if (distance >= getRadius()) {
return true;
}
return false;
}
/** /**
* Return the velocity of the jump * Return the velocity of the jump
* *
@@ -749,25 +778,33 @@ public class Worm extends GameObject {
return force / getMass() * FORCE_TIME; return force / getMass() * FORCE_TIME;
} }
public void fight(Worm worm1, Worm... worm2) { /**
for (Worm wormB: worm2) { *
Worm attackedWorm; * @param newLocation
Worm attacker; * @param oldLocation
Random rand = new Random(); * @param worm1
int coin = rand.nextInt(2); * @param worm2
if (coin == 1) { */
attackedWorm = worm1; public void fight(Coordinate newLocation, Coordinate oldLocation, Worm worm1, Worm... worm2) {
attacker = wormB; 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);
} }
} }