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
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
public World createWorld(double width, double height, boolean[][] passableMap) throws ModelException {
return new World(width, height, passableMap);
try {
return new World(width, height, passableMap);
} catch (IllegalArgumentException e) {
throw new ModelException(e);
}
}
/**
@@ -585,7 +593,11 @@ public class Facade implements IFacade {
*/
@Override
public void removeFood(World world, Food food) throws ModelException {
world.remove(food);
try {
world.remove(food);
} catch (IllegalArgumentException e) {
throw new ModelException(e);
}
}
/**
@@ -695,7 +707,11 @@ public class Facade implements IFacade {
*/
@Override
public Worm createWorm(World world, double[] location, double direction, double radius, String name, Team team) throws ModelException {
return new Worm(world, location, direction, radius, name, team);
try {
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
public Team createTeam(World world, String name) throws ModelException, MustNotImplementException {
try {
return new Team(name);
return new Team(name, world);
} catch(IllegalNameException e) {
throw new ModelException(e.getMessage());
}
@@ -949,7 +965,7 @@ public class Facade implements IFacade {
*/
@Override
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
public void addWormsToTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException {
team.addWorm(worms);
try {
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 static java.lang.Math.cos;
import static java.lang.Math.sin;
public class Bazooka extends Projectile {
protected Bazooka(World world, Coordinate location, double force, double orientation) {
super(world, location, 300, force, orientation);
protected Bazooka(World world, Coordinate wormLocation, double wormOrientation, double wormRadius, double force) {
super(world, wormLocation, wormOrientation, wormRadius, 300, force);
}
@Override

View File

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

View File

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

View File

@@ -30,7 +30,7 @@ public class World {
*/
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.height = height;
@@ -51,8 +51,15 @@ public class World {
}
public void startGame() {
this.game = true;
this.activeWorm = 0;
try {
this.game = true;
this.activeWorm = -1;
activateNextWorm();
} catch (IllegalStateException e) {
this.game = false;
}
}
public void finishGame() {
@@ -65,11 +72,14 @@ public class World {
throw new IllegalStateException("No worms");
}
getWormList().get(this.activeWorm).setActionPoints((long) getWormList().get(this.activeWorm).getMass());
this.activeWorm++;
if (this.activeWorm == getWormList().size()) {
if (this.activeWorm >= getWormList().size()) {
this.activeWorm = 0;
}
Worm active = getWormList().get(this.activeWorm);
active.setActionPoints(active.getMaxActionPoints());
active.incrementHitPoints(10);
}
public Worm getActiveWorm() {
@@ -79,6 +89,8 @@ public class World {
public String getWinner() {
if (!hasActiveGame()) return null;
if (getWormList().size() == 1) {
return getWormList().get(0).getName();
} else if (getWormList().size() > 1) {
@@ -176,6 +188,10 @@ public class World {
* |this.terminated = true
*/
public void terminate() {
Set<GameObject> gameObjectList = new HashSet<>(getGameObjects());
gameObjectList.forEach(g -> g.terminate());
this.terminated = true;
}
@@ -212,7 +228,8 @@ public class World {
location[1] / lengthY >= getMap().length || location[1] < 0.0) {
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());
}
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 ...
@@ -382,11 +409,7 @@ public class World {
*/
public Set<Team> getAllTeams() {
Set<Team> teams = new HashSet<>();
for(Worm worm: getWormList()) {
teams.add(worm.getTeam());
}
return teams;
return new HashSet<>(teams);
}
/**
@@ -428,6 +451,11 @@ public class World {
public void remove(GameObject obj) throws 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);
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() ||
newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) {
if (getDistance(getLocation(), newLoc) < getRadius()) throw new IllegalStateException();
break;
}
}
@@ -1185,19 +1191,19 @@ public class Worm extends GameObject {
if (getActionPoints() >= 30 && getWorld() != null) {
int random = ThreadLocalRandom.current().nextInt(2);
Coordinate projLocation = Coordinate.create(cos(getOrientation()) * getRadius(), sin(getOrientation()) * getRadius());
if (random == 0) {
decreaseActionPoints(10);
return new Rifle(getWorld(), projLocation,getOrientation());
return new Rifle(getWorld(), getLocation(), getOrientation(), getRadius());
} else {
double force = Bazooka.calcForce(getActionPoints());
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() {