monster #20
@@ -4,7 +4,7 @@ import Entity.Position;
|
|||||||
//Är detta ett ok namn?
|
//Är detta ett ok namn?
|
||||||
public interface CanMove {
|
public interface CanMove {
|
||||||
|
|
||||||
abstract boolean move();
|
abstract boolean move(World world);
|
||||||
|
|
||||||
abstract boolean moveTo(Position position, World world);
|
abstract boolean moveTo(Position position, World world);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package Monster;
|
|||||||
import Entity.*;
|
import Entity.*;
|
||||||
|
|
||||||
public abstract class Monster implements CanMove {
|
public abstract class Monster implements CanMove {
|
||||||
public static final Position DEFAULT_POSITION = new Position(0,0);
|
public static final Position DEFAULT_POSITION = new Position(1,1);
|
||||||
|
|
||||||
protected int health;
|
protected int health;
|
||||||
protected int energy; //Borde kanske beräknas genom en algoritm istället för att kunna sättas i konstruktorn... Så det alltid blir balanserat
|
protected int energy; //Borde kanske beräknas genom en algoritm istället för att kunna sättas i konstruktorn... Så det alltid blir balanserat
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package Monster;
|
|||||||
import Entity.*;
|
import Entity.*;
|
||||||
import World.*;
|
import World.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import static java.util.Objects.*;
|
||||||
|
|
||||||
public class Shade extends Monster implements CanMove, CanAttack {
|
public class Shade extends Monster implements CanMove, CanAttack {
|
||||||
public static final List<Biomes> HABITAT = Collections.unmodifiableList(Arrays.asList(Biomes.COAST, Biomes.FOREST, Biomes.GRASSLAND, Biomes.MOUNTAIN));
|
public static final List<Biomes> HABITAT = Collections.unmodifiableList(Arrays.asList(Biomes.COAST, Biomes.FOREST, Biomes.GRASSLAND, Biomes.MOUNTAIN));
|
||||||
@@ -51,10 +52,15 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean move() {
|
public boolean move(World world) {
|
||||||
if (isDead()) {
|
if (isDead()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
for (Position destination : validDestinations) {
|
||||||
|
if (playerIsAtPosition(this.position, world)) {
|
||||||
|
validDestinations.remove(destination);
|
||||||
|
}
|
||||||
|
}
|
||||||
position = validDestinations.get(random.nextInt(validDestinations.size()));
|
position = validDestinations.get(random.nextInt(validDestinations.size()));
|
||||||
updateDestinations(position);
|
updateDestinations(position);
|
||||||
|
|
||||||
@@ -70,7 +76,7 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
|||||||
int y = position.y();
|
int y = position.y();
|
||||||
int worldSize = world.getMap().length;
|
int worldSize = world.getMap().length;
|
||||||
|
|
||||||
if (x >= worldSize || y >= worldSize || x < 0 || y < 0) {
|
if (x >= worldSize || y >= worldSize || x < 1 || y < 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (playerIsAtPosition(position, world)) {
|
if (playerIsAtPosition(position, world)) {
|
||||||
@@ -97,6 +103,9 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
|||||||
|
|
||||||
private boolean playerIsAtPosition(Position position, World world) {
|
private boolean playerIsAtPosition(Position position, World world) {
|
||||||
List<Entity> entitiesAtPosition = world.getPositionEntityMap().get(position);
|
List<Entity> entitiesAtPosition = world.getPositionEntityMap().get(position);
|
||||||
|
if (isNull(entitiesAtPosition)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
for (Entity entity : entitiesAtPosition) {
|
for (Entity entity : entitiesAtPosition) {
|
||||||
if (entity instanceof Player) {
|
if (entity instanceof Player) {
|
||||||
return true;
|
return true;
|
||||||
@@ -104,4 +113,8 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean noPlayerIsAtPosition(Position position, World world) {
|
||||||
|
return !playerIsAtPosition(position, world);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,14 @@ public class World {
|
|||||||
private HashMap<Position, Tile> tileMap = new HashMap<>();
|
private HashMap<Position, Tile> tileMap = new HashMap<>();
|
||||||
private HashMap<Entity, Position> entityMap = new HashMap<>();
|
private HashMap<Entity, Position> entityMap = new HashMap<>();
|
||||||
private String[][] map;
|
private String[][] map;
|
||||||
|
private int width;
|
||||||
|
private int height;
|
||||||
|
|
||||||
public World(int x, int y) {
|
public World(int x, int y) {
|
||||||
this.map = new String[(x * 2) + 1][y + 2];
|
this.map = new String[(x * 2) + 1][y + 2];
|
||||||
addWorldToMap();
|
addWorldToMap();
|
||||||
|
width = x;
|
||||||
|
height = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addWorldToMap() {
|
private void addWorldToMap() {
|
||||||
@@ -111,6 +115,14 @@ public class World {
|
|||||||
return invertEntityMap();
|
return invertEntityMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
public void changePosition(Entity Character, Position newPosition) {
|
public void changePosition(Entity Character, Position newPosition) {
|
||||||
entityMap.put(Character, newPosition);
|
entityMap.put(Character, newPosition);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class MonsterTest {
|
|||||||
private Shade defaultShade;
|
private Shade defaultShade;
|
||||||
private Player defaultPlayer = mock(Player.class);
|
private Player defaultPlayer = mock(Player.class);
|
||||||
private World defaultWorld = mock(World.class);
|
private World defaultWorld = mock(World.class);
|
||||||
private MapGenerator worldGenerator = new MapGenerator(4, 4);
|
private MapGenerator worldGenerator = new MapGenerator(5, 5);
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void reset() {
|
void reset() {
|
||||||
@@ -26,7 +26,7 @@ public class MonsterTest {
|
|||||||
@Test
|
@Test
|
||||||
void monster_cannot_move_after_death() {
|
void monster_cannot_move_after_death() {
|
||||||
defaultShade.kill();
|
defaultShade.kill();
|
||||||
assertThat(false, equalTo(defaultShade.moveTo(new Position(0, 1), defaultWorld)));
|
assertThat(false, equalTo(defaultShade.moveTo(new Position(1, 2), defaultWorld)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -58,23 +58,40 @@ public class MonsterTest {
|
|||||||
legalDestinations.addAll(move.findLegalDestinations(startingPosition));
|
legalDestinations.addAll(move.findLegalDestinations(startingPosition));
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultShade.move();
|
defaultShade.move(defaultWorld);
|
||||||
assertThat(legalDestinations, hasItem(defaultShade.getPosition()));
|
assertThat(legalDestinations, hasItem(defaultShade.getPosition()));
|
||||||
}
|
}
|
||||||
|
|
||||||
//Borde nog ha mer mocks här men tycker det är svårt med flera lager av dom...
|
//Borde nog ha mer mocks här men tycker det är svårt med flera lager av dom...
|
||||||
@Test
|
@Test
|
||||||
void shade_wont_move_to_same_position_as_player() {
|
void method_move_to_wont_move_shade_to_same_position_as_player() {
|
||||||
worldGenerator.randomWorldGeneration();
|
worldGenerator.randomWorldGeneration();
|
||||||
World world = worldGenerator.getWorld();
|
World world = worldGenerator.getWorld();
|
||||||
|
|
||||||
Position destination = new Position(1, 1);
|
Position destination = new Position(2, 2);
|
||||||
when(defaultPlayer.getPosition()).thenReturn(destination);
|
when(defaultPlayer.getPosition()).thenReturn(destination);
|
||||||
world.addEntityToMap(defaultPlayer);
|
world.addEntityToMap(defaultPlayer);
|
||||||
|
|
||||||
assertThat(defaultShade.moveTo(destination, world), equalTo(false));
|
assertThat(defaultShade.moveTo(destination, world), equalTo(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void method_move_wont_move_shade_to_same_position_as_player() {
|
||||||
|
worldGenerator.randomWorldGeneration();
|
||||||
|
World world = worldGenerator.getWorld();
|
||||||
|
|
||||||
|
Position destination = new Position(2, 2);
|
||||||
|
when(defaultPlayer.getPosition()).thenReturn(destination);
|
||||||
|
world.addEntityToMap(defaultPlayer);
|
||||||
|
|
||||||
|
defaultShade.move(world);
|
||||||
|
assertThat(defaultShade.getPosition(), not(defaultPlayer.getPosition()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void method_move_places_monster_within_bounds() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void monster_cannot_do_anything_when_out_of_energy() {
|
void monster_cannot_do_anything_when_out_of_energy() {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user