Job terrain #12

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

View File

@ -17,16 +17,7 @@ public class Miner extends Job {
}
public void dig(Biome biome) {
switch (biome) {
case COAST:
actor.getInventory().addItem("Sand");
break;
case MOUNTAIN:
actor.getInventory().addItem("Stone");
break;
default:
actor.getInventory().addItem("Clay");
break;
}
var item = biome.getLootTable().roll();
actor.getInventory().addItem(item);
}
}

View File

@ -0,0 +1,11 @@
package Shared;
import java.util.Random;
public class DefaultRandomProvider implements RandomProvider {
private final Random random = new Random();
@Override
public int nextInt(int bound) {
return random.nextInt(bound);
}
}

View File

@ -5,13 +5,24 @@ import java.util.List;
import java.util.Random;
public class LootTable<T> {
private final Random random;
private RandomProvider random;
private final List<LootEntry<T>> entries = new ArrayList<>();
public LootTable() {
this.random = new Random();
this.random = new DefaultRandomProvider();
}
public LootTable(Random random) {
public LootTable(List<LootEntry<T>> entries) {
this.random = new DefaultRandomProvider();
this.entries.addAll(entries);
}
public void setRandomProvider(RandomProvider random) {
this.random = random;
}
public LootTable(RandomProvider random) {
this.random = random;
}

View File

@ -0,0 +1,5 @@
package Shared;
public interface RandomProvider {
int nextInt(int bound);
}

View File

@ -1,7 +1,49 @@
package Terrain;
import Shared.LootEntry;
import Shared.LootTable;
import java.util.ArrayList;
import java.util.List;
public enum Biome {
GRASSLAND, MOUNTAIN, COAST, FOREST //Är inte fäst vid dessa
GRASSLAND("Grassland", new LootTable<>(new ArrayList<>(List.of(
new LootEntry<>("Mud", 10),
new LootEntry<>("Earth Root", 50),
new LootEntry<>("Crystal core", 180)
)) {
})),
MOUNTAIN("Mountain", new LootTable<>(new ArrayList<>(List.of(
new LootEntry<>("Rock", 10),
new LootEntry<>("Iron", 50),
new LootEntry<>("Diamond", 180)
)) {
})),
COAST("Coast", new LootTable<>(new ArrayList<>(List.of(
new LootEntry<>("Sand", 10),
new LootEntry<>("Shell", 50),
new LootEntry<>("Sandfish", 180)
)) {
})),
FOREST("Forest", new LootTable<>(new ArrayList<>(List.of(
new LootEntry<>("Moss", 10),
new LootEntry<>("Fairy rock", 50),
new LootEntry<>("Unicorn horn", 230)
)) {
}));
private final String name;
private final LootTable<String> lootTable;
Biome(String name, LootTable<String> lootTable) {
this.name = name;
this.lootTable = lootTable;
}
public String getName() {
return name;
}
public LootTable<String> getLootTable() {
return lootTable;
}
}

View File

@ -1,4 +1,5 @@
import Shared.LootTable;
import Shared.RandomProvider;
import org.junit.jupiter.api.Test;
import java.util.Random;
@ -6,6 +7,9 @@ import java.util.Random;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class LootTableTest {
LootTable<String> defaultLootTable() {
@ -24,12 +28,9 @@ public class LootTableTest {
@Test
void respects_weight_when_rolling() {
LootTable<String> loot = new LootTable<>(new Random() {
@Override
public int nextInt(int bound) {
return 94;
}
});
var mockRandomProvider = mock(RandomProvider.class);
when(mockRandomProvider.nextInt(anyInt())).thenReturn(94);
LootTable<String> loot = new LootTable<>(mockRandomProvider);
loot.addEntry("Stone", 90);
loot.addEntry("Iron", 10);
String result = loot.roll();

View File

@ -1,11 +1,15 @@
import Entity.Player;
import Job.Miner;
import Shared.RandomProvider;
import Terrain.Biome;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
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.when;
public class MinerTest {
private Player defaultPlayer() {return new Player("John"); }
@ -37,19 +41,22 @@ public class MinerTest {
job.gainExperience(job.remainingXpUntilLevelUp() + 10);
assertEquals(10, job.getExperience());
}
@Test
void dig_in_coast_use_coast_loot() {
void respects_biome_loot_table() {
RandomProvider random = bound -> 9;
var job = new Miner(defaultPlayer());
Biome.COAST.getLootTable().setRandomProvider(random);
job.dig(Biome.COAST);
assertThat(job.getActor().getInventory().getItems(), hasItem("Sand"));
}
@Test
void dig_in_mountain_use_mountain_loot() {
RandomProvider random = bound -> 15;
var job = new Miner(defaultPlayer());
Biome.MOUNTAIN.getLootTable().setRandomProvider(random);
job.dig(Biome.MOUNTAIN);
assertThat(job.getActor().getInventory().getItems(), hasItem("Stone"));
assertThat(job.getActor().getInventory().getItems(), hasItem("Iron"));
}
}

View File

@ -12,6 +12,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class PlayerTest {
private Player defaultPlayer() {
@ -58,8 +59,9 @@ class PlayerTest {
void miner_can_dig() {
var p = new Player("John");
p.learnJob(new Miner(p));
p.performAction(new DigAction(Biome.FOREST));
assertThat(p.getInventory().getItems(), hasItem("Clay"));
var mockAction = mock(DigAction.class);
p.performAction(mockAction);
verify(mockAction, times(1)).execute(any());
}
@Test