This commit is contained in:
Leen Dereu
2018-05-23 13:19:51 +02:00
4 changed files with 132 additions and 224 deletions

View File

@@ -1,6 +1,5 @@
package worms.facade; package worms.facade;
import be.kuleuven.cs.som.annotate.Model;
import worms.internal.gui.game.IActionHandler; import worms.internal.gui.game.IActionHandler;
import worms.model.*; import worms.model.*;
import worms.model.Food; import worms.model.Food;
@@ -832,11 +831,11 @@ public class Facade implements IFacade {
* @param delta * @param delta
* the value that schould be decreased * the value that schould be decreased
* @post the action points are decreased with delta * @post the action points are decreased with delta
* |worm.decreaseActionPoints((int) delta) * |worm.decrementActionPoints((int) delta)
*/ */
@Override @Override
public void decreaseNbActionPoints(Worm worm, long delta) throws ModelException { public void decreaseNbActionPoints(Worm worm, long delta) throws ModelException {
worm.decreaseActionPoints((int) delta); worm.decrementActionPoints((int) delta);
} }
/** /**

View File

@@ -111,8 +111,8 @@ public abstract class GameObject {
*/ */
public void terminate() { public void terminate() {
this.terminated = true; this.terminated = true;
if (world != null) { if (this.world != null) {
getWorld().remove(this); this.world.remove(this);
} }
} }

View File

@@ -567,7 +567,7 @@ public class World {
smWorm.incrementActionPoints(smWorm.getActionPoints() + lgHitPoints); smWorm.incrementActionPoints(smWorm.getActionPoints() + lgHitPoints);
} else { } else {
smWorm.incrementActionPoints(5); smWorm.incrementActionPoints(5);
lgWorm.decreaseActionPoints(5); lgWorm.decrementActionPoints(5);
} }
} }
} }

View File

