mass update
This commit is contained in:
@@ -1,73 +1,18 @@
|
|||||||
package worms.facade;
|
package worms.facade;
|
||||||
|
|
||||||
import worms.model.Food;
|
import worms.model.*;
|
||||||
import worms.model.Team;
|
|
||||||
import worms.model.World;
|
|
||||||
import worms.model.Worm;
|
|
||||||
import worms.util.IllegalNameException;
|
import worms.util.IllegalNameException;
|
||||||
import worms.util.ModelException;
|
import worms.util.ModelException;
|
||||||
import worms.util.Coordinate;
|
import worms.util.Coordinate;
|
||||||
import worms.util.MustNotImplementException;
|
import worms.util.MustNotImplementException;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class Facade implements IFacade {
|
public class Facade implements IFacade {
|
||||||
// /**
|
|
||||||
// * Create and return a new worm that is positioned at the given location, looks
|
|
||||||
// * in the given direction, has the given radius and the given name.
|
|
||||||
// *
|
|
||||||
// * @param location
|
|
||||||
// * An array containing the x-coordinate of the position of the new
|
|
||||||
// * worm followed by the y-coordinate of the position of the new worm
|
|
||||||
// * (in meter)
|
|
||||||
// * @param direction
|
|
||||||
// * The direction of the new worm (in radians)
|
|
||||||
// * @param radius
|
|
||||||
// * The radius of the new worm (in meter)
|
|
||||||
// * @param name
|
|
||||||
// * the name of the new worm
|
|
||||||
// * @post the new worm has the given location, direction, name and radius
|
|
||||||
// * |new Worm(Coordinate.create(location), direction, name, radius)
|
|
||||||
// * @throws ModelException
|
|
||||||
// * the worm throws an IllegalArgumentException
|
|
||||||
// * |catch(IllegalArgumentException e)
|
|
||||||
// */
|
|
||||||
// @Override
|
|
||||||
// public Worm createWorm(double[] location, double direction, double radius, String name) throws ModelException {
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// return new Worm(Coordinate.create(location), direction, name, radius);
|
|
||||||
// }
|
|
||||||
// catch(IllegalArgumentException e) {
|
|
||||||
// throw new ModelException(e);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Moves the given worm by the given number of steps.
|
|
||||||
// *
|
|
||||||
// * @param worm
|
|
||||||
// * the worm who is going to move
|
|
||||||
// * @param nbSteps
|
|
||||||
// * the number of steps the worm is going to take
|
|
||||||
// * @post the worm has taken the given number of steps
|
|
||||||
// * |worm.move(nbSteps)
|
|
||||||
// * @throws ModelException
|
|
||||||
// * the worm throws an IllegalArgumentException
|
|
||||||
// * |catch(IllegalArgumentException e)
|
|
||||||
// */
|
|
||||||
// @Override
|
|
||||||
// public void move(Worm worm, int nbSteps) throws ModelException {
|
|
||||||
// try {
|
|
||||||
// worm.move(nbSteps);
|
|
||||||
// }
|
|
||||||
// catch(IllegalArgumentException e) {
|
|
||||||
// throw new ModelException(e);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turns the given worm by the given angle.
|
* Turns the given worm by the given angle.
|
||||||
@@ -82,7 +27,12 @@ public class Facade implements IFacade {
|
|||||||
@Override
|
@Override
|
||||||
public void turn(Worm worm, double angle) throws ModelException {
|
public void turn(Worm worm, double angle) throws ModelException {
|
||||||
|
|
||||||
worm.turn(angle);
|
try {
|
||||||
|
worm.turn(angle);
|
||||||
|
}
|
||||||
|
catch (AssertionError e) {
|
||||||
|
throw new ModelException("");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,7 +56,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(e);
|
throw new ModelException("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +67,12 @@ public class Facade implements IFacade {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void move(Worm worm) throws ModelException {
|
public void move(Worm worm) throws ModelException {
|
||||||
worm.move();
|
|
||||||
|
try {
|
||||||
|
worm.move();
|
||||||
|
} catch(IllegalArgumentException e) {
|
||||||
|
throw new ModelException("");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -456,7 +411,7 @@ public class Facade implements IFacade {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean hasAsWorm(World world, Worm worm) throws ModelException {
|
public boolean hasAsWorm(World world, Worm worm) throws ModelException {
|
||||||
return world.hasAsWorm(worm);
|
return world.hasAsGameObject(worm);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -467,7 +422,7 @@ 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 {
|
||||||
world.addWorm(worm);
|
world.add(worm);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -478,7 +433,7 @@ 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 {
|
||||||
world.removeWorm(worm);
|
world.remove(worm);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -488,6 +443,7 @@ public class Facade implements IFacade {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<Worm> getAllWorms(World world) throws ModelException {
|
public List<Worm> getAllWorms(World world) throws ModelException {
|
||||||
|
|
||||||
return world.getWormList();
|
return world.getWormList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -499,7 +455,7 @@ public class Facade implements IFacade {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean hasAsFood(World world, Food food) throws ModelException {
|
public boolean hasAsFood(World world, Food food) throws ModelException {
|
||||||
return world.hasAsFood(food);
|
return world.hasAsGameObject(food);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -510,7 +466,7 @@ 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 {
|
||||||
world.addFood(food);
|
world.add(food);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -521,7 +477,7 @@ 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 {
|
||||||
world.removeFood(food);
|
world.remove(food);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -832,7 +788,11 @@ 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 {
|
||||||
return new Team(name);
|
try {
|
||||||
|
return new Team(name);
|
||||||
|
} catch(IllegalNameException e) {
|
||||||
|
throw new ModelException(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,14 +1,13 @@
|
|||||||
package worms.model;
|
package worms.model;
|
||||||
|
|
||||||
|
import worms.util.Coordinate;
|
||||||
|
|
||||||
public class Food extends GameObject {
|
public class Food extends GameObject {
|
||||||
|
|
||||||
public Food(World world, double[] location) {
|
public Food(World world, double[] location) {
|
||||||
setWorld(world);
|
super(world, location, 0.2);
|
||||||
setLocation(location);
|
|
||||||
|
|
||||||
setRadius(0.2);
|
|
||||||
final double rho = 150;
|
final double rho = 150;
|
||||||
setMass(getRadius(), rho);
|
setMass(getRadius(), rho);
|
||||||
world.addFood(this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package worms.model;
|
package worms.model;
|
||||||
|
|
||||||
|
import be.kuleuven.cs.som.annotate.Raw;
|
||||||
import worms.util.Coordinate;
|
import worms.util.Coordinate;
|
||||||
|
|
||||||
import static java.lang.Math.PI;
|
import static java.lang.Math.PI;
|
||||||
@@ -8,6 +9,12 @@ import static java.lang.Math.round;
|
|||||||
|
|
||||||
public abstract class GameObject {
|
public abstract class GameObject {
|
||||||
|
|
||||||
|
GameObject(World world, double[] location, double radius) {
|
||||||
|
setWorld(world);
|
||||||
|
world.add(this);
|
||||||
|
setLocation(location);
|
||||||
|
setRadius(radius);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -35,8 +42,6 @@ public abstract class GameObject {
|
|||||||
|
|
||||||
private World world;
|
private World world;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
@@ -46,10 +51,11 @@ public abstract class GameObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Terminate the worm
|
* Terminate the object
|
||||||
*/
|
*/
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
this.terminated = true;
|
this.terminated = true;
|
||||||
|
getWorld().remove(this);
|
||||||
}
|
}
|
||||||
private boolean terminated = false;
|
private boolean terminated = false;
|
||||||
|
|
||||||
@@ -112,9 +118,24 @@ public abstract class GameObject {
|
|||||||
return this.radius;
|
return this.radius;
|
||||||
}
|
}
|
||||||
void setRadius(double radius) {
|
void setRadius(double radius) {
|
||||||
|
if (!canHaveAsRadius(radius)) throw new IllegalArgumentException();
|
||||||
this.radius = radius;
|
this.radius = radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the given radius is a valid radius for the worm
|
||||||
|
*
|
||||||
|
* @param radius
|
||||||
|
* the radius to check
|
||||||
|
* @return True if and only if the radius is bigger then the minimum radius
|
||||||
|
* (or equal) and the radius is a number
|
||||||
|
* |result == (radius >= getMinRadius() && !Double.isNaN(radius))
|
||||||
|
*/
|
||||||
|
@Raw
|
||||||
|
private boolean canHaveAsRadius(double radius) {
|
||||||
|
return !Double.isNaN(radius) && radius > 0;
|
||||||
|
}
|
||||||
|
|
||||||
boolean isValidLocation(Coordinate location) {
|
boolean isValidLocation(Coordinate location) {
|
||||||
double radius = getRadius();
|
double radius = getRadius();
|
||||||
|
|
||||||
|
@@ -3,6 +3,7 @@ package worms.model;
|
|||||||
import worms.util.Coordinate;
|
import worms.util.Coordinate;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class World {
|
public class World {
|
||||||
|
|
||||||
@@ -13,12 +14,11 @@ public class World {
|
|||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.wormList = new ArrayList<>();
|
|
||||||
this.foodSet = new HashSet<>();
|
|
||||||
|
|
||||||
this.lengthX = width / map[0].length;
|
this.lengthX = width / map[0].length;
|
||||||
this.lengthY = height / map.length;
|
this.lengthY = height / map.length;
|
||||||
this.game = false;
|
this.game = false;
|
||||||
|
this.gameObjects = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -38,7 +38,11 @@ public class World {
|
|||||||
this.game = false;
|
this.game = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void activateNextWorm() {
|
public void activateNextWorm() throws IllegalStateException {
|
||||||
|
|
||||||
|
if (getWormList().size() == 0) {
|
||||||
|
throw new IllegalStateException("No worms");
|
||||||
|
}
|
||||||
|
|
||||||
this.activeWorm++;
|
this.activeWorm++;
|
||||||
if (this.activeWorm == getWormList().size()) {
|
if (this.activeWorm == getWormList().size()) {
|
||||||
@@ -52,6 +56,7 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getWinner() {
|
public String getWinner() {
|
||||||
|
|
||||||
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) {
|
||||||
@@ -139,6 +144,10 @@ public class World {
|
|||||||
return this.isPassable(location.toArray());
|
return this.isPassable(location.toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPassable(Coordinate location, double radius) {
|
||||||
|
return this.isPassable(location.toArray(), radius);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isPassable(double[] center, double radius) {
|
public boolean isPassable(double[] center, double radius) {
|
||||||
|
|
||||||
for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180) {
|
for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180) {
|
||||||
@@ -158,13 +167,22 @@ public class World {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return ...
|
||||||
|
*/
|
||||||
|
public boolean isAdjacent(double[] center, double radius) {
|
||||||
|
return isAdjacent(Coordinate.create(center), radius);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param center ...
|
* @param center ...
|
||||||
* @param radius ...
|
* @param radius ...
|
||||||
* @return ...
|
* @return ...
|
||||||
*/
|
*/
|
||||||
public boolean isAdjacent(double[] center, double radius) {
|
public boolean isAdjacent(Coordinate center, double radius) {
|
||||||
|
|
||||||
double maxDistance = radius * 0.1;
|
double maxDistance = radius * 0.1;
|
||||||
double length = lengthX;
|
double length = lengthX;
|
||||||
@@ -213,10 +231,8 @@ public class World {
|
|||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
public Collection<Object> getAllItems() {
|
public Collection<Object> getAllItems() {
|
||||||
Set<Object> all = new HashSet<Object>();
|
|
||||||
all.add(getWormList());
|
return new ArrayList<>(getGameObjects());
|
||||||
all.add(getFoodSet());
|
|
||||||
return all;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Team> getAllTeams() {
|
public Set<Team> getAllTeams() {
|
||||||
@@ -228,47 +244,46 @@ public class World {
|
|||||||
return teams;
|
return teams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<GameObject> getGameObjects() {
|
||||||
|
return this.gameObjects;
|
||||||
public boolean hasAsWorm(Worm worm) throws NullPointerException {
|
|
||||||
return getWormList().contains(worm);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addWorm(Worm worm) throws IllegalArgumentException, NullPointerException, IllegalStateException {
|
public void add(GameObject obj) throws NullPointerException {
|
||||||
|
if (hasActiveGame()) throw new IllegalStateException();
|
||||||
|
if (!getGameObjects().add(obj)) throw new IllegalArgumentException();
|
||||||
|
obj.setWorld(this);
|
||||||
|
}
|
||||||
|
public void remove(GameObject obj) throws IllegalArgumentException {
|
||||||
|
|
||||||
if (hasActiveGame() || hasAsWorm(worm)) throw new IllegalStateException();
|
if (!getGameObjects().remove(obj)) throw new IllegalArgumentException();
|
||||||
getWormList().add(worm);
|
|
||||||
worm.setWorld(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeWorm(Worm worm) throws IllegalArgumentException, NullPointerException {
|
public boolean hasAsGameObject(GameObject obj) throws NullPointerException {
|
||||||
if (!getWormList().remove(worm)) throw new IllegalArgumentException();
|
return getGameObjects().contains(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Worm> getWormList() {
|
public List<Worm> getWormList() {
|
||||||
return this.wormList;
|
|
||||||
|
List<Worm> list = new ArrayList<>();
|
||||||
|
for (GameObject x : getGameObjects()) {
|
||||||
|
if (x.getClass().equals(Worm.class)) {
|
||||||
|
list.add((Worm) x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<GameObject> getGameObjectList(Class cl) {
|
||||||
public boolean hasAsFood(Food food) throws NullPointerException {
|
List<GameObject> list = new ArrayList<>();
|
||||||
return getFoodSet().contains(food);
|
for (GameObject x : getGameObjects()) {
|
||||||
}
|
if (x.getClass().equals(cl)) {
|
||||||
public void addFood(Food food) throws IllegalArgumentException, NullPointerException, IllegalStateException {
|
list.add(x);
|
||||||
|
}
|
||||||
if (hasActiveGame()) throw new IllegalStateException();
|
}
|
||||||
if (!getFoodSet().add(food)) throw new IllegalArgumentException();
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeFood(Food food) throws IllegalArgumentException, NullPointerException {
|
private Set<GameObject> gameObjects;
|
||||||
if (!getFoodSet().remove(food)) throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Set<Food> getFoodSet() {
|
|
||||||
return this.foodSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Worm> wormList;
|
|
||||||
private Set<Food> foodSet;
|
|
||||||
|
|
||||||
|
|
||||||
// ===================================================================================
|
// ===================================================================================
|
||||||
|
@@ -7,6 +7,7 @@ import worms.util.IllegalNameException;
|
|||||||
import static java.lang.Math.*;
|
import static java.lang.Math.*;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class with the specifications of the worm
|
* A class with the specifications of the worm
|
||||||
@@ -40,14 +41,16 @@ public class Worm extends GameObject {
|
|||||||
/**
|
/**
|
||||||
*initialize the new worm with given location, orientation, name and radius
|
*initialize the new worm with given location, orientation, name and radius
|
||||||
*
|
*
|
||||||
|
* @param world
|
||||||
|
* The world this worm belongs to
|
||||||
* @param location
|
* @param location
|
||||||
* the location for the new worm
|
* the location for the new worm
|
||||||
* @param orientation
|
|
||||||
* the orientation for the new worm
|
|
||||||
* @param name
|
* @param name
|
||||||
* the name for the new worm
|
* the name for the new worm
|
||||||
* @param radius
|
* @param radius
|
||||||
* the radius for the new worm
|
* the radius for the new worm
|
||||||
|
* @param team
|
||||||
|
* The team of the new worm
|
||||||
* @post the new location of the worm is equal to the given location
|
* @post the new location of the worm is equal to the given location
|
||||||
* |new.getLocation() == location
|
* |new.getLocation() == location
|
||||||
* @post the new orientation of the worm is equal to the given orientation
|
* @post the new orientation of the worm is equal to the given orientation
|
||||||
@@ -58,9 +61,10 @@ public class Worm extends GameObject {
|
|||||||
* |new.getRadius() == radius
|
* |new.getRadius() == radius
|
||||||
*/
|
*/
|
||||||
@Raw
|
@Raw
|
||||||
public Worm(Coordinate location, double orientation, String name, double radius) {
|
public Worm(World world, double[] location, double direction, double radius, String name, Team team) {
|
||||||
|
|
||||||
|
this(world, location, direction, name, radius, 0.25, team);
|
||||||
|
|
||||||
this(location, orientation, name, radius, 0.25);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,12 +105,10 @@ public class Worm extends GameObject {
|
|||||||
* |isValidName(name)
|
* |isValidName(name)
|
||||||
*/
|
*/
|
||||||
@Raw
|
@Raw
|
||||||
public Worm(Coordinate location, double orientation, String name, double radius, double minRadius)
|
public Worm(World world, double[] location, double orientation, String name, double radius, double minRadius, Team team)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
|
super(world, location, radius);
|
||||||
if (!isValidLocation(location))
|
setTeam(team);
|
||||||
throw new IllegalArgumentException("Illegal value for location");
|
|
||||||
setLocation(location);
|
|
||||||
|
|
||||||
setOrientation(orientation);
|
setOrientation(orientation);
|
||||||
|
|
||||||
@@ -114,9 +116,7 @@ public class Worm extends GameObject {
|
|||||||
throw new IllegalArgumentException("Invalid min radius");
|
throw new IllegalArgumentException("Invalid min radius");
|
||||||
this.minRadius = minRadius;
|
this.minRadius = minRadius;
|
||||||
|
|
||||||
if (!canHaveAsRadius(radius))
|
if (this.radius < this.minRadius) throw new IllegalArgumentException("Invalid radius");
|
||||||
throw new IllegalArgumentException("Invalid radius");
|
|
||||||
setRadius(radius);
|
|
||||||
|
|
||||||
setActionPoints(getMaxActionPoints());
|
setActionPoints(getMaxActionPoints());
|
||||||
|
|
||||||
@@ -124,19 +124,11 @@ public class Worm extends GameObject {
|
|||||||
if (validName != -1)
|
if (validName != -1)
|
||||||
throw new IllegalNameException(validName, name);
|
throw new IllegalNameException(validName, name);
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|
||||||
long startHitPoints = 1000 + (new Random().nextLong() * (2000 - 1000));
|
long startHitPoints = ThreadLocalRandom.current().nextLong(1001, 2000);
|
||||||
setHitPoints(startHitPoints);
|
setHitPoints(startHitPoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Worm(World world, double[] location, double direction, double radius, String name, Team team) {
|
|
||||||
|
|
||||||
this(Coordinate.create(location), direction, name, radius, 0.25);
|
|
||||||
setWorld(world);
|
|
||||||
setTeam(team);
|
|
||||||
world.addWorm(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
@@ -219,7 +211,7 @@ public class Worm extends GameObject {
|
|||||||
* the given radius is not a valid radius for any worm
|
* the given radius is not a valid radius for any worm
|
||||||
* |! canHaveAsMinRadius(radius)
|
* |! canHaveAsMinRadius(radius)
|
||||||
*/
|
*/
|
||||||
@Raw
|
@Raw @Override
|
||||||
public void setRadius(double radius) throws IllegalArgumentException {
|
public void setRadius(double radius) throws IllegalArgumentException {
|
||||||
if (!canHaveAsRadius(radius))
|
if (!canHaveAsRadius(radius))
|
||||||
throw new IllegalArgumentException("Invalid radius");
|
throw new IllegalArgumentException("Invalid radius");
|
||||||
@@ -247,7 +239,7 @@ public class Worm extends GameObject {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether the given radius is a valid radius for the worm
|
* Check whether the given radius is a valid radius for the worm
|
||||||
*
|
*
|
||||||
* @param radius
|
* @param radius
|
||||||
* the radius to check
|
* the radius to check
|
||||||
* @return True if and only if the radius is bigger then the minimum radius
|
* @return True if and only if the radius is bigger then the minimum radius
|
||||||
@@ -469,9 +461,6 @@ public class Worm extends GameObject {
|
|||||||
/**
|
/**
|
||||||
* move the worm for the given number of steps
|
* move the worm for the given number of steps
|
||||||
*
|
*
|
||||||
* @param numberSteps
|
|
||||||
* the number of steps the worm should take
|
|
||||||
*
|
|
||||||
* @post the x-coordinate of the new location of the worm should be the location of
|
* @post the x-coordinate of the new location of the worm should be the location of
|
||||||
* the old x-coordinate plus the number of steps multiplied with the distance
|
* the old x-coordinate plus the number of steps multiplied with the distance
|
||||||
* that the x-coordinate should move (distance is equal to the radius multiplied
|
* that the x-coordinate should move (distance is equal to the radius multiplied
|
||||||
@@ -495,7 +484,6 @@ public class Worm extends GameObject {
|
|||||||
*/
|
*/
|
||||||
public void move() throws IllegalArgumentException {
|
public void move() throws IllegalArgumentException {
|
||||||
|
|
||||||
|
|
||||||
double newDirection = getFurthestLocationDirection();
|
double newDirection = getFurthestLocationDirection();
|
||||||
Coordinate newLocation = getFurthestLocationInDirection(newDirection, this.getRadius());
|
Coordinate newLocation = getFurthestLocationInDirection(newDirection, this.getRadius());
|
||||||
double distance = getDistance(this.getLocation(), newLocation);
|
double distance = getDistance(this.getLocation(), newLocation);
|
||||||
@@ -628,7 +616,7 @@ public class Worm extends GameObject {
|
|||||||
double currentAngle = getOrientation();
|
double currentAngle = getOrientation();
|
||||||
return 0 <= angle + currentAngle && angle + currentAngle < (2 * PI) &&
|
return 0 <= angle + currentAngle && angle + currentAngle < (2 * PI) &&
|
||||||
!Double.isNaN(angle) &&
|
!Double.isNaN(angle) &&
|
||||||
getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0;
|
getActionPoints() - (long) Math.abs(ceil(toDegrees(angle) / 6)) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
@@ -668,6 +656,10 @@ public class Worm extends GameObject {
|
|||||||
|
|
||||||
setLocation(Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY()));
|
setLocation(Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY()));
|
||||||
setActionPoints(0);
|
setActionPoints(0);
|
||||||
|
|
||||||
|
if (!getWorld().isAdjacent(getLocation(), getRadius())) {
|
||||||
|
fall();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -800,7 +792,7 @@ public class Worm extends GameObject {
|
|||||||
*/
|
*/
|
||||||
@Raw
|
@Raw
|
||||||
private void setHitPoints(long hitPoints) {
|
private void setHitPoints(long hitPoints) {
|
||||||
if (hitPoints <= 0)
|
if (hitPoints < 0)
|
||||||
terminate();
|
terminate();
|
||||||
this.hitPoints = hitPoints;
|
this.hitPoints = hitPoints;
|
||||||
}
|
}
|
||||||
@@ -833,26 +825,35 @@ public class Worm extends GameObject {
|
|||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
public void fall() {
|
public void fall() {
|
||||||
double[] center = {getLocation().getX(), getLocation().getY()};
|
double heigth = getWorld().getHeight() - getRadius();
|
||||||
Coordinate oldLocation = getLocation();
|
Coordinate oldLocation = getLocation();
|
||||||
double endY = getLocation().getY();
|
if (canFall()) {
|
||||||
if (canFall()) {
|
for (double y = oldLocation.getY(); y <= heigth; y = y - 0.01) {
|
||||||
for (double y = oldLocation.getY(); y <= (getWorld().getHeight() + 1); y--) {
|
Coordinate newLoc = Coordinate.create(oldLocation.getX(), y);
|
||||||
if (y >= (getWorld().getHeight() + 1)) {
|
|
||||||
terminate();
|
|
||||||
}
|
|
||||||
double[] newLoc = {oldLocation.getX(), y};
|
|
||||||
setLocation(newLoc);
|
|
||||||
if (! canFall()) {
|
|
||||||
endY = y;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
center[1] = y;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long cost = 3 * (long) Math.floor(oldLocation.getY() - endY);
|
if (y - radius < 0) {
|
||||||
decreaseHitPoints(cost);
|
terminate();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!getWorld().isAdjacent(newLoc, radius)) {
|
||||||
|
setLocation(newLoc);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// for (double i = y; i >= y - 1; i = i - 0.1) {
|
||||||
|
// double[] newLocation = {oldLocation.getX(), i};
|
||||||
|
// if (! getWorld().isPassable(newLocation, getRadius())) {
|
||||||
|
// setLocation(newLocation);
|
||||||
|
// endY = i;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long cost = 3 * (long) Math.floor(oldLocation.getY() - getLocation().getY());
|
||||||
|
decreaseHitPoints(cost);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canFall() {
|
public boolean canFall() {
|
||||||
|
Reference in New Issue
Block a user