monster #20

Merged
viud3133 merged 39 commits from monster into equipments 2025-10-30 12:05:40 +01:00
7 changed files with 62 additions and 12 deletions
Showing only changes of commit 556dcf4160 - Show all commits

View File

@@ -13,6 +13,7 @@
<maven.compiler.target>23</maven.compiler.target> <maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
@@ -33,5 +34,5 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project>
</project>

11
src/main/java/Action.java Normal file
View File

@@ -0,0 +1,11 @@
public abstract class Action {
private int cost;
public Action(int cost) {
}
public int getCost() {
return cost;
}
}

View File

@@ -0,0 +1,9 @@
public enum Actions {
HEAL(2);
private final int cost;
Actions(int cost) {
this.cost = cost;
}
}

View File

@@ -0,0 +1,8 @@
public class Healing extends Action {
private int healthRegenerated;
public Healing() {
super(2);
}
}

View File

@@ -19,6 +19,7 @@ public abstract class Monster {
public void kill() { public void kill() {
health = 0; health = 0;
energy = 0;
isAlive = false; isAlive = false;
} }
@@ -40,7 +41,7 @@ public abstract class Monster {
abstract void heal(); abstract void heal();
abstract void takeDamage(); abstract void takeDamage(int amount);
abstract void recharge(); abstract void recharge();

View File

@@ -3,24 +3,38 @@ import java.util.*;
public class Shade extends Monster { public class Shade extends Monster {
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 int MAX_HEALTH = 20;
public static final int MAX_ENERGY = 14;
private static final int MAX_HEALTH = 20; private static final int HEALTH_PER_HEAL = 2;
private static final int MAX_ENERGY = 14; private static final int ENERGY_COST_PER_HEAL = 1;
public Shade() { public Shade() {
super(MAX_HEALTH, MAX_ENERGY, Monster.DEFAULT_POSITION); super(MAX_HEALTH, MAX_ENERGY, Monster.DEFAULT_POSITION);
} }
public void heal() { public void heal() {
if (isAlive()) { if (!isAlive) {
health += 2; throw new IllegalStateException("This shade is dead");
}
if (health < MAX_HEALTH) {
health += HEALTH_PER_HEAL;
energy -= ENERGY_COST_PER_HEAL;
enforceHealthMaximum();
} }
} }
public void takeDamage() { public void takeDamage(int amount) {
if (amount < 0) {
throw new IllegalArgumentException("'amount' must be a positive integer");
}
if (health - amount <= 0) {
kill();
}
else {
health =- amount;
}
} }
public void recharge() { public void recharge() {
@@ -34,4 +48,10 @@ public class Shade extends Monster {
public void moveTo(Position position) { public void moveTo(Position position) {
} }
private void enforceHealthMaximum() {
if (health > MAX_HEALTH) {
health = MAX_HEALTH;
}
}
} }

View File

@@ -9,6 +9,7 @@ import Shared.*;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@@ -30,14 +31,13 @@ public class MonsterTest {
@Test @Test
void reaching_zero_health_triggers_death() { void reaching_zero_health_triggers_death() {
defaultShade.kill(); defaultShade.takeDamage(Shade.MAX_HEALTH);
assertThat(false, equalTo(defaultShade.isAlive())); assertThat(false, equalTo(defaultShade.isAlive()));
} }
@Test @Test
void monster_cannot_regain_health_after_death() { void monster_cannot_regain_health_after_death() {
defaultShade.kill(); defaultShade.kill();
defaultShade.heal(); assertThrows(IllegalStateException.class, () -> defaultShade.heal());
assertThat(defaultShade.getHealth(), equalTo(0));
} }
} }