monster #20
@@ -4,7 +4,7 @@ import Entity.Position;
|
||||
//Är detta ett ok namn?
|
||||
public interface CanMove {
|
||||
|
||||
abstract boolean move();
|
||||
abstract boolean move(World world);
|
||||
|
||||
abstract boolean moveTo(Position position, World world);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package Monster;
|
||||
import Entity.*;
|
||||
|
||||
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 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 World.*;
|
||||
import java.util.*;
|
||||
import static java.util.Objects.*;
|
||||
|
||||
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));
|
||||
@@ -51,10 +52,15 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean move() {
|
||||
public boolean move(World world) {
|
||||
if (isDead()) {
|
||||
return false;
|
||||
}
|
||||
for (Position destination : validDestinations) {
|
||||
if (playerIsAtPosition(this.position, world)) {
|
||||
validDestinations.remove(destination);
|
||||
}
|
||||
}
|
||||
position = validDestinations.get(random.nextInt(validDestinations.size()));
|
||||
updateDestinations(position);
|
||||
|
||||
@@ -70,7 +76,7 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
||||
int y = position.y();
|
||||
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;
|
||||
}
|
||||
if (playerIsAtPosition(position, world)) {
|
||||
@@ -97,6 +103,9 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
||||
|
||||
private boolean playerIsAtPosition(Position position, World world) {
|
||||
List<Entity> entitiesAtPosition = world.getPositionEntityMap().get(position);
|
||||
if (isNull(entitiesAtPosition)) {
|
||||
return false;
|
||||
}
|
||||
for (Entity entity : entitiesAtPosition) {
|
||||
if (entity instanceof Player) {
|
||||
return true;
|
||||
@@ -104,4 +113,8 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
||||
}
|
||||
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<Entity, Position> entityMap = new HashMap<>();
|
||||
private String[][] map;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public World(int x, int y) {
|
||||
this.map = new String[(x * 2) + 1][y + 2];
|
||||
addWorldToMap();
|
||||
width = x;
|
||||
height = y;
|
||||
}
|
||||
|
||||
private void addWorldToMap() {
|
||||
@@ -111,6 +115,14 @@ public class World {
|
||||
return invertEntityMap();
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void changePosition(Entity Character, Position newPosition) {
|
||||
entityMap.put(Character, newPosition);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ public class MonsterTest {
|
||||
private Shade defaultShade;
|
||||
private Player defaultPlayer = mock(Player.class);
|
||||
private World defaultWorld = mock(World.class);
|
||||
private MapGenerator worldGenerator = new MapGenerator(4, 4);
|
||||
private MapGenerator worldGenerator = new MapGenerator(5, 5);
|
||||
|
||||
@BeforeEach
|
||||
void reset() {
|
||||
@@ -26,7 +26,7 @@ public class MonsterTest {
|
||||
@Test
|
||||
void monster_cannot_move_after_death() {
|
||||
defaultShade.kill();
|
||||
assertThat(false, equalTo(defaultShade.moveTo(new Position(0, 1), defaultWorld)));
|
||||
assertThat(false, equalTo(defaultShade.moveTo(new Position(1, 2), defaultWorld)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -58,23 +58,40 @@ public class MonsterTest {
|
||||
legalDestinations.addAll(move.findLegalDestinations(startingPosition));
|
||||
}
|
||||
|
||||
defaultShade.move();
|
||||
defaultShade.move(defaultWorld);
|
||||
assertThat(legalDestinations, hasItem(defaultShade.getPosition()));
|
||||
}
|
||||
|
||||
//Borde nog ha mer mocks här men tycker det är svårt med flera lager av dom...
|
||||
@Test
|
||||
void shade_wont_move_to_same_position_as_player() {
|
||||
void method_move_to_wont_move_shade_to_same_position_as_player() {
|
||||
worldGenerator.randomWorldGeneration();
|
||||
World world = worldGenerator.getWorld();
|
||||
|
||||
Position destination = new Position(1, 1);
|
||||
Position destination = new Position(2, 2);
|
||||
when(defaultPlayer.getPosition()).thenReturn(destination);
|
||||
world.addEntityToMap(defaultPlayer);
|
||||
|
||||
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
|
||||
void monster_cannot_do_anything_when_out_of_energy() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user