Jump methods
This commit is contained in:
@@ -665,9 +665,26 @@ public class Worm extends GameObject {
|
||||
|
||||
if (!canJump())
|
||||
throw new IllegalStateException();
|
||||
|
||||
setLocation(Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY()));
|
||||
setActionPoints(0);
|
||||
|
||||
Coordinate newLocation = Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY());
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -733,6 +750,18 @@ public class Worm extends GameObject {
|
||||
private double jumpDistance(double v) {
|
||||
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
|
||||
@@ -749,25 +778,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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user