@@ -217,7 +217,7 @@ public class Worm extends GameObject {
this.radius = radius; this.radius = radius;
final double rho = 1062; final double rho = 1062;
setMass(radius, rho); setMass(radius, rho);
setMaxActionPoints(getMass()); setMaxActionPoints(this.mass);
} }
/** /**
@@ -246,23 +246,13 @@ public class Worm extends GameObject {
*/ */
@Raw @Raw
private boolean canHaveAsRadius(double radius) { private boolean canHaveAsRadius(double radius) {
if (getWorld() != null) { if (this.world != null) {
return !Double.isNaN(radius) && radius >= getMinRadius() && !Double.isInfinite(radius) && getWorld().isPassable(getLocation(), radius); return !Double.isNaN(radius) && radius >= this.minRadius && !Double.isInfinite(radius) && this.world.isPassable(this.location, radius);
} else { } else {
return !Double.isNaN(radius) && radius >= getMinRadius() && !Double.isInfinite(radius); return !Double.isNaN(radius) && radius >= this.minRadius && !Double.isInfinite(radius);
} }
} }
/**
* Return the mass of the worm.
* The mass of the worm expresses the weight of the worm.
*/
@Basic
@Raw
public double getMass() {
return this.mass;
}
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -306,14 +296,21 @@ public class Worm extends GameObject {
*/ */
@Raw @Raw
public void setActionPoints(long actionPoints) { public void setActionPoints(long actionPoints) {
if (actionPoints > getMaxActionPoints()) if (actionPoints > this.maxActionPoints)
actionPoints = getMaxActionPoints(); actionPoints = this.maxActionPoints;
else if (actionPoints < 0) else if (actionPoints < 0)
actionPoints = 0; actionPoints = 0;
this.actionPoints = actionPoints; this.actionPoints = actionPoints;
} }
// TODO add documentatie
@Raw
public void incrementActionPoints(long delta) {
setActionPoints(this.actionPoints + delta);
}
/** /**
* Substract the current points of the worm. * Substract the current points of the worm.
* *
@@ -321,24 +318,31 @@ public class Worm extends GameObject {
* @post The current points are set to the old current points minus the given value. * @post The current points are set to the old current points minus the given value.
* |setActionPoints(getActionPoints() - delta) * |setActionPoints(getActionPoints() - delta)
*/ */
public void decreaseActionPoints(long delta) { @Raw
public void decrementActionPoints(long delta) {
setActionPoints(getActionPoints() - delta); setActionPoints(this.actionPoints - delta);
} }
public void incrementActionPoints(long delta) { /**
setActionPoints(getActionPoints() + delta); * Substract the current points of the worm.
*
* @param angle The angle needed to calculate the new current points.
* @post The current points are set to the old current points minus
* the angle (in degrees) divided by 6.
* |setActionPoints(getActionPoints() - (long) ceil(toDegrees(angle) / 6))
*/
private void decreaseActionPointsByAngle(double angle) {
setActionPoints(this.actionPoints - (long) ceil(toDegrees(angle) / 6));
} }
/** /**
* This variable contains the current action points of the worm. * This variable contains the current action points of the worm.
*/ */
private long actionPoints; private long actionPoints;
/**
* This variable contains the maximum action points a worm can have.
*/
private long maxActionPoints;
/** /**
* Set the maximum of points to the given maximum of points. * Set the maximum of points to the given maximum of points.
@@ -352,33 +356,14 @@ public class Worm extends GameObject {
@Raw @Raw
private void setMaxActionPoints(double maxActionPoints) { private void setMaxActionPoints(double maxActionPoints) {
this.maxActionPoints = round(maxActionPoints); this.maxActionPoints = round(maxActionPoints);
setActionPoints(getActionPoints()); setActionPoints(this.actionPoints);
} }
/** /**
* Substract the current points of the worm. * This variable contains the maximum action points a worm can have.
*
* @param value The value which should be subtracted.
* @post The current points are set to the old current points minus the given value.
* |setActionPoints(getActionPoints() - value)
*/ */
private void subtractActionPoints(long value) { private long maxActionPoints;
setActionPoints(getActionPoints() - value);
}
/**
* Substract the current points of the worm.
*
* @param angle The angle needed to calculate the new current points.
* @post The current points are set to the old current points minus
* the angle (in degrees) divided by 6.
* |setActionPoints(getActionPoints() - (long) ceil(toDegrees(angle) / 6))
*/
private void subtractActionPoints(double angle) {
setActionPoints(getActionPoints() - (long) ceil(toDegrees(angle) / 6));
}
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -477,39 +462,21 @@ public class Worm extends GameObject {
*/ */
public void move() throws IllegalArgumentException { public void move() throws IllegalArgumentException {
if (getWorld() == null || getActionPoints() == 0) throw new IllegalStateException(); if (this.world == null || this.actionPoints == 0) throw new IllegalStateException();
double newDirection = getFurthestLocationDirection(); double newDirection = getFurthestLocationDirection();
Coordinate newLocation = getFurthestLocationInDirection(newDirection, this.getRadius()); Coordinate newLocation = getFurthestLocationInDirection(newDirection, this.radius);
double distance = getDistance(this.getLocation(), newLocation); double distance = getDistance(this.location, newLocation);
long cost = (long) Math.ceil(abs(distance * cos(newDirection)) + abs(4 * distance * sin(newDirection))); long cost = (long) Math.ceil(abs(distance * cos(newDirection)) + abs(4 * distance * sin(newDirection)));
if (cost > getActionPoints()) if (cost > this.actionPoints)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
World world = getWorld(); collision(newLocation);
if (world != null && world.isAdjacent(newLocation, getRadius())) {
for (Worm w : world.getGameObjectsByClass(Worm.class)) {
if (w.equals(this)) continue;
Worm smallest = this;
Worm largest = w;
if (smallest.getRadius() > largest.getRadius()) {
smallest = w;
largest = this;
}
double smRadius = smallest.getRadius();
double lgRadius = largest.getRadius();
int nb = ThreadLocalRandom.current().nextInt(1, 10);
long smallHp = round(nb / (lgRadius / (smRadius + lgRadius)));
smallest.decreaseHitPoints(smallHp);
largest.decreaseHitPoints((long) nb - smallHp);
}
}
setLocation(newLocation); setLocation(newLocation);
subtractActionPoints(cost); decrementActionPoints(cost);
} }
/** /**
@@ -523,15 +490,15 @@ public class Worm extends GameObject {
*/ */
public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) { public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) {
Coordinate currentLocation = getLocation(); Coordinate currentLocation = this.location;
double radius = getRadius(); double radius = this.radius;
World world = getWorld(); World world = this.world;
if (direction < 0 || direction > 2 * Math.PI || Double.isNaN(direction) || maxDistance < 0 || if (direction < 0 || direction > 2 * Math.PI || Double.isNaN(direction) || maxDistance < 0 ||
Double.isNaN(maxDistance) || Double.isInfinite(maxDistance)) Double.isNaN(maxDistance) || Double.isInfinite(maxDistance))
throw new IllegalArgumentException("Invalid direction/distance"); throw new IllegalArgumentException("Invalid direction/distance");
if (getWorld() == null) { if (this.world == null) {
return Coordinate.create(currentLocation.getX() + maxDistance * cos(direction), currentLocation.getY() + maxDistance * sin(direction)); return Coordinate.create(currentLocation.getX() + maxDistance * cos(direction), currentLocation.getY() + maxDistance * sin(direction));
} }
@@ -573,23 +540,25 @@ public class Worm extends GameObject {
* |result == maxDistance * |result == maxDistance
*/ */
public double getFurthestLocationDirection() { public double getFurthestLocationDirection() {
Coordinate location = getLocation();
double direction = getOrientation(); Coordinate location = this.location;
double direction = this.orientation;
double minDirection = direction - 0.7875; double minDirection = direction - 0.7875;
double maxDirection = direction + 0.7875; double maxDirection = direction + 0.7875;
double maxLocDirection = minDirection; double maxLocDirection = minDirection;
Coordinate maxLoc = location; Coordinate maxLoc = location;
for (; minDirection <= maxDirection; minDirection += 0.0175) { for (; minDirection <= maxDirection; minDirection += 0.0175) {
if (minDirection < 0) minDirection = 0.0; if (minDirection < 0) minDirection = 0.0;
Coordinate tempLoc = getFurthestLocationInDirection(minDirection, this.getRadius()); Coordinate tempLoc = getFurthestLocationInDirection(minDirection, this.radius);
if (!getWorld().isAdjacent(tempLoc, getRadius())) tempLoc = location; if (!this.world.isAdjacent(tempLoc, this.radius)) tempLoc = location;
if (getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) { if (getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) {
maxLoc = tempLoc; maxLoc = tempLoc;
maxLocDirection = minDirection; maxLocDirection = minDirection;
} }
} }
Coordinate tempLoc = getFurthestLocationInDirection(maxDirection, this.getRadius()); Coordinate tempLoc = getFurthestLocationInDirection(maxDirection, this.radius);
if (getWorld().isAdjacent(tempLoc, getRadius()) && getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) { if (this.world.isAdjacent(tempLoc, this.radius) && getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) {
maxLoc = tempLoc; maxLoc = tempLoc;
maxLocDirection = maxDirection; maxLocDirection = maxDirection;
} }
@@ -599,7 +568,7 @@ public class Worm extends GameObject {
} }
/** /**
* Gives the distance between two coordinates. * Returns the distance between two coordinates.
* *
* @param start The start coordinate of the equation. * @param start The start coordinate of the equation.
* @param end The end coordinate of the equation. * @param end The end coordinate of the equation.
@@ -614,6 +583,7 @@ public class Worm extends GameObject {
} }
/** /**
* TODO update documentatie
* The clashing worms their hit points are reduced. * The clashing worms their hit points are reduced.
* *
* @param basicWorm The worm who has moved. * @param basicWorm The worm who has moved.
@@ -625,22 +595,26 @@ public class Worm extends GameObject {
* @post The lose of the largest worm is N minus the lose of the smallest one. * @post The lose of the largest worm is N minus the lose of the smallest one.
* |new.getHitPoints() == old.getHitPoints - loseSmallest * |new.getHitPoints() == old.getHitPoints - loseSmallest
*/ */
public void collision(Worm basicWorm, Worm... worm) { public void collision(Coordinate newLocation) {
Worm largestWorm;
Worm smallestWorm; World world = this.world;
for (Worm w1 : worm) { if (world != null && world.isAdjacent(newLocation, this.radius)) {
if (w1.getMass() > basicWorm.getMass()) { for (Worm w : world.getGameObjectsByClass(Worm.class)) {
largestWorm = w1; if (w.equals(this)) continue;
smallestWorm = basicWorm;
} else { Worm smallest = this;
largestWorm = basicWorm; Worm largest = w;
smallestWorm = w1; if (smallest.getRadius() > largest.getRadius()) {
smallest = w;
largest = this;
}
double smRadius = smallest.getRadius();
double lgRadius = largest.getRadius();
int nb = ThreadLocalRandom.current().nextInt(1, 10);
long smallHp = round(nb / (lgRadius / (smRadius + lgRadius)));
smallest.decreaseHitPoints(smallHp);
largest.decreaseHitPoints((long) nb - smallHp);
} }
long total = 1 + (new Random().nextLong() * (10 - 1));
long loseSmallest = (long) (total / (largestWorm.getOrientation() / (smallestWorm.getOrientation() + largestWorm.getOrientation())));
long loseLargest = total - loseSmallest;
smallestWorm.incrementHitPoints(loseSmallest);
largestWorm.incrementHitPoints(loseLargest);
} }
} }
@@ -667,8 +641,8 @@ public class Worm extends GameObject {
*/ */
public void turn(double angle) { public void turn(double angle) {
assert canTurn(angle); assert canTurn(angle);
setOrientation(getOrientation() + angle); setOrientation(this.orientation + angle);
subtractActionPoints(abs(angle)); decreaseActionPointsByAngle(abs(angle));
} }
/** /**
@@ -682,10 +656,10 @@ public class Worm extends GameObject {
* | (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) ) * | (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) )
*/ */
protected boolean canTurn(double angle) { protected boolean canTurn(double angle) {
double currentAngle = getOrientation(); double currentAngle = this.orientation;
return 0 <= angle + currentAngle && angle + currentAngle < (2 * PI) && return 0 <= angle + currentAngle && angle + currentAngle < (2 * PI) &&
!Double.isNaN(angle) && !Double.isNaN(angle) &&
getActionPoints() - (long) Math.abs(ceil(toDegrees(angle) / 6)) >= 0; this.actionPoints - (long) Math.abs(ceil(toDegrees(angle) / 6)) >= 0;
} }
//=================================================================================== //===================================================================================
@@ -733,20 +707,21 @@ public class Worm extends GameObject {
double v = jumpVelocity(); double v = jumpVelocity();
double t = calcJumpTime(); double t = calcJumpTime();
double a = getOrientation(); double a = this.orientation;
Coordinate location = this.location;
Coordinate newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a), getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0); Coordinate newLocation = Coordinate.create(location.getX() + v * t * cos(a), location.getY() + v * t * sin(a) - (G * t * t) / 2.0);
if (!isValidLocation(newLocation)) { if (!isValidLocation(newLocation)) {
terminate(); terminate();
return; return;
} }
if (!newLocation.equals(getLocation())) { if (!newLocation.equals(location)) {
setLocation(newLocation); setLocation(newLocation);
fight(); fight();
} }
setActionPoints(0); this.actionPoints = 0;
} }
@@ -779,13 +754,13 @@ public class Worm extends GameObject {
* |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) * |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
*/ */
public Coordinate jumpStep(double deltaTime) { public Coordinate jumpStep(double deltaTime) {
if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0 || getActionPoints() == 0) if (Double.isNaN(deltaTime) || deltaTime > jumpTime() || deltaTime < 0 || this.actionPoints == 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
double velocity = this.jumpVelocity(); double velocity = jumpVelocity();
return Coordinate.create(getLocation().getX() + velocity * cos(getOrientation()) * deltaTime, return Coordinate.create(this.location.getX() + velocity * cos(this.orientation) * deltaTime,
getLocation().getY() + velocity * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)); this.location.getY() + velocity * sin(this.orientation) * deltaTime - 0.5 * G * pow(deltaTime, 2));
} }
/** /**
@@ -806,14 +781,14 @@ public class Worm extends GameObject {
*/ */
private double calcJumpTime() { private double calcJumpTime() {
World world = getWorld(); World world = this.world;
if (world == null) throw new IllegalStateException("World cannot be null"); if (world == null) throw new IllegalStateException("World cannot be null");
double v = jumpVelocity(); double v = jumpVelocity();
double t = 0; double t = 0;
double a = getOrientation(); double a = this.orientation;
Coordinate loc = getLocation(); Coordinate loc = this.location;
double radius = getRadius(); double radius = this.radius;
Coordinate newLoc; Coordinate newLoc;
while (true) { while (true) {
@@ -828,9 +803,9 @@ public class Worm extends GameObject {
t -= 0.01; t -= 0.01;
newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0); newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0);
if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() || if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > world.getWidth() ||
newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) { newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) {
if (getDistance(getLocation(), newLoc) < getRadius()) throw new IllegalStateException(); if (getDistance(loc, newLoc) < radius) throw new IllegalStateException();
break; break;
} }
} }
@@ -843,29 +818,6 @@ public class Worm extends GameObject {
return t; return t;
} }
// private Coordinate getJumpLocation() {
//
// double v = jumpVelocity();
// double t = calcJumpTime();
// double a = getOrientation();
// double radius = getRadius();
// World world = getWorld();
// Coordinate loc = getLocation();
// Coordinate newLoc;
//
// while (true) {
//
// t -= 0.01;
// newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0);
//
// if (newLoc.getX() < 0 || newLoc.getY() < 0 ||
// newLoc.getX() > getWorld().getWidth() || newLoc.getY() > world.getHeight()) {
// return newLoc;
// }
// if (world.isAdjacent(newLoc, radius)) return newLoc;
// }
// }
/** /**
* Return a boolean whether the worm can jump or not. * Return a boolean whether the worm can jump or not.
* *
@@ -874,32 +826,7 @@ public class Worm extends GameObject {
* |result == getActionPoints() > 0 && getOrientation() < PI * |result == getActionPoints() > 0 && getOrientation() < PI
*/ */
private boolean canJump() { private boolean canJump() {
return getActionPoints() > 0 && getOrientation() < PI; return this.actionPoints > 0 && this.orientation < PI;
}
/**
* Return the distance the worm will jump.
*
* @param v 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.
* |(pow(v, 2) * sin(2 * getOrientation())) / G
*/
private double jumpDistance(double v) {
return (pow(v, 2) * sin(2 * getOrientation())) / G;
}
/**
* Returns of the jump distance valid is.
*
* @param distance 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) {
return Math.abs(distance) >= getRadius();
} }
/** /**
@@ -913,8 +840,8 @@ public class Worm extends GameObject {
*/ */
private double jumpVelocity() { private double jumpVelocity() {
double force = 5 * getActionPoints() + getMass() * G; double force = 5 * this.actionPoints + this.mass * G;
return force / getMass() * FORCE_TIME; return force / this.mass * FORCE_TIME;
} }
/** /**
@@ -998,7 +925,7 @@ public class Worm extends GameObject {
* |new.getHitPoints() == old.getHitPoints() - value * |new.getHitPoints() == old.getHitPoints() - value
*/ */
public void decreaseHitPoints(long value) { public void decreaseHitPoints(long value) {
setHitPoints(getHitPoints() - value); setHitPoints(this.hitPoints - value);
} }
/** /**
@@ -1010,9 +937,9 @@ public class Worm extends GameObject {
*/ */
public void incrementHitPoints(long value) { public void incrementHitPoints(long value) {
long current = getHitPoints(); long current = this.hitPoints;
if (current + value < 0) setHitPoints(0); if (current + value < 0) setHitPoints(0);
else setHitPoints(getHitPoints() + value); else setHitPoints(this.hitPoints + value);
} }
@@ -1037,18 +964,18 @@ public class Worm extends GameObject {
* |checkEat(); * |checkEat();
*/ */
public void fall() { public void fall() {
double height = getWorld().getHeight() - getRadius(); double height = this.world.getHeight() - this.radius;
Coordinate oldLocation = getLocation(); Coordinate oldLocation = this.location;
if (canFall()) { if (canFall()) {
for (double y = oldLocation.getY(); y < height; y -= 0.01) { for (double y = oldLocation.getY(); y < height; y -= 0.01) {
Coordinate newLoc = Coordinate.create(oldLocation.getX(), Math.floor(y * 100.0) / 100.0); Coordinate newLoc = Coordinate.create(oldLocation.getX(), Math.floor(y * 100.0) / 100.0);
if (y - radius < 0) { if (y - this.radius < 0) {
terminate(); terminate();
break; break;
} }
if (getWorld().isPassable(newLoc, getRadius())) { if (this.world.isPassable(newLoc, this.radius)) {
setLocation(newLoc); setLocation(newLoc);
} else { } else {
break; break;
@@ -1056,11 +983,11 @@ public class Worm extends GameObject {
} }
} }
long cost = 3 * (long) Math.floor(oldLocation.getY() - getLocation().getY()); long cost = 3 * (long) Math.floor(oldLocation.getY() - this.location.getY());
decreaseHitPoints(cost); decreaseHitPoints(cost);
if (getWorld() != null) { if (this.world != null) {
for (Worm w : getWorld().getGameObjectsByClass(Worm.class)) { for (Worm w : this.world.getGameObjectsByClass(Worm.class)) {
if (w.equals(this)) continue; if (w.equals(this)) continue;
if (w.getDistance(this) < 0) { if (w.getDistance(this) < 0) {
@@ -1079,28 +1006,10 @@ public class Worm extends GameObject {
* |result == ! getWorld().isAdjacent(center, getRadius()) * |result == ! getWorld().isAdjacent(center, getRadius())
*/ */
public boolean canFall() { public boolean canFall() {
double[] center = {getLocation().getX(), getLocation().getY()}; double[] center = {this.location.getX(), this.location.getY()};
return !getWorld().isAdjacent(center, getRadius()); return !this.world.isAdjacent(center, this.radius);
} }
/**
* 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;
fallingWorm.incrementHitPoints(value);
stationaryWorm.decreaseHitPoints(value);
}
}
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -1163,21 +1072,21 @@ public class Worm extends GameObject {
if (terminate) food.terminate(); if (terminate) food.terminate();
double radius = getRadius(); double radius = this.radius;
double changeRadius = radius * 0.1; double changeRadius = radius * 0.1;
if (food.isPoisonous()) { if (food.isPoisonous()) {
changeRadius *= -1; changeRadius *= -1;
radius *= 0.9; radius *= 0.9;
if (radius < getMinRadius()) radius = getMinRadius(); if (radius < this.minRadius) radius = this.minRadius;
} else { } else {
radius *= 1.1; radius *= 1.1;
} }
decreaseActionPoints(8); decrementActionPoints(8);
World world = getWorld(); World world = this.world;
Coordinate location = getLocation(); Coordinate location = this.location;
if (!world.isAdjacent(location, radius)) { if (!world.isAdjacent(location, radius)) {
Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY()); Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY());
if (world.isAdjacent(newLoc, radius)) { if (world.isAdjacent(newLoc, radius)) {
@@ -1242,7 +1151,7 @@ public class Worm extends GameObject {
if (!canEat()) return; if (!canEat()) return;
World world = getWorld(); World world = this.world;
if (world != null) { if (world != null) {
List<Food> foodList = world.getFoodList(); List<Food> foodList = world.getFoodList();
for (Food food : foodList) { for (Food food : foodList) {
@@ -1255,7 +1164,7 @@ public class Worm extends GameObject {
} }
public Boolean canEat() { public Boolean canEat() {
return getActionPoints() >= 8; return this.actionPoints >= 8;
} }
@@ -1264,8 +1173,8 @@ public class Worm extends GameObject {
public Projectile fire() { public Projectile fire() {
if (canFire()) { if (canFire()) {
if (!getWorld().getGameObjectsByClass(Projectile.class).isEmpty()) { if (!this.world.getGameObjectsByClass(Projectile.class).isEmpty()) {
List<Projectile> projectiles = getWorld().getGameObjectsByClass(Projectile.class); List<Projectile> projectiles = this.world.getGameObjectsByClass(Projectile.class);
for (Projectile project: projectiles) { for (Projectile project: projectiles) {
if (this.getDistance(project) < 0) { if (this.getDistance(project) < 0) {
project.hit(this); project.hit(this);
@@ -1278,13 +1187,13 @@ public class Worm extends GameObject {
int random = ThreadLocalRandom.current().nextInt(2); int random = ThreadLocalRandom.current().nextInt(2);
if (random == 0) { if (random == 0) {
decreaseActionPoints(10); decrementActionPoints(10);
return new Rifle(getWorld(), getLocation(), getOrientation(), getRadius()); return new Rifle(this.world, this.location, this.orientation, this.radius);
} else { } else {
double force = Bazooka.calcForce(getActionPoints()); double force = Bazooka.calcForce(this.actionPoints);
decreaseActionPoints(25); decrementActionPoints(25);
return new Bazooka(getWorld(), getLocation(), getOrientation(), getRadius(), force); return new Bazooka(this.world, this.location, this.orientation, this.radius, force);
} }
} }
@@ -1292,7 +1201,7 @@ public class Worm extends GameObject {
} }
public Boolean canFire() { public Boolean canFire() {
return getActionPoints() >= 30 && getWorld() != null; return this.actionPoints >= 30 && this.world != null;
} }
// =================================================================================== // ===================================================================================
@@ -1305,7 +1214,7 @@ public class Worm extends GameObject {
public void loadProgram(Program program, IActionHandler actionHandler) { public void loadProgram(Program program, IActionHandler actionHandler) {
if (getWorld().hasActiveGame()) throw new IllegalStateException(); if (this.world.hasActiveGame()) throw new IllegalStateException();
this.program = program; this.program = program;
program.setWorm(this); program.setWorm(this);