update
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
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;
|
||||||
@@ -61,7 +62,7 @@ public class Facade implements IFacade {
|
|||||||
try {
|
try {
|
||||||
return worm.getFurthestLocationInDirection(direction, maxDistance).toArray();
|
return worm.getFurthestLocationInDirection(direction, maxDistance).toArray();
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
throw new ModelException("");
|
throw new ModelException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -538,7 +539,11 @@ public class Facade implements IFacade {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void addWorm(World world, Worm worm) throws ModelException {
|
public void addWorm(World world, Worm worm) throws ModelException {
|
||||||
|
try {
|
||||||
world.add(worm);
|
world.add(worm);
|
||||||
|
} catch(IllegalStateException | IllegalArgumentException e) {
|
||||||
|
throw new ModelException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -549,7 +554,11 @@ public class Facade implements IFacade {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void removeWorm(World world, Worm worm) throws ModelException {
|
public void removeWorm(World world, Worm worm) throws ModelException {
|
||||||
|
try {
|
||||||
world.remove(worm);
|
world.remove(worm);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw new ModelException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -582,7 +591,11 @@ public class Facade implements IFacade {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void addFood(World world, Food food) throws ModelException {
|
public void addFood(World world, Food food) throws ModelException {
|
||||||
|
try {
|
||||||
world.add(food);
|
world.add(food);
|
||||||
|
} catch(IllegalArgumentException | IllegalStateException e) {
|
||||||
|
throw new ModelException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -985,7 +998,11 @@ public class Facade implements IFacade {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void removeWormsFromTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException {
|
public void removeWormsFromTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException {
|
||||||
|
try {
|
||||||
team.removeWormsFromTeam(worms);
|
team.removeWormsFromTeam(worms);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw new ModelException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
package worms.model;
|
package worms.model;
|
||||||
|
|
||||||
|
import worms.util.Coordinate;
|
||||||
|
|
||||||
public class Food extends GameObject {
|
public class Food extends GameObject {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,4 +39,16 @@ public class Food extends GameObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean poisonous = false;
|
private boolean poisonous = false;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isValidLocation(Coordinate location) {
|
||||||
|
if (getWorld() != null) return super.isValidLocation(location) && getWorld().isAdjacent(location, getRadius());
|
||||||
|
return super.isValidLocation(location);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isValidLocation(Coordinate location, World world) {
|
||||||
|
if (world != null) return super.isValidLocation(location) && world.isAdjacent(location, getRadius());
|
||||||
|
return super.isValidLocation(location);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -42,10 +42,10 @@ public abstract class GameObject {
|
|||||||
* |setRadius(radius)
|
* |setRadius(radius)
|
||||||
*/
|
*/
|
||||||
protected GameObject(World world, double[] location, double radius) {
|
protected GameObject(World world, double[] location, double radius) {
|
||||||
setWorld(world);
|
|
||||||
if (isValidWorld(world)) world.add(this);
|
|
||||||
setLocation(location);
|
setLocation(location);
|
||||||
setRadius(radius);
|
setRadius(radius);
|
||||||
|
setWorld(world);
|
||||||
|
if (isValidWorld(world)) world.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,8 +116,10 @@ public abstract class GameObject {
|
|||||||
*/
|
*/
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
this.terminated = true;
|
this.terminated = true;
|
||||||
|
if (world != null) {
|
||||||
getWorld().remove(this);
|
getWorld().remove(this);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the location of the game object
|
* Return the location of the game object
|
||||||
|
@@ -52,17 +52,15 @@ public class Team {
|
|||||||
* ...
|
* ...
|
||||||
* |worm == null || ! canHaveAsWorm(worm)
|
* |worm == null || ! canHaveAsWorm(worm)
|
||||||
*/
|
*/
|
||||||
public void addWorm(Worm... worm) throws IllegalArgumentException {
|
public void addWorm(Worm... worms) throws IllegalArgumentException {
|
||||||
if (worm == null) throw new IllegalArgumentException();
|
if (worms == null) throw new IllegalArgumentException();
|
||||||
for (Worm w: worm) {
|
if (!canHaveAsWorm(worms)) throw new IllegalArgumentException();
|
||||||
if (w != null && canHaveAsWorm(w)) {
|
|
||||||
getAllWormsOfTeam().add(w);
|
Collection<Worm> wormCollection = getAllWormsOfTeam();
|
||||||
w.setTeam(this);
|
|
||||||
}
|
Arrays.stream(worms).forEach(w -> w.setTeam(this));
|
||||||
else {
|
|
||||||
throw new IllegalArgumentException();
|
getAllWormsOfTeam().addAll(Arrays.asList(worms));
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,22 +76,22 @@ public class Team {
|
|||||||
* ...
|
* ...
|
||||||
* |worm == null
|
* |worm == null
|
||||||
*/
|
*/
|
||||||
private boolean canHaveAsWorm(Worm worm) throws IllegalArgumentException {
|
private boolean canHaveAsWorm(Worm... worm) {
|
||||||
|
|
||||||
if (worm == null) throw new IllegalArgumentException();
|
|
||||||
|
|
||||||
|
HashSet<String> names = new HashSet<>();
|
||||||
Collection<Worm> worms = getAllWormsOfTeam();
|
Collection<Worm> worms = getAllWormsOfTeam();
|
||||||
if (worms.size() == 0 && !worm.isTerminated()) return true;
|
|
||||||
|
|
||||||
if (worm.getMass() >= getMinMassTeam() / 2 && worm.getMass() <= 2 * getMinMassTeam()
|
for (Worm w : worm) {
|
||||||
&& !worm.isTerminated()) {
|
if (!names.add(w.getName())) return false;
|
||||||
for (Worm elWorm : worms) {
|
if (worms.contains(w)) return false;
|
||||||
if (elWorm.getName().equals(worm.getName())) return false;
|
if (worms.size() == 0 && !w.isTerminated()) continue;
|
||||||
|
if (w.getMass() < getMinMassTeam() / 2 || w.getMass() > 2 * getMinMassTeam()
|
||||||
|
|| w.isTerminated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -258,6 +256,9 @@ public class Team {
|
|||||||
* |new.terminatd = true
|
* |new.terminatd = true
|
||||||
*/
|
*/
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
|
|
||||||
|
removeWormsFromTeam(getAllWormsOfTeam().toArray(new Worm[0]));
|
||||||
|
|
||||||
this.terminated = true;
|
this.terminated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -434,9 +434,13 @@ public class World {
|
|||||||
* ...
|
* ...
|
||||||
* |!getGameObjects().add(obj)
|
* |!getGameObjects().add(obj)
|
||||||
*/
|
*/
|
||||||
public void add(GameObject obj) throws NullPointerException {
|
public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException {
|
||||||
if (hasActiveGame()) throw new IllegalStateException();
|
if (hasActiveGame()) throw new IllegalStateException();
|
||||||
if (!getGameObjects().add(obj)) throw new IllegalArgumentException();
|
if (obj == null || !getGameObjects().add(obj) || obj.isTerminated()) throw new IllegalArgumentException();
|
||||||
|
|
||||||
|
if (obj.getClass().equals(Food.class) && !((Food) obj).isValidLocation(obj.getLocation(), this)) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
obj.setWorld(this);
|
obj.setWorld(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,10 +456,6 @@ public class World {
|
|||||||
|
|
||||||
if (!getGameObjects().remove(obj)) throw new IllegalArgumentException();
|
if (!getGameObjects().remove(obj)) throw new IllegalArgumentException();
|
||||||
obj.setWorld(null);
|
obj.setWorld(null);
|
||||||
|
|
||||||
if (obj.getClass().equals(Worm.class)) {
|
|
||||||
//System.out.println("remove " + ((Worm) obj).getName());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -493,7 +493,6 @@ public class World {
|
|||||||
public List<Food> getFoodList() {
|
public List<Food> getFoodList() {
|
||||||
|
|
||||||
return getGameObjectsByClass(Food.class);
|
return getGameObjectsByClass(Food.class);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -539,6 +538,10 @@ public class World {
|
|||||||
int size = gameObjects.size();
|
int size = gameObjects.size();
|
||||||
int nb1 = new Random().nextInt(size);
|
int nb1 = new Random().nextInt(size);
|
||||||
int nb2 = new Random().nextInt(size);
|
int nb2 = new Random().nextInt(size);
|
||||||
|
while (nb2 == nb1) {
|
||||||
|
nb2 = new Random().nextInt(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
GameObject item1 = null;
|
GameObject item1 = null;
|
||||||
GameObject item2 = null;
|
GameObject item2 = null;
|
||||||
|
@@ -493,9 +493,16 @@ public class Worm extends GameObject {
|
|||||||
* |result == (0 <= new.getLocation() <= maxDistance) && (world.isPassable(new.getLocation(), radius))
|
* |result == (0 <= new.getLocation() <= maxDistance) && (world.isPassable(new.getLocation(), radius))
|
||||||
*/
|
*/
|
||||||
public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) {
|
public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) {
|
||||||
|
|
||||||
Coordinate currentLocation = getLocation();
|
Coordinate currentLocation = getLocation();
|
||||||
double radius = getRadius();
|
double radius = getRadius();
|
||||||
World world = getWorld();
|
World world = getWorld();
|
||||||
|
|
||||||
|
if (getWorld() == null) {
|
||||||
|
return Coordinate.create(currentLocation.getX() + maxDistance * cos(direction), currentLocation.getY() + maxDistance * sin(direction));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
double step = Math.sqrt(Math.pow(world.getLengthX() * cos(direction), 2) + Math.pow(world.getLengthY() * sin(direction), 2));
|
double step = Math.sqrt(Math.pow(world.getLengthX() * cos(direction), 2) + Math.pow(world.getLengthY() * sin(direction), 2));
|
||||||
if (step > radius) step = radius;
|
if (step > radius) step = radius;
|
||||||
Coordinate nextLoc = currentLocation;
|
Coordinate nextLoc = currentLocation;
|
||||||
@@ -1170,6 +1177,7 @@ public class Worm extends GameObject {
|
|||||||
public void checkEat() {
|
public void checkEat() {
|
||||||
|
|
||||||
World world = getWorld();
|
World world = getWorld();
|
||||||
|
if (world != null) {
|
||||||
List<Food> foodList = world.getFoodList();
|
List<Food> foodList = world.getFoodList();
|
||||||
for (Food food : foodList) {
|
for (Food food : foodList) {
|
||||||
if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) {
|
if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) {
|
||||||
@@ -1177,6 +1185,7 @@ public class Worm extends GameObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// region firing and projectiles
|
// region firing and projectiles
|
||||||
|
Reference in New Issue
Block a user