Job terrain #12

Merged
erns6604 merged 16 commits from job_terrain into main 2025-10-28 11:21:35 +01:00
6 changed files with 63 additions and 24 deletions
Showing only changes of commit b61d678b5c - Show all commits

View File

@ -1,21 +1,23 @@
package Inventory; package Inventory;
import Item.Item;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class Inventory { public class Inventory {
List<String> items; List<Item> items;
public Inventory() { public Inventory() {
items = new ArrayList<>(); items = new ArrayList<>();
} }
public List<String> getItems() { public List<Item> getItems() {
return Collections.unmodifiableList(items); return Collections.unmodifiableList(items);
} }
public void addItem(String item) { public void addItem(Item item) {
items.add(item); items.add(item);
} }

View File

@ -0,0 +1,13 @@
package Item;
public record BasicItem(String id, String name) implements Item {
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
}

View File

@ -0,0 +1,6 @@
package Item;
public interface Item {
String getId();
String getName();
}

View File

@ -1,5 +1,7 @@
package Terrain; package Terrain;
import Item.BasicItem;
import Item.Item;
import Shared.LootEntry; import Shared.LootEntry;
import Shared.LootTable; import Shared.LootTable;
@ -8,33 +10,33 @@ import java.util.List;
public enum Biome { public enum Biome {
GRASSLAND("Grassland", new LootTable<>(new ArrayList<>(List.of( GRASSLAND("Grassland", new LootTable<>(new ArrayList<>(List.of(
new LootEntry<>("Mud", 10), new LootEntry<>(new BasicItem("mud", "Mud"), 10),
new LootEntry<>("Earth Root", 50), new LootEntry<>(new BasicItem("earth_root", "Earth Root"), 50),
new LootEntry<>("Crystal core", 180) new LootEntry<>(new BasicItem("crystal_core", "Crystal core"), 180)
)) { )) {
})), })),
MOUNTAIN("Mountain", new LootTable<>(new ArrayList<>(List.of( MOUNTAIN("Mountain", new LootTable<>(new ArrayList<>(List.of(
new LootEntry<>("Rock", 10), new LootEntry<>(new BasicItem("rock", "Rock"), 10),
new LootEntry<>("Iron", 50), new LootEntry<>(new BasicItem("iron", "Iron"), 50),
new LootEntry<>("Diamond", 180) new LootEntry<>(new BasicItem("diamond", "Diamond"), 180)
)) { )) {
})), })),
COAST("Coast", new LootTable<>(new ArrayList<>(List.of( COAST("Coast", new LootTable<>(new ArrayList<>(List.of(
new LootEntry<>("Sand", 10), new LootEntry<>(new BasicItem("sand", "Sand"), 10),
new LootEntry<>("Shell", 50), new LootEntry<>(new BasicItem("shell", "Shell"), 50),
new LootEntry<>("Sandfish", 180) new LootEntry<>(new BasicItem("sandfish", "Sandfish"), 180)
)) { )) {
})), })),
FOREST("Forest", new LootTable<>(new ArrayList<>(List.of( FOREST("Forest", new LootTable<>(new ArrayList<>(List.of(
new LootEntry<>("Moss", 10), new LootEntry<>(new BasicItem("moss", "Moss"), 10),
new LootEntry<>("Fairy rock", 50), new LootEntry<>(new BasicItem("fairy_rock", "Fairy rock"), 50),
new LootEntry<>("Unicorn horn", 230) new LootEntry<>(new BasicItem("unicorn_horn", "Unicorn horn"), 230)
)) { )) {
})); }));
private final String name; private final String name;
private final LootTable<String> lootTable; private final LootTable<? extends Item> lootTable;
Biome(String name, LootTable<String> lootTable) { Biome(String name, LootTable<? extends Item> lootTable) {
this.name = name; this.name = name;
this.lootTable = lootTable; this.lootTable = lootTable;
} }
@ -43,7 +45,7 @@ public enum Biome {
return name; return name;
} }
public LootTable<String> getLootTable() { public LootTable<? extends Item> getLootTable() {
return lootTable; return lootTable;
} }
} }

View File

@ -0,0 +1,17 @@
import Inventory.Inventory;
import Item.BasicItem;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class InventoryTest {
@Test
void can_add_item_to_inventory() {
var inventory = new Inventory();
inventory.addItem(new BasicItem("iron_sword", "Iron Sword"));
assertThat(inventory.getItems(), hasItem(
hasProperty("id", equalTo("iron_sword"))
));
}
}

View File

@ -5,11 +5,9 @@ import Terrain.Biome;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MinerTest { public class MinerTest {
private Player defaultPlayer() {return new Player("John"); } private Player defaultPlayer() {return new Player("John"); }
@ -42,12 +40,13 @@ public class MinerTest {
assertEquals(10, job.getExperience()); assertEquals(10, job.getExperience());
} }
@Test @Test
void respects_biome_loot_table() { void dig_in_coast_use_coast_loot() {
RandomProvider random = bound -> 9; RandomProvider random = bound -> 9;
var job = new Miner(defaultPlayer()); var job = new Miner(defaultPlayer());
Biome.COAST.getLootTable().setRandomProvider(random); Biome.COAST.getLootTable().setRandomProvider(random);
job.dig(Biome.COAST); job.dig(Biome.COAST);
assertThat(job.getActor().getInventory().getItems(), hasItem("Sand")); assertThat(job.getActor().getInventory().getItems(), hasItem(
hasProperty("id", equalTo("sand"))));
} }
@Test @Test
@ -56,7 +55,7 @@ public class MinerTest {
var job = new Miner(defaultPlayer()); var job = new Miner(defaultPlayer());
Biome.MOUNTAIN.getLootTable().setRandomProvider(random); Biome.MOUNTAIN.getLootTable().setRandomProvider(random);
job.dig(Biome.MOUNTAIN); job.dig(Biome.MOUNTAIN);
assertThat(job.getActor().getInventory().getItems(), hasItem("Iron")); assertThat(job.getActor().getInventory().getItems(), hasItem( hasProperty("id", equalTo("iron"))));
} }
} }