Documentatie World
This commit is contained in:
@@ -8,25 +8,24 @@ import java.util.stream.Collectors;
|
|||||||
public class World {
|
public class World {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param width
|
* @param width
|
||||||
* @param height
|
* @param height
|
||||||
* @param map
|
* @param map
|
||||||
*
|
*
|
||||||
* @post ...
|
* @post ...
|
||||||
* new.width = width
|
* |new.width = width
|
||||||
* @post ...
|
* @post ...
|
||||||
* new.height = height
|
* |new.height = height
|
||||||
* @post ...
|
* @post ...
|
||||||
* new.map = map
|
* |new.map = map
|
||||||
* @post ...
|
* @post ...
|
||||||
* new.legthX = width / map[0].length
|
* |new.legthX = width / map[0].length
|
||||||
* @post ...
|
* @post ...
|
||||||
* new.lengthY = height / map.length
|
* |new.lengthY = height / map.length
|
||||||
* @post ...
|
* @post ...
|
||||||
* new.game = false
|
* |new.game = false
|
||||||
* @post ...
|
* @post ...
|
||||||
* new.gameObjets = new HashSet<>()
|
* |new.gameObjets = new HashSet<>()
|
||||||
*/
|
*/
|
||||||
public World(double width, double height, boolean[][] map) {
|
public World(double width, double height, boolean[][] map) {
|
||||||
|
|
||||||
@@ -43,7 +42,7 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// region game
|
// region game (Total)
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
public boolean hasActiveGame() {
|
public boolean hasActiveGame() {
|
||||||
@@ -120,11 +119,10 @@ public class World {
|
|||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
|
||||||
// region Dimensions
|
// region Dimensions (Total)
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param dimension
|
* @param dimension
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
@@ -135,7 +133,6 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == width
|
* |result == width
|
||||||
*/
|
*/
|
||||||
@@ -144,7 +141,6 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == height
|
* |result == height
|
||||||
*/
|
*/
|
||||||
@@ -152,14 +148,8 @@ public class World {
|
|||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private final double width;
|
private final double width;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private final double height;
|
private final double height;
|
||||||
|
|
||||||
// ===================================================================================
|
// ===================================================================================
|
||||||
@@ -167,11 +157,10 @@ public class World {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// region map
|
// region map (Total)
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == map
|
* |result == map
|
||||||
*/
|
*/
|
||||||
@@ -180,7 +169,6 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* result = this.terminated
|
* result = this.terminated
|
||||||
*/
|
*/
|
||||||
@@ -189,6 +177,8 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @post ...
|
||||||
|
* |teams.clear()
|
||||||
* @post ...
|
* @post ...
|
||||||
* |this.terminated = true
|
* |this.terminated = true
|
||||||
*/
|
*/
|
||||||
@@ -202,32 +192,25 @@ public class World {
|
|||||||
this.terminated = true;
|
this.terminated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private final boolean[][] map;
|
private final boolean[][] map;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private boolean terminated = false;
|
private boolean terminated = false;
|
||||||
|
|
||||||
// ===================================================================================
|
// ===================================================================================
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region passable
|
// region passable (Total)
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param location
|
* @param location
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
* |false if:
|
* |true if:
|
||||||
* | Math.floor(location[0] / lengthX) >= getMap()[0].length ||
|
* | Math.floor(location[0] / lengthX) >= getMap()[0].length ||
|
||||||
* | location[0] < 0.0 || location[1] / lengthY >= getMap().length || location[1] < 0.0
|
* | location[0] < 0.0 || location[1] / lengthY >= getMap().length || location[1] < 0.0
|
||||||
* |various:
|
* |various:
|
||||||
* | result == this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)]
|
* | result == this.map[map.length - 1 - (int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)]
|
||||||
*/
|
*/
|
||||||
public boolean isPassable(double[] location) {
|
public boolean isPassable(double[] location) {
|
||||||
|
|
||||||
@@ -240,7 +223,6 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param location
|
* @param location
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
@@ -256,26 +238,27 @@ public class World {
|
|||||||
* @param radius
|
* @param radius
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == this.isPassable(location.toArray(), radius)
|
* |result == this.isPassable(location.toArray(), radius)
|
||||||
*/
|
*/
|
||||||
public boolean isPassable(Coordinate location, double radius) {
|
public boolean isPassable(Coordinate location, double radius) {
|
||||||
return this.isPassable(location.toArray(), radius);
|
return this.isPassable(location.toArray(), radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param center
|
* @param center
|
||||||
* @param radius
|
* @param radius
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
* |for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180)
|
* |for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180)
|
||||||
* | double lenX = center[0] + radius * Math.cos(i)
|
* | double lenX = center[0] + radius * Math.cos(i)
|
||||||
* | double lenY = center[1] + radius * Math.sin(i)
|
* | double lenY = center[1] + radius * Math.sin(i)
|
||||||
* | if (i < 1.58 && i > 1.57)
|
* | if (center[0] + radius == lenX)
|
||||||
* | lenY -= 0.0000000001
|
* | lenX -= 0.0000000001
|
||||||
* | else if (i < 0.79 && i > 0.78 )
|
* | if (center[1] + radius == lenY)
|
||||||
* | lenX -= 0.0000000001
|
* | lenY -= 0.0000000001
|
||||||
* |result == isPassable(lenX, lenY)
|
* | if (!isPassable(lenX, lenY))
|
||||||
|
* | result == false
|
||||||
|
* |result == true
|
||||||
*/
|
*/
|
||||||
public boolean isPassable(double[] center, double radius) {
|
public boolean isPassable(double[] center, double radius) {
|
||||||
|
|
||||||
@@ -304,7 +287,6 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == isAdjacent(Coordinate.create(center), radius)
|
* |result == isAdjacent(Coordinate.create(center), radius)
|
||||||
*/
|
*/
|
||||||
@@ -313,7 +295,6 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param center ...
|
* @param center ...
|
||||||
* @param radius ...
|
* @param radius ...
|
||||||
*
|
*
|
||||||
@@ -330,7 +311,6 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == this.lengtX
|
* |result == this.lengtX
|
||||||
*/
|
*/
|
||||||
@@ -339,26 +319,18 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == this.lengthY
|
* |result == this.lengthY
|
||||||
*/
|
*/
|
||||||
public double getLengthY() {
|
public double getLengthY() {
|
||||||
return this.lengthY;
|
return this.lengthY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private final double lengthX;
|
private final double lengthX;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private final double lengthY;
|
private final double lengthY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param x
|
* @param x
|
||||||
* @param y
|
* @param y
|
||||||
*
|
*
|
||||||
@@ -373,8 +345,6 @@ public class World {
|
|||||||
*/
|
*/
|
||||||
private boolean isPassable(double x, double y) {
|
private boolean isPassable(double x, double y) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int xCoord = (int) Math.floor(x / lengthX);
|
int xCoord = (int) Math.floor(x / lengthX);
|
||||||
int yCoord = map.length - 1 - (int) Math.floor(y / lengthY);
|
int yCoord = map.length - 1 - (int) Math.floor(y / lengthY);
|
||||||
|
|
||||||
@@ -392,11 +362,10 @@ public class World {
|
|||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
|
||||||
// region objects
|
// region objects (Defensive)
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == new ArrayList<>(getGameObjects())
|
* |result == new ArrayList<>(getGameObjects())
|
||||||
*/
|
*/
|
||||||
@@ -405,9 +374,16 @@ public class World {
|
|||||||
return new ArrayList<>(getGameObjects());
|
return new ArrayList<>(getGameObjects());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Set<Team> teams = new HashSet<>();
|
private Set<Team> teams = new HashSet<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param team
|
||||||
|
|
||||||
|
* @throws IllegalStateException ...
|
||||||
|
* |team.size() == 10
|
||||||
|
* @throws IllegalArgumentException ...
|
||||||
|
* |! teams.add(team)
|
||||||
|
*/
|
||||||
public void addTeam(Team team) {
|
public void addTeam(Team team) {
|
||||||
|
|
||||||
if (teams.size() == 10) throw new IllegalStateException("Maximum 10 teams");
|
if (teams.size() == 10) throw new IllegalStateException("Maximum 10 teams");
|
||||||
@@ -416,17 +392,14 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == teams
|
* |result == HashSet<>(teams)
|
||||||
*/
|
*/
|
||||||
public Set<Team> getAllTeams() {
|
public Set<Team> getAllTeams() {
|
||||||
|
|
||||||
return new HashSet<>(teams);
|
return new HashSet<>(teams);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |result == this.gameObjects
|
* |result == this.gameObjects
|
||||||
*/
|
*/
|
||||||
@@ -435,17 +408,30 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param obj
|
* @param obj
|
||||||
*
|
*
|
||||||
* @post obj.setWorld(this)
|
* @post ...
|
||||||
*
|
* |obj.setWorld(this)
|
||||||
* @throws IllegalStateException
|
* @post ...
|
||||||
* ...
|
* |if (!obj.isValidLocation(obj.getLocation()))
|
||||||
* |hasActiveGame()
|
* |obj.setWorld(null)
|
||||||
* @throws IllegalArgumentException
|
* @post ...
|
||||||
* ...
|
* |if (Worm.class.isInstance(obj) && (!obj.isValidLocation(obj.getLocation())
|
||||||
* |!getGameObjects().add(obj)
|
* | || !isAdjacent(obj.getLocation(), obj.getRadius()) ))
|
||||||
|
* |obj.setWorld(null)
|
||||||
|
* @post ...
|
||||||
|
* |getGameObjects().add(obj)
|
||||||
|
*
|
||||||
|
* @throws IllegalStateException ...
|
||||||
|
* |hasActiveGame()
|
||||||
|
* @throws IllegalArgumentException ...
|
||||||
|
* |obj == null || getGameObjects().contains(obj) || obj.isTerminated() ||
|
||||||
|
* | obj.getWorld() != null
|
||||||
|
* @throws IllegalArgumentException ...
|
||||||
|
* |! obj.isValidLocation(obj.getLocation())
|
||||||
|
* @throws IllegalArgumentException ...
|
||||||
|
* |Worm.class.isInstance(obj) && (!obj.isValidLocation(obj.getLocation()) ||
|
||||||
|
* | !isAdjacent(obj.getLocation(), obj.getRadius()))
|
||||||
*/
|
*/
|
||||||
public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException {
|
public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException {
|
||||||
if (hasActiveGame()) throw new IllegalStateException();
|
if (hasActiveGame()) throw new IllegalStateException();
|
||||||
@@ -466,9 +452,11 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param obj
|
* @param obj
|
||||||
*
|
*
|
||||||
|
* @post ...
|
||||||
|
* |obj.setWorld(null)
|
||||||
|
*
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException
|
||||||
* ...
|
* ...
|
||||||
* |!getGameObjects().remove(obj)
|
* |!getGameObjects().remove(obj)
|
||||||
@@ -480,11 +468,10 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param obj
|
* @param obj
|
||||||
*
|
*
|
||||||
* @return ...
|
* @return ...
|
||||||
* |getGameObjects().contains(obj)
|
* |result == getGameObjects().contains(obj)
|
||||||
*
|
*
|
||||||
* @throws NullPointerException
|
* @throws NullPointerException
|
||||||
*/
|
*/
|
||||||
@@ -493,10 +480,8 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |lis.add(worm) for each worm
|
* |result == getGameObjectsByClass(Worm.class)
|
||||||
* |result == list (ArrayList<>())
|
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Worm> getWormList() {
|
public List<Worm> getWormList() {
|
||||||
@@ -505,10 +490,8 @@ public class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return ...
|
* @return ...
|
||||||
* |list.add(food) for each food
|
* |result == getGameObjectsByClass(Food.class)
|
||||||
* |result == list (ArrayList<>())
|
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Food> getFoodList() {
|
public List<Food> getFoodList() {
|
||||||
@@ -516,14 +499,17 @@ public class World {
|
|||||||
return getGameObjectsByClass(Food.class);
|
return getGameObjectsByClass(Food.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param cl
|
||||||
|
* @param <T>
|
||||||
|
*
|
||||||
|
* @return ...
|
||||||
|
* |result == getGameObjects().stream().filter(cl::isInstance).map(cl::cast).collect(Collectors.toList())
|
||||||
|
*/
|
||||||
public <T extends GameObject> List<T> getGameObjectsByClass(Class<T> cl) {
|
public <T extends GameObject> List<T> getGameObjectsByClass(Class<T> cl) {
|
||||||
return getGameObjects().stream().filter(cl::isInstance).map(cl::cast).collect(Collectors.toList());
|
return getGameObjects().stream().filter(cl::isInstance).map(cl::cast).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private Set<GameObject> gameObjects;
|
private Set<GameObject> gameObjects;
|
||||||
|
|
||||||
|
|
||||||
@@ -531,7 +517,7 @@ public class World {
|
|||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
|
||||||
// region Wizard
|
// region Wizard (Defensive)
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
public void castSpell() {
|
public void castSpell() {
|
||||||
|
Reference in New Issue
Block a user