equipments #22

Merged
viud3133 merged 40 commits from equipments into main 2025-10-30 12:09:43 +01:00
5 changed files with 59 additions and 34 deletions
Showing only changes of commit 1430e08777 - Show all commits

View File

@@ -1,7 +1,7 @@
package Monster;
public enum Attacks {
CHILL(3, 2);
CHILL(3, 2), STOMP(7, 5);
private int damage;
private int cost;

View File

@@ -6,5 +6,5 @@ public interface CanMove {
abstract boolean move(World world);
abstract boolean moveTo(Position position, World world);
abstract boolean moveTo(Position destination, World world);
}

View File

@@ -14,8 +14,8 @@ public enum MovementPatterns {
this.id = id;
}
public List<Position> findLegalDestinations(Position position) {
List<Position> legalDestinations = new ArrayList<>();
public Set<Position> findLegalDestinations(Position position) {
Set<Position> legalDestinations = new HashSet<>();
switch(id) {
case 1:

View File

@@ -18,7 +18,6 @@ public class Shade extends Monster implements CanMove, CanAttack {
public Shade() {
super(MAX_HEALTH, MAX_ENERGY, Monster.DEFAULT_POSITION);
updateDestinations(position);
}
public void heal() {
@@ -56,35 +55,25 @@ public class Shade extends Monster implements CanMove, CanAttack {
if (isDead()) {
return false;
}
for (Position destination : validDestinations) {
if (playerIsAtPosition(this.position, world)) {
validDestinations.remove(destination);
}
}
updateDestinations(position, world);
position = validDestinations.get(random.nextInt(validDestinations.size()));
updateDestinations(position);
updateDestinations(position, world);
return true;
}
public boolean moveTo(Position position, World world) {
public boolean moveTo(Position destination, World world) {
if (isDead()) {
return false;
}
int x = position.x();
int y = position.y();
int worldSize = world.getMap().length;
if (x >= worldSize || y >= worldSize || x < 1 || y < 1) {
if (positionIsOutOfBounds(destination, world)) {
return false;
}
if (playerIsAtPosition(position, world)) {
if (playerIsAtPosition(destination, world)) {
return false;
}
this.position = position;
updateDestinations(this.position);
position = destination;
updateDestinations(position, world);
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();
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) {
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);
}
}

View File

@@ -1,6 +1,7 @@
import Entity.*;
import Monster.*;
import World.*;
import org.hamcrest.core.AllOf;
import org.junit.jupiter.api.*;
import java.util.*;
@@ -14,10 +15,11 @@ import static org.mockito.Mockito.*;
public class MonsterTest {
private Shade defaultShade;
private Player defaultPlayer = mock(Player.class);
private World defaultWorld = mock(World.class);
private MapGenerator worldGenerator = new MapGenerator(5, 5);
private Player mockPlayer = mock(Player.class);
private World mockWorld = mock(World.class);
@BeforeEach
void reset() {
defaultShade = new Shade(); //Återställer alla värden till default
@@ -26,13 +28,13 @@ public class MonsterTest {
@Test
void monster_cannot_move_after_death() {
defaultShade.kill();
assertThat(false, equalTo(defaultShade.moveTo(new Position(1, 2), defaultWorld)));
assertThat(false, equalTo(defaultShade.moveTo(new Position(1, 2), mockWorld)));
}
@Test
void monster_cannot_attack_after_death() {
defaultShade.kill();
assertThat(false, equalTo(defaultShade.performAttack(Attacks.CHILL, defaultPlayer)));
assertThat(false, equalTo(defaultShade.performAttack(Attacks.CHILL, mockPlayer)));
}
@Test
@@ -58,7 +60,7 @@ public class MonsterTest {
legalDestinations.addAll(move.findLegalDestinations(startingPosition));
}
defaultShade.move(defaultWorld);
defaultShade.move(mockWorld);
assertThat(legalDestinations, hasItem(defaultShade.getPosition()));
}
@@ -69,27 +71,39 @@ public class MonsterTest {
World world = worldGenerator.getWorld();
Position destination = new Position(2, 2);
when(defaultPlayer.getPosition()).thenReturn(destination);
world.addEntityToMap(defaultPlayer);
when(mockPlayer.getPosition()).thenReturn(destination);
world.addEntityToMap(mockPlayer);
assertThat(defaultShade.moveTo(destination, world), equalTo(false));
}
@Test
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);
when(mockPlayer.getPosition()).thenReturn(destination);
world.addEntityToMap(mockPlayer);
defaultShade.move(world);
assertThat(defaultShade.getPosition(), not(defaultPlayer.getPosition()));
assertThat(defaultShade.getPosition(), not(mockPlayer.getPosition()));
}
//Flera assertThat i samma. Farligt????
@Test
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
@@ -111,4 +125,6 @@ public class MonsterTest {
void use_of_attack_not_in_arsenal_is_rejected() {
}
}