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;
import be.kuleuven.cs.som.annotate.Model;
import worms.internal.gui.game.IActionHandler;
import worms.model.*;
import worms.model.Food;
@@ -22,7 +21,7 @@ public class Facade implements IFacade {
/**
* Turns the given worm by the given angle.
*
*
* @param worm
* the worm who is going to move
* @param angle
@@ -158,7 +157,7 @@ public class Facade implements IFacade {
/**
* Returns the location on the jump trajectory of the given worm after a time t.
*
*
* @param worm
* the worm who is going to move
* @param t
@@ -769,7 +768,7 @@ public class Facade implements IFacade {
/**
* Returns the current orientation of the given worm (in radians).
*
*
* @param worm
* the worm who is going to move
*/
@@ -780,7 +779,7 @@ public class Facade implements IFacade {
/**
* Returns the radius of the given worm.
*
*
* @param worm
* the worm who is going to move
*/
@@ -791,7 +790,7 @@ public class Facade implements IFacade {
/**
* Sets the radius of the given worm to the given value.
*
*
* @param worm
* the worm who is going to move
* @param newRadius
@@ -814,7 +813,7 @@ public class Facade implements IFacade {
/**
* Returns the current number of action points of the given worm.
*
*
* @param worm
* the worm who is going to move
*/
@@ -826,17 +825,17 @@ public class Facade implements IFacade {
/**
* Decreases the current number of action points of the given worm with the
* given delta.
*
*
* @param worm
* the worm who is going to move
* @param delta
* the value that schould be decreased
* @post the action points are decreased with delta
* |worm.decreaseActionPoints((int) delta)
* |worm.decrementActionPoints((int) delta)
*/
@Override
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() {
this.terminated = true;
if (world != null) {
getWorld().remove(this);
if (this.world != null) {
this.world.remove(this);
}
}

View File

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

View File

@@ -217,7 +217,7 @@ public class Worm extends GameObject {
this.radius = radius;
final double rho = 1062;
setMass(radius, rho);
setMaxActionPoints(getMass());
setMaxActionPoints(this.mass);
}
/**
@@ -246,23 +246,13 @@ public class Worm extends GameObject {
*/
@Raw
private boolean canHaveAsRadius(double radius) {
if (getWorld() != null) {
return !Double.isNaN(radius) && radius >= getMinRadius() && !Double.isInfinite(radius) && getWorld().isPassable(getLocation(), radius);
if (this.world != null) {
return !Double.isNaN(radius) && radius >= this.minRadius && !Double.isInfinite(radius) && this.world.isPassable(this.location, radius);
} 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
@@ -306,14 +296,21 @@ public class Worm extends GameObject {
*/
@Raw
public void setActionPoints(long actionPoints) {
if (actionPoints > getMaxActionPoints())
actionPoints = getMaxActionPoints();
if (actionPoints > this.maxActionPoints)
actionPoints = this.maxActionPoints;
else if (actionPoints < 0)
actionPoints = 0;
this.actionPoints = actionPoints;
}
// TODO add documentatie
@Raw
public void incrementActionPoints(long delta) {
setActionPoints(this.actionPoints + delta);
}
/**
* 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.
* |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.
*/
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.
@@ -352,33 +356,14 @@ public class Worm extends GameObject {
@Raw
private void setMaxActionPoints(double maxActionPoints) {
this.maxActionPoints = round(maxActionPoints);
setActionPoints(getActionPoints());
setActionPoints(this.actionPoints);
}
/**
* Substract the current points of the worm.
*
* @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)
* This variable contains the maximum action points a worm can have.
*/
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
@@ -477,39 +462,21 @@ public class Worm extends GameObject {
*/
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();
Coordinate newLocation = getFurthestLocationInDirection(newDirection, this.getRadius());
double distance = getDistance(this.getLocation(), newLocation);
Coordinate newLocation = getFurthestLocationInDirection(newDirection, this.radius);
double distance = getDistance(this.location, newLocation);
long cost = (long) Math.ceil(abs(distance * cos(newDirection)) + abs(4 * distance * sin(newDirection)));
if (cost > getActionPoints())
if (cost > this.actionPoints)
throw new IllegalArgumentException();
World world = getWorld();
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);
}
}
collision(newLocation);
setLocation(newLocation);
subtractActionPoints(cost);
decrementActionPoints(cost);
}
/**
@@ -523,15 +490,15 @@ public class Worm extends GameObject {
*/
public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) {
Coordinate currentLocation = getLocation();
double radius = getRadius();
World world = getWorld();
Coordinate currentLocation = this.location;
double radius = this.radius;
World world = this.world;
if (direction < 0 || direction > 2 * Math.PI || Double.isNaN(direction) || maxDistance < 0 ||
Double.isNaN(maxDistance) || Double.isInfinite(maxDistance))
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));
}
@@ -573,23 +540,25 @@ public class Worm extends GameObject {
* |result == maxDistance
*/
public double getFurthestLocationDirection() {
Coordinate location = getLocation();
double direction = getOrientation();
Coordinate location = this.location;
double direction = this.orientation;
double minDirection = direction - 0.7875;
double maxDirection = direction + 0.7875;
double maxLocDirection = minDirection;
Coordinate maxLoc = location;
for (; minDirection <= maxDirection; minDirection += 0.0175) {
if (minDirection < 0) minDirection = 0.0;
Coordinate tempLoc = getFurthestLocationInDirection(minDirection, this.getRadius());
if (!getWorld().isAdjacent(tempLoc, getRadius())) tempLoc = location;
Coordinate tempLoc = getFurthestLocationInDirection(minDirection, this.radius);
if (!this.world.isAdjacent(tempLoc, this.radius)) tempLoc = location;
if (getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) {
maxLoc = tempLoc;
maxLocDirection = minDirection;
}
}
Coordinate tempLoc = getFurthestLocationInDirection(maxDirection, this.getRadius());
if (getWorld().isAdjacent(tempLoc, getRadius()) && getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) {
Coordinate tempLoc = getFurthestLocationInDirection(maxDirection, this.radius);
if (this.world.isAdjacent(tempLoc, this.radius) && getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) {
maxLoc = tempLoc;
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 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.
*
* @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.
* |new.getHitPoints() == old.getHitPoints - loseSmallest
*/
public void collision(Worm basicWorm, Worm... worm) {
Worm largestWorm;
Worm smallestWorm;
for (Worm w1 : worm) {
if (w1.getMass() > basicWorm.getMass()) {
largestWorm = w1;
smallestWorm = basicWorm;
} else {
largestWorm = basicWorm;
smallestWorm = w1;
public void collision(Coordinate newLocation) {
World world = this.world;
if (world != null && world.isAdjacent(newLocation, this.radius)) {
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);
}
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) {
assert canTurn(angle);
setOrientation(getOrientation() + angle);
subtractActionPoints(abs(angle));
setOrientation(this.orientation + angle);
decreaseActionPointsByAngle(abs(angle));
}
/**
@@ -682,10 +656,10 @@ public class Worm extends GameObject {
* | (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) )
*/
protected boolean canTurn(double angle) {
double currentAngle = getOrientation();
double currentAngle = this.orientation;
return 0 <= angle + currentAngle && angle + currentAngle < (2 * PI) &&
!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 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)) {
terminate();
return;
}
if (!newLocation.equals(getLocation())) {
if (!newLocation.equals(location)) {
setLocation(newLocation);
fight();
}
setActionPoints(0);
this.actionPoints = 0;
}
@@ -779,13 +754,13 @@ public class Worm extends GameObject {
* |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
*/
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();
double velocity = this.jumpVelocity();
double velocity = jumpVelocity();
return Coordinate.create(getLocation().getX() + velocity * cos(getOrientation()) * deltaTime,
getLocation().getY() + velocity * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2));
return Coordinate.create(this.location.getX() + velocity * cos(this.orientation) * deltaTime,
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() {
World world = getWorld();
World world = this.world;
if (world == null) throw new IllegalStateException("World cannot be null");
double v = jumpVelocity();
double t = 0;
double a = getOrientation();
Coordinate loc = getLocation();
double a = this.orientation;
Coordinate loc = this.location;
double radius = getRadius();
double radius = this.radius;
Coordinate newLoc;
while (true) {
@@ -828,9 +803,9 @@ public class Worm extends GameObject {
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() ||
if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > world.getWidth() ||
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;
}
}
@@ -843,29 +818,6 @@ public class Worm extends GameObject {
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.
*
@@ -874,32 +826,7 @@ public class Worm extends GameObject {
* |result == getActionPoints() > 0 && getOrientation() < PI
*/
private boolean canJump() {
return getActionPoints() > 0 && getOrientation() < 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();
return this.actionPoints > 0 && this.orientation < PI;
}
/**
@@ -913,8 +840,8 @@ public class Worm extends GameObject {
*/
private double jumpVelocity() {
double force = 5 * getActionPoints() + getMass() * G;
return force / getMass() * FORCE_TIME;
double force = 5 * this.actionPoints + this.mass * G;
return force / this.mass * FORCE_TIME;
}
/**
@@ -998,7 +925,7 @@ public class Worm extends GameObject {
* |new.getHitPoints() == old.getHitPoints() - 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) {
long current = getHitPoints();
long current = this.hitPoints;
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();
*/
public void fall() {
double height = getWorld().getHeight() - getRadius();
Coordinate oldLocation = getLocation();
double height = this.world.getHeight() - this.radius;
Coordinate oldLocation = this.location;
if (canFall()) {
for (double y = oldLocation.getY(); y < height; y -= 0.01) {
Coordinate newLoc = Coordinate.create(oldLocation.getX(), Math.floor(y * 100.0) / 100.0);
if (y - radius < 0) {
if (y - this.radius < 0) {
terminate();
break;
}
if (getWorld().isPassable(newLoc, getRadius())) {
if (this.world.isPassable(newLoc, this.radius)) {
setLocation(newLoc);
} else {
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);
if (getWorld() != null) {
for (Worm w : getWorld().getGameObjectsByClass(Worm.class)) {
if (this.world != null) {
for (Worm w : this.world.getGameObjectsByClass(Worm.class)) {
if (w.equals(this)) continue;
if (w.getDistance(this) < 0) {
@@ -1079,28 +1006,10 @@ public class Worm extends GameObject {
* |result == ! getWorld().isAdjacent(center, getRadius())
*/
public boolean canFall() {
double[] center = {getLocation().getX(), getLocation().getY()};
return !getWorld().isAdjacent(center, getRadius());
double[] center = {this.location.getX(), this.location.getY()};
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
@@ -1163,21 +1072,21 @@ public class Worm extends GameObject {
if (terminate) food.terminate();
double radius = getRadius();
double radius = this.radius;
double changeRadius = radius * 0.1;
if (food.isPoisonous()) {
changeRadius *= -1;
radius *= 0.9;
if (radius < getMinRadius()) radius = getMinRadius();
if (radius < this.minRadius) radius = this.minRadius;
} else {
radius *= 1.1;
}
decreaseActionPoints(8);
decrementActionPoints(8);
World world = getWorld();
Coordinate location = getLocation();
World world = this.world;
Coordinate location = this.location;
if (!world.isAdjacent(location, radius)) {
Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY());
if (world.isAdjacent(newLoc, radius)) {
@@ -1242,7 +1151,7 @@ public class Worm extends GameObject {
if (!canEat()) return;
World world = getWorld();
World world = this.world;
if (world != null) {
List<Food> foodList = world.getFoodList();
for (Food food : foodList) {
@@ -1255,7 +1164,7 @@ public class Worm extends GameObject {
}
public Boolean canEat() {
return getActionPoints() >= 8;
return this.actionPoints >= 8;
}
@@ -1264,8 +1173,8 @@ public class Worm extends GameObject {
public Projectile fire() {
if (canFire()) {
if (!getWorld().getGameObjectsByClass(Projectile.class).isEmpty()) {
List<Projectile> projectiles = getWorld().getGameObjectsByClass(Projectile.class);
if (!this.world.getGameObjectsByClass(Projectile.class).isEmpty()) {
List<Projectile> projectiles = this.world.getGameObjectsByClass(Projectile.class);
for (Projectile project: projectiles) {
if (this.getDistance(project) < 0) {
project.hit(this);
@@ -1278,13 +1187,13 @@ public class Worm extends GameObject {
int random = ThreadLocalRandom.current().nextInt(2);
if (random == 0) {
decreaseActionPoints(10);
return new Rifle(getWorld(), getLocation(), getOrientation(), getRadius());
decrementActionPoints(10);
return new Rifle(this.world, this.location, this.orientation, this.radius);
} else {
double force = Bazooka.calcForce(getActionPoints());
decreaseActionPoints(25);
double force = Bazooka.calcForce(this.actionPoints);
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() {
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) {
if (getWorld().hasActiveGame()) throw new IllegalStateException();
if (this.world.hasActiveGame()) throw new IllegalStateException();
this.program = program;
program.setWorm(this);