equipments #22

Merged
viud3133 merged 40 commits from equipments into main 2025-10-30 12:09:43 +01:00
3 changed files with 42 additions and 10 deletions
Showing only changes of commit 6f94d1510c - Show all commits

View File

@@ -2,19 +2,28 @@ import Entity.Position;
import java.util.*; import java.util.*;
public enum MovementPatterns { public enum MovementPatterns {
ONE_STEP_DIAGONALLY(1); ONE_DIAGONAL_STEP(1, 1);
private int cost; public int cost;
private int id;
MovementPatterns(int cost) { MovementPatterns(int cost, int id) {
this.cost = cost; this.cost = cost;
this.id = id;
} }
public List<Position> getLegalDestinations(Position position) { public List<Position> findLegalDestinations(Position position) {
List<Position> legalDestinations = new ArrayList<>(); List<Position> legalDestinations = new ArrayList<>();
switch { switch(id) {
case case 1:
int x = position.x();
int y = position.y();
legalDestinations.addAll(Arrays.asList(new Position(x - 1, y + 1), new Position(x + 1, y + 1),
new Position(x - 1, y - 1), new Position(x + 1, y - 1)));
break;
} }
return legalDestinations;
} }
} }

View File

@@ -3,15 +3,20 @@ import Entity.Position;
import java.util.*; import java.util.*;
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));
public static final List<MovementPatterns> MOVES = Collections.unmodifiableList(Arrays.asList(MovementPatterns.ONE_DIAGONAL_STEP));
public static final int MAX_HEALTH = 20; public static final int MAX_HEALTH = 20;
public static final int MAX_ENERGY = 14; public static final int MAX_ENERGY = 14;
private static final int HEALTH_PER_HEAL = 2; private static final int HEALTH_PER_HEAL = 2;
private static final int ENERGY_COST_PER_HEAL = 1; private static final int ENERGY_COST_PER_HEAL = 1;
private Random random = new Random();
private List<Position> validDestinations = new ArrayList<>();
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() {
@@ -46,7 +51,13 @@ public class Shade extends Monster implements CanMove, CanAttack {
} }
public boolean move() { public boolean move() {
return false; if (isDead()) {
return false;
}
position = validDestinations.get(random.nextInt(validDestinations.size()));
updateDestinations(position);
return true;
} }
public boolean moveTo(Position position) { public boolean moveTo(Position position) {
@@ -62,4 +73,11 @@ public class Shade extends Monster implements CanMove, CanAttack {
health = MAX_HEALTH; health = MAX_HEALTH;
} }
} }
private void updateDestinations(Position position) {
validDestinations.clear();
for (MovementPatterns move : MOVES) {
validDestinations.addAll(move.findLegalDestinations(position));
}
}
} }

View File

@@ -50,6 +50,7 @@ public class MonsterTest {
assertThrows(IllegalStateException.class, () -> defaultShade.heal()); assertThrows(IllegalStateException.class, () -> defaultShade.heal());
} }
//Är denna för lång?
@Test @Test
void method_move_places_monster_in_legal_position() { void method_move_places_monster_in_legal_position() {
Position startingPosition = defaultShade.getPosition(); Position startingPosition = defaultShade.getPosition();
@@ -57,11 +58,15 @@ public class MonsterTest {
List<MovementPatterns> moves = defaultShade.MOVES; List<MovementPatterns> moves = defaultShade.MOVES;
for (MovementPatterns move : moves) { for (MovementPatterns move : moves) {
legalDestinations.addAll(move.getLegalDestinations(startingPosition)); legalDestinations.addAll(move.findLegalDestinations(startingPosition));
} }
defaultShade.move(); defaultShade.move();
assertThat(legalDestinations, hasItem(defaultShade.getPosition())); assertThat(legalDestinations, hasItem(defaultShade.getPosition()));
} }
@Test
void monster_cannot_do_anything_when_out_of_energy() {
}
} }