This commit is contained in:
2018-05-16 16:25:06 +02:00
parent e0be7248ed
commit 2cd0d3af7a
8 changed files with 170 additions and 89 deletions

View File

@@ -402,7 +402,11 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public Projectile fire(Worm worm) throws ModelException { public Projectile fire(Worm worm) throws ModelException {
return null; try {
return worm.fire();
} catch (IllegalStateException e) {
throw new ModelException(e);
}
} }
/** /**
@@ -427,7 +431,11 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public World createWorld(double width, double height, boolean[][] passableMap) throws ModelException { public World createWorld(double width, double height, boolean[][] passableMap) throws ModelException {
try {
return new World(width, height, passableMap); return new World(width, height, passableMap);
} catch (IllegalArgumentException e) {
throw new ModelException(e);
}
} }
/** /**
@@ -585,7 +593,11 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void removeFood(World world, Food food) throws ModelException { public void removeFood(World world, Food food) throws ModelException {
try {
world.remove(food); world.remove(food);
} catch (IllegalArgumentException e) {
throw new ModelException(e);
}
} }
/** /**
@@ -695,7 +707,11 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public Worm createWorm(World world, double[] location, double direction, double radius, String name, Team team) throws ModelException { public Worm createWorm(World world, double[] location, double direction, double radius, String name, Team team) throws ModelException {
try {
return new Worm(world, location, direction, radius, name, team); return new Worm(world, location, direction, radius, name, team);
} catch(IllegalArgumentException e) {
throw new ModelException(e);
}
} }
/** /**
@@ -897,7 +913,7 @@ public class Facade implements IFacade {
@Override @Override
public Team createTeam(World world, String name) throws ModelException, MustNotImplementException { public Team createTeam(World world, String name) throws ModelException, MustNotImplementException {
try { try {
return new Team(name); return new Team(name, world);
} catch(IllegalNameException e) { } catch(IllegalNameException e) {
throw new ModelException(e.getMessage()); throw new ModelException(e.getMessage());
} }
@@ -949,7 +965,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public List<Worm> getAllWormsOfTeam(Team team) throws ModelException, MustNotImplementException { public List<Worm> getAllWormsOfTeam(Team team) throws ModelException, MustNotImplementException {
return (List<Worm>) team.getAllWormsOfTeam(); return team.getAllWormsList();
} }
/** /**
@@ -957,7 +973,11 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void addWormsToTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException { public void addWormsToTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException {
try {
team.addWorm(worms); team.addWorm(worms);
} catch(IllegalArgumentException e) {
throw new ModelException(e);
}
} }
/** /**

View File

@@ -4,9 +4,12 @@ import worms.util.Coordinate;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
public class Bazooka extends Projectile { public class Bazooka extends Projectile {
protected Bazooka(World world, Coordinate location, double force, double orientation) { protected Bazooka(World world, Coordinate wormLocation, double wormOrientation, double wormRadius, double force) {
super(world, location, 300, force, orientation); super(world, wormLocation, wormOrientation, wormRadius, 300, force);
} }
@Override @Override

View File

@@ -43,7 +43,7 @@ public abstract class GameObject {
*/ */
protected GameObject(World world, double[] location, double radius) { protected GameObject(World world, double[] location, double radius) {
setWorld(world); setWorld(world);
world.add(this); if (isValidWorld(world)) world.add(this);
setLocation(location); setLocation(location);
setRadius(radius); setRadius(radius);
} }
@@ -73,7 +73,7 @@ public abstract class GameObject {
* @return ... |result == !world.hasActiveGame() && !world.isTerminated() * @return ... |result == !world.hasActiveGame() && !world.isTerminated()
*/ */
public static boolean isValidWorld(World world) { public static boolean isValidWorld(World world) {
return !world.hasActiveGame() && !world.isTerminated(); return world != null && !world.hasActiveGame() && !world.isTerminated();
} }
/** /**
@@ -241,7 +241,8 @@ public abstract class GameObject {
* |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) * |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
*/ */
protected void setMass(double radius, double rho) { protected void setMass(double radius, double rho) {
this.mass = (double) round(rho * (4.0 / 3.0 * PI * pow(radius, 3)));
this.mass = rho * (4.0 / 3.0 * PI * pow(radius, 3.0));
} }

View File

@@ -8,12 +8,11 @@ public abstract class Projectile extends GameObject {
private static final int rho = 7800; private static final int rho = 7800;
protected Projectile (World world, Coordinate wormLocation, double wormOrientation, double wormRadius, double mass, double force) {
protected Projectile (World world, Coordinate location, double mass, double force, double orientation) { super(world, calcLocation(wormLocation, wormOrientation, wormRadius, mass), calcRadius(mass));
super(world, location, calcRadius(mass));
super.mass = mass; super.mass = mass;
this.force = force; this.force = force;
this.orientation = orientation; this.orientation = wormOrientation;
this.hitPoints = getRandomHitPoints(); this.hitPoints = getRandomHitPoints();
} }
@@ -83,70 +82,80 @@ public abstract class Projectile extends GameObject {
private double jumpVelocity() { private double jumpVelocity() {
//wat met de action points? //wat met de action points?
double force = 5 * Worm.getActionPoints() + getMass() * G; // double force = 5 * Worm.getActionPoints() + getMass() * G;
return force/getMass() * FORCE_TIME; // return force/getMass() * FORCE_TIME;
return 0.0;
} }
private double getJumpTime(double jumpTimeStep) { private double getJumpTime(double jumpTimeStep) {
// TODO zie naar worm hoe dit moet, implementatie moet wel anders! // // TODO zie naar worm hoe dit moet, implementatie moet wel anders!
//wat met parameter jumpTimeStep? // //wat met parameter jumpTimeStep?
//
double v = jumpVelocity(); // double v = jumpVelocity();
double time = 0; // double time = 0;
double a = getOrientation(); // double a = getOrientation();
Coordinate loc = getLocation(); // Coordinate loc = getLocation();
World world = getWorld(); // World world = getWorld();
double radius = getRadius(); // double radius = getRadius();
Coordinate newLoc; // Coordinate newLoc;
//
while (true) { // while (true) {
time += 0.05; // time += 0.05;
double x = loc.getX() + v * time * cos(a); // double x = loc.getX() + v * time * cos(a);
double y = loc.getY() + v * time * sin(a) - (G * time * time) / 2.0; // double y = loc.getY() + v * time * sin(a) - (G * time * time) / 2.0;
newLoc = Coordinate.create(x, y); // newLoc = Coordinate.create(x, y);
if (! world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) { // if (! world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) {
while (true) { // while (true) {
time -= 0.01; // time -= 0.01;
newLoc = Coordinate.create(loc.getX() + v * time * cos(a), loc.getY() + v * time * sin(a) - (G * time * time) / 2.0); // newLoc = Coordinate.create(loc.getX() + v * time * cos(a), loc.getY() + v * time * sin(a) - (G * time * time) / 2.0);
if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() || // if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() ||
newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) { // newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) {
break; // break;
} // }
} // }
break; // break;
} // }
if ((int) round(time) == 10) { // if ((int) round(time) == 10) {
throw new RuntimeException(); // throw new RuntimeException();
} // }
} // }
//
return time; // return time;
return 0.0;
} }
private boolean canJump() { private boolean canJump() {
// wat met action points. // wat met action points.
return Worm.getActionPoints() > 0 && getOrientation() < PI; //return this.getActionPoints() > 0 && getOrientation() < PI;
return false;
} }
public void jump() throws IllegalStateException { public void jump() throws IllegalStateException {
// TODO zie naar worm hoe dit moet, implementatie moet wel anders! // // TODO zie naar worm hoe dit moet, implementatie moet wel anders!
// wat meegeven met getJumpTime() ? // // wat meegeven met getJumpTime() ?
// wat met action points? // // wat met action points?
//
if (!canJump()) // if (!canJump())
throw new IllegalStateException(); // throw new IllegalStateException();
//
double v = jumpVelocity(); // double v = jumpVelocity();
double t = getJumpTime(); // double t = getJumpTime();
double a = getOrientation(); // double a = getOrientation();
//
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(getLocation().getX() + v * t * cos(a), getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0);
//
if (! isValidLocation(newLocation)) { // if (! isValidLocation(newLocation)) {
terminate(); // terminate();
return; // return;
// }
// setLocation(newLocation);
// //Worm.setActionPoints(0);
} }
setLocation(newLocation);
Worm.setActionPoints(0); private static Coordinate calcLocation(Coordinate wormLocation, double wormOrientation, double wormRadius, double mass) {
double radius = calcRadius(mass);
return Coordinate.create(wormLocation.getX() + cos(wormOrientation) * (wormRadius + radius),
wormLocation.getY() + sin(wormOrientation) * (wormRadius + radius));
} }
} }

View File

@@ -4,11 +4,14 @@ import worms.util.Coordinate;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
public class Rifle extends Projectile { public class Rifle extends Projectile {
public Rifle(World world, Coordinate location, double orientation) { public Rifle(World world, Coordinate wormLocation, double wormOrientation, double wormRadius) {
super(world, location, 10, 1.5, orientation); super(world, wormLocation, wormOrientation, wormRadius, 10, 1.5);
} }
@Override @Override
@@ -23,4 +26,8 @@ public class Rifle extends Projectile {
super.hitPoints = value; super.hitPoints = value;
} }
public static double calcRadius() {
return calcRadius(10);
}
} }

View File

@@ -20,9 +20,12 @@ public class Team {
* @post ... * @post ...
* |wormCollection = new Treeset<>(new TeamComparator()) * |wormCollection = new Treeset<>(new TeamComparator())
*/ */
public Team (String name) { public Team (String name, World world) {
setName(name); setName(name);
this.wormCollection = new TreeSet<>(new TeamComparator()); this.wormCollection = new TreeSet<>(new TeamComparator());
if (world != null) {
world.addTeam(this);
}
} }
//=================================================================================== //===================================================================================
@@ -140,6 +143,10 @@ public class Team {
return wormCollection; return wormCollection;
} }
public List<Worm> getAllWormsList() {
return new ArrayList<>(this.wormCollection);
}
/** /**
* *
* @return ... * @return ...

View File

@@ -30,7 +30,7 @@ public class World {
*/ */
public World(double width, double height, boolean[][] map) { public World(double width, double height, boolean[][] map) {
if (!isValidDimension(width) || !isValidDimension(height)) throw new IllegalArgumentException(); if (!isValidDimension(width) || !isValidDimension(height) || map == null || map.length != height || map[0].length != height) throw new IllegalArgumentException();
this.width = width; this.width = width;
this.height = height; this.height = height;
@@ -51,8 +51,15 @@ public class World {
} }
public void startGame() { public void startGame() {
try {
this.game = true; this.game = true;
this.activeWorm = 0; this.activeWorm = -1;
activateNextWorm();
} catch (IllegalStateException e) {
this.game = false;
}
} }
public void finishGame() { public void finishGame() {
@@ -65,11 +72,14 @@ public class World {
throw new IllegalStateException("No worms"); throw new IllegalStateException("No worms");
} }
getWormList().get(this.activeWorm).setActionPoints((long) getWormList().get(this.activeWorm).getMass());
this.activeWorm++; this.activeWorm++;
if (this.activeWorm == getWormList().size()) { if (this.activeWorm >= getWormList().size()) {
this.activeWorm = 0; this.activeWorm = 0;
} }
Worm active = getWormList().get(this.activeWorm);
active.setActionPoints(active.getMaxActionPoints());
active.incrementHitPoints(10);
} }
public Worm getActiveWorm() { public Worm getActiveWorm() {
@@ -79,6 +89,8 @@ public class World {
public String getWinner() { public String getWinner() {
if (!hasActiveGame()) return null;
if (getWormList().size() == 1) { if (getWormList().size() == 1) {
return getWormList().get(0).getName(); return getWormList().get(0).getName();
} else if (getWormList().size() > 1) { } else if (getWormList().size() > 1) {
@@ -176,6 +188,10 @@ public class World {
* |this.terminated = true * |this.terminated = true
*/ */
public void terminate() { public void terminate() {
Set<GameObject> gameObjectList = new HashSet<>(getGameObjects());
gameObjectList.forEach(g -> g.terminate());
this.terminated = true; this.terminated = true;
} }
@@ -212,7 +228,8 @@ public class World {
location[1] / lengthY >= getMap().length || location[1] < 0.0) { location[1] / lengthY >= getMap().length || location[1] < 0.0) {
return false; return false;
} }
return this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)];
return this.map[map.length - 1 - (int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)];
} }
/** /**
@@ -375,6 +392,16 @@ public class World {
return new ArrayList<>(getGameObjects()); return new ArrayList<>(getGameObjects());
} }
private Set<Team> teams = new HashSet<>();
public void addTeam(Team team) {
if (teams.size() == 10) throw new IllegalStateException("Maximum 10 teams");
if (!teams.add(team)) throw new IllegalArgumentException("Team already exists");
}
/** /**
* *
* @return ... * @return ...
@@ -382,11 +409,7 @@ public class World {
*/ */
public Set<Team> getAllTeams() { public Set<Team> getAllTeams() {
Set<Team> teams = new HashSet<>(); return new HashSet<>(teams);
for(Worm worm: getWormList()) {
teams.add(worm.getTeam());
}
return teams;
} }
/** /**
@@ -428,6 +451,11 @@ public class World {
public void remove(GameObject obj) throws IllegalArgumentException { public void remove(GameObject obj) throws IllegalArgumentException {
if (!getGameObjects().remove(obj)) throw new IllegalArgumentException(); if (!getGameObjects().remove(obj)) throw new IllegalArgumentException();
obj.setWorld(null);
if (obj.getClass().equals(Worm.class)) {
//System.out.println("remove " + ((Worm) obj).getName());
}
} }
/** /**

View File

@@ -127,6 +127,10 @@ public class Worm extends GameObject {
long startHitPoints = ThreadLocalRandom.current().nextLong(1001, 2000); long startHitPoints = ThreadLocalRandom.current().nextLong(1001, 2000);
setHitPoints(startHitPoints); setHitPoints(startHitPoints);
if (team != null) {
team.addWorm(this);
}
} }
//=================================================================================== //===================================================================================
@@ -776,6 +780,8 @@ public class Worm extends GameObject {
if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() || if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().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();
break; break;
} }
} }
@@ -1185,19 +1191,19 @@ public class Worm extends GameObject {
if (getActionPoints() >= 30 && getWorld() != null) { if (getActionPoints() >= 30 && getWorld() != null) {
int random = ThreadLocalRandom.current().nextInt(2); int random = ThreadLocalRandom.current().nextInt(2);
Coordinate projLocation = Coordinate.create(cos(getOrientation()) * getRadius(), sin(getOrientation()) * getRadius());
if (random == 0) { if (random == 0) {
decreaseActionPoints(10); decreaseActionPoints(10);
return new Rifle(getWorld(), projLocation,getOrientation()); return new Rifle(getWorld(), getLocation(), getOrientation(), getRadius());
} else { } else {
double force = Bazooka.calcForce(getActionPoints()); double force = Bazooka.calcForce(getActionPoints());
decreaseActionPoints(25); decreaseActionPoints(25);
return new Bazooka(getWorld(), projLocation, force, getOrientation());
return new Bazooka(getWorld(), getLocation(), getOrientation(), getRadius(), force);
} }
} }
return null; throw new IllegalStateException();
} }
// public void hitByRifle() { // public void hitByRifle() {