monster #20
@@ -1,7 +1,7 @@
|
|||||||
package Monster;
|
package Monster;
|
||||||
|
|
||||||
public enum Attacks {
|
public enum Attacks {
|
||||||
CHILL(3, 2);
|
CHILL(3, 2), STOMP(7, 5);
|
||||||
|
|
||||||
private int damage;
|
private int damage;
|
||||||
private int cost;
|
private int cost;
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ public interface CanMove {
|
|||||||
|
|
||||||
abstract boolean move(World world);
|
abstract boolean move(World world);
|
||||||
|
|
||||||
abstract boolean moveTo(Position position, World world);
|
abstract boolean moveTo(Position destination, World world);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ public enum MovementPatterns {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Position> findLegalDestinations(Position position) {
|
public Set<Position> findLegalDestinations(Position position) {
|
||||||
List<Position> legalDestinations = new ArrayList<>();
|
Set<Position> legalDestinations = new HashSet<>();
|
||||||
|
|
||||||
switch(id) {
|
switch(id) {
|
||||||
case 1:
|
case 1:
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
|||||||
|
|
||||||
public Shade() {
|
public Shade() {
|
||||||
super(MAX_HEALTH, MAX_ENERGY, Monster.DEFAULT_POSITION);
|
super(MAX_HEALTH, MAX_ENERGY, Monster.DEFAULT_POSITION);
|
||||||
updateDestinations(position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void heal() {
|
public void heal() {
|
||||||
@@ -56,35 +55,25 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
|||||||
if (isDead()) {
|
if (isDead()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (Position destination : validDestinations) {
|
updateDestinations(position, world);
|
||||||
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, world);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean moveTo(Position position, World world) {
|
public boolean moveTo(Position destination, World world) {
|
||||||
if (isDead()) {
|
if (isDead()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (positionIsOutOfBounds(destination, world)) {
|
||||||
int x = position.x();
|
|
||||||
int y = position.y();
|
|
||||||
int worldSize = world.getMap().length;
|
|
||||||
|
|
||||||
if (x >= worldSize || y >= worldSize || x < 1 || y < 1) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (playerIsAtPosition(position, world)) {
|
if (playerIsAtPosition(destination, world)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.position = position;
|
position = destination;
|
||||||
updateDestinations(this.position);
|
updateDestinations(position, world);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,10 +83,15 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateDestinations(Position position) {
|
private void updateDestinations(Position position, World world) {
|
||||||
validDestinations.clear();
|
validDestinations.clear();
|
||||||
for (MovementPatterns move : MOVES) {
|
for (MovementPatterns move : MOVES) {
|
||||||
validDestinations.addAll(move.findLegalDestinations(position));
|
Set<Position> destinations = move.findLegalDestinations(position);
|
||||||
|
for (Position destination : destinations) {
|
||||||
|
if (noPlayerIsAtPosition(destination, world) && positionIsInBounds(destination, world)) {
|
||||||
|
validDestinations.add(destination);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,4 +111,19 @@ public class Shade extends Monster implements CanMove, CanAttack {
|
|||||||
private boolean noPlayerIsAtPosition(Position position, World world) {
|
private boolean noPlayerIsAtPosition(Position position, World world) {
|
||||||
return !playerIsAtPosition(position, world);
|
return !playerIsAtPosition(position, world);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean positionIsInBounds(Position position, World world) {
|
||||||
|
int upperX = world.getWidth();
|
||||||
|
int lowerX = 1;
|
||||||
|
int upperY = world.getHeight();
|
||||||
|
int lowerY = 1;
|
||||||
|
int x = position.x();
|
||||||
|
int y = position.y();
|
||||||
|
|
||||||
|
return x > upperX || x < lowerX || y > upperY || y < lowerY;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean positionIsOutOfBounds(Position position, World world) {
|
||||||
|
return !positionIsInBounds(position, world);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import Entity.*;
|
import Entity.*;
|
||||||
import Monster.*;
|
import Monster.*;
|
||||||
import World.*;
|
import World.*;
|
||||||
|
import org.hamcrest.core.AllOf;
|
||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -14,10 +15,11 @@ import static org.mockito.Mockito.*;
|
|||||||
public class MonsterTest {
|
public class MonsterTest {
|
||||||
|
|
||||||
private Shade defaultShade;
|
private Shade defaultShade;
|
||||||
private Player defaultPlayer = mock(Player.class);
|
|
||||||
private World defaultWorld = mock(World.class);
|
|
||||||
private MapGenerator worldGenerator = new MapGenerator(5, 5);
|
private MapGenerator worldGenerator = new MapGenerator(5, 5);
|
||||||
|
|
||||||
|
private Player mockPlayer = mock(Player.class);
|
||||||
|
private World mockWorld = mock(World.class);
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void reset() {
|
void reset() {
|
||||||
defaultShade = new Shade(); //Återställer alla värden till default
|
defaultShade = new Shade(); //Återställer alla värden till default
|
||||||
@@ -26,13 +28,13 @@ 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(1, 2), defaultWorld)));
|
assertThat(false, equalTo(defaultShade.moveTo(new Position(1, 2), mockWorld)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void monster_cannot_attack_after_death() {
|
void monster_cannot_attack_after_death() {
|
||||||
defaultShade.kill();
|
defaultShade.kill();
|
||||||
assertThat(false, equalTo(defaultShade.performAttack(Attacks.CHILL, defaultPlayer)));
|
assertThat(false, equalTo(defaultShade.performAttack(Attacks.CHILL, mockPlayer)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -58,7 +60,7 @@ public class MonsterTest {
|
|||||||
legalDestinations.addAll(move.findLegalDestinations(startingPosition));
|
legalDestinations.addAll(move.findLegalDestinations(startingPosition));
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultShade.move(defaultWorld);
|
defaultShade.move(mockWorld);
|
||||||
assertThat(legalDestinations, hasItem(defaultShade.getPosition()));
|
assertThat(legalDestinations, hasItem(defaultShade.getPosition()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,27 +71,39 @@ public class MonsterTest {
|
|||||||
World world = worldGenerator.getWorld();
|
World world = worldGenerator.getWorld();
|
||||||
|
|
||||||
Position destination = new Position(2, 2);
|
Position destination = new Position(2, 2);
|
||||||
when(defaultPlayer.getPosition()).thenReturn(destination);
|
when(mockPlayer.getPosition()).thenReturn(destination);
|
||||||
world.addEntityToMap(defaultPlayer);
|
world.addEntityToMap(mockPlayer);
|
||||||
|
|
||||||
assertThat(defaultShade.moveTo(destination, world), equalTo(false));
|
assertThat(defaultShade.moveTo(destination, world), equalTo(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
void method_move_wont_move_shade_to_same_position_as_player() {
|
void method_move_wont_move_shade_to_same_position_as_player() {
|
||||||
worldGenerator.randomWorldGeneration();
|
worldGenerator.randomWorldGeneration();
|
||||||
World world = worldGenerator.getWorld();
|
World world = worldGenerator.getWorld();
|
||||||
|
|
||||||
Position destination = new Position(2, 2);
|
Position destination = new Position(2, 2);
|
||||||
when(defaultPlayer.getPosition()).thenReturn(destination);
|
when(mockPlayer.getPosition()).thenReturn(destination);
|
||||||
world.addEntityToMap(defaultPlayer);
|
world.addEntityToMap(mockPlayer);
|
||||||
|
|
||||||
defaultShade.move(world);
|
defaultShade.move(world);
|
||||||
assertThat(defaultShade.getPosition(), not(defaultPlayer.getPosition()));
|
assertThat(defaultShade.getPosition(), not(mockPlayer.getPosition()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Flera assertThat i samma. Farligt????
|
||||||
@Test
|
@Test
|
||||||
void method_move_places_monster_within_bounds() {
|
void method_move_places_monster_within_bounds() {
|
||||||
|
worldGenerator.randomWorldGeneration();
|
||||||
|
World world = worldGenerator.getWorld();
|
||||||
|
/*when(mockWorld.getWidth()).thenReturn(5);
|
||||||
|
when(mockWorld.getHeight()).thenReturn(5);*/
|
||||||
|
|
||||||
|
defaultShade.move(world);
|
||||||
|
int newX = defaultShade.getPosition().x();
|
||||||
|
int newY = defaultShade.getPosition().y();
|
||||||
|
|
||||||
|
boolean positionCheck = newX > 0 ;
|
||||||
|
assertThat(true, equalTo(positionCheck));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -111,4 +125,6 @@ public class MonsterTest {
|
|||||||
void use_of_attack_not_in_arsenal_is_rejected() {
|
void use_of_attack_not_in_arsenal_is_rejected() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user