monster #20

Merged
viud3133 merged 39 commits from monster into equipments 2025-10-30 12:05:40 +01:00
11 changed files with 104 additions and 46 deletions
Showing only changes of commit 667998337e - Show all commits

View File

@@ -28,7 +28,7 @@ The player object is a representation of the user and it's agency in the game wo
-- Warrior -- Warrior
-- Mage -- Mage
- Spells - Spells
- World generation - World.World generation
-- Terrain types -- Terrain types
- Loot - Loot
- Equipment - Equipment

View File

@@ -1,10 +1,10 @@
package Monster; package Monster;
import World.*;
import Entity.Position; import Entity.Position;
//Är detta ett ok namn? //Är detta ett ok namn?
public interface CanMove { public interface CanMove {
abstract boolean move(); abstract boolean move();
abstract boolean moveTo(Position position); abstract boolean moveTo(Position position, World world);
} }

View File

@@ -1,7 +1,6 @@
package Monster; package Monster;
import Entity.*;
import Entity.Player; import World.*;
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 {
@@ -62,11 +61,24 @@ public class Shade extends Monster implements CanMove, CanAttack {
return true; return true;
} }
public boolean moveTo(Position position) { public boolean moveTo(Position position, World world) {
if (isDead()) { if (isDead()) {
return false; return false;
} }
int x = position.x();
int y = position.y();
int worldSize = world.getMap().length;
if (x >= worldSize || y >= worldSize || x < 0 || y < 0) {
return false;
}
if (playerIsAtPosition(position, world)) {
return false;
}
this.position = position;
updateDestinations(this.position);
return true; return true;
} }
@@ -82,4 +94,14 @@ public class Shade extends Monster implements CanMove, CanAttack {
validDestinations.addAll(move.findLegalDestinations(position)); validDestinations.addAll(move.findLegalDestinations(position));
} }
} }
private boolean playerIsAtPosition(Position position, World world) {
List<Entity> entitiesAtPosition = world.getPositionEntityMap().get(position);
for (Entity entity : entitiesAtPosition) {
if (entity instanceof Player) {
return true;
}
}
return false;
}
} }

View File

@@ -1,6 +0,0 @@
public abstract class Nixie extends Monster implements CanMove, CanAttack {
public Nixie() {
super(0,0,Monster.DEFAULT_POSITION);
}
}

View File

@@ -1,4 +1,9 @@
package World;
import java.util.Random; import java.util.Random;
import Entity.*;
import Monster.Biomes;
public class MapGenerator { public class MapGenerator {
private int x; private int x;
private int y; private int y;
@@ -19,24 +24,24 @@ public class MapGenerator {
for (int j = 0; j < y; j++) { for (int j = 0; j < y; j++) {
int tileDice = rand.nextInt(4); int tileDice = rand.nextInt(4);
if (tileDice == 0) { if (tileDice == 0) {
Tile newTile = new Tile("Mountain", 3, "Future development"); Tile newTile = new Tile(Biomes.FOREST, 3, "Future development");
gameWorld.addTile(newTile, new Position(i, j)); gameWorld.addTile(newTile, new Position(i, j));
} else if (tileDice == 1) { } else if (tileDice == 1) {
Tile newTile = new Tile("plain", 1, "Future development"); Tile newTile = new Tile(Biomes.GRASSLAND, 1, "Future development");
gameWorld.addTile(newTile, new Position(i, j)); gameWorld.addTile(newTile, new Position(i, j));
} else if (tileDice == 2) { } else if (tileDice == 2) {
Tile newTile = new Tile("Lava", 2, "Future development"); Tile newTile = new Tile(Biomes.MOUNTAIN, 2, "Future development");
gameWorld.addTile(newTile, new Position(i, j)); gameWorld.addTile(newTile, new Position(i, j));
} else if (tileDice == 3) { } else if (tileDice == 3) {
Tile newTile = new Tile("Water", 4, "Future development"); Tile newTile = new Tile(Biomes.COAST, 4, "Future development");
gameWorld.addTile(newTile, new Position(i, j)); gameWorld.addTile(newTile, new Position(i, j));
} }
} }
} }
} }
public void addTile(String name, int staminaCost, String description, Position position) { public void addTile(Biomes biome, int staminaCost, String description, Position position) {
Tile newTile = new Tile(name, staminaCost, description); Tile newTile = new Tile(biome, staminaCost, description);
gameWorld.addTile(newTile, position); gameWorld.addTile(newTile, position);
} }

View File

@@ -1,18 +1,22 @@
package World;
import Monster.Biomes;
public class Tile { public class Tile {
private String tileName; private Biomes biome;
private int staminaCost; private int staminaCost;
private String tileID; private String tileID;
public Tile(String tileName, int staminaCost, String tileID) {
this.tileName = tileName; public Tile(Biomes biome, int staminaCost, String tileID) {
this.biome = biome;
this.staminaCost = staminaCost; this.staminaCost = staminaCost;
this.tileID = tileID; this.tileID = tileID;
} }
public void setStaminaCost(int newStaminaCost) { public void setStaminaCost(int newStaminaCost) {
staminaCost = newStaminaCost; staminaCost = newStaminaCost;
} }
public String getName() { public Biomes getBiome() {
return tileName; return biome;
} }
public int getStaminaCost() { public int getStaminaCost() {
return staminaCost; return staminaCost;

View File

@@ -1,7 +1,8 @@
import java.util.ArrayList; package World;
import java.util.HashMap;
import java.util.Map; import Entity.*;
import java.util.Random; import java.util.*;
public class World { public class World {
private HashMap<Position, Tile> tileMap = new HashMap<>(); private HashMap<Position, Tile> tileMap = new HashMap<>();
@@ -91,6 +92,25 @@ public class World {
return null; return null;
} }
//Har inte testat denna den kanske inte alls funkar
private Map<Position, List<Entity>> invertEntityMap() {
Map<Position, List<Entity>> entitysByPosition = new HashMap<>();
for (Position position : entityMap.values()) {
entitysByPosition.put(position, new ArrayList<>());
}
for (Entity entity : entityMap.keySet()) {
Position position = entityMap.get(entity);
entitysByPosition.get(position).add(entity);
}
return entitysByPosition;
}
public Map<Position, List<Entity>> getPositionEntityMap() {
return invertEntityMap();
}
public void changePosition(Entity Character, Position newPosition) { public void changePosition(Entity Character, Position newPosition) {
entityMap.put(Character, newPosition); entityMap.put(Character, newPosition);
} }

View File

@@ -1,3 +1,8 @@
import Monster.Biomes;
import World.MapGenerator;
import World.Tile;
import World.World;
import Entity.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.HashMap; import java.util.HashMap;
@@ -19,7 +24,7 @@ class MapGeneratorTest {
@Test @Test
void addTileAtPositionTest() { void addTileAtPositionTest() {
MapGenerator mapGenerate = new MapGenerator(X_FIVE, Y_FIVE); MapGenerator mapGenerate = new MapGenerator(X_FIVE, Y_FIVE);
mapGenerate.addTile("Test", 10, "test description", new Position(2, 2)); mapGenerate.addTile(Biomes.MOUNTAIN, 10, "test description", new Position(2, 2));
World testWorld = mapGenerate.getWorld(); World testWorld = mapGenerate.getWorld();
assertTrue(testWorld.getTileAtPosition(new Position(2, 2)) instanceof Tile); assertTrue(testWorld.getTileAtPosition(new Position(2, 2)) instanceof Tile);
assertFalse(testWorld.getTileAtPosition(new Position(2, 1)) instanceof Tile); assertFalse(testWorld.getTileAtPosition(new Position(2, 1)) instanceof Tile);

View File

@@ -1,8 +1,6 @@
import Entity.*; import Entity.*;
import Monster.Attacks; import Monster.*;
import Monster.MovementPatterns; import World.*;
import Monster.Shade;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import java.util.*; import java.util.*;
@@ -17,6 +15,8 @@ public class MonsterTest {
private Shade defaultShade; private Shade defaultShade;
private Player defaultPlayer = mock(Player.class); private Player defaultPlayer = mock(Player.class);
private World defaultWorld = mock(World.class);
private MapGenerator worldGenerator = new MapGenerator(4, 4);
@BeforeEach @BeforeEach
void reset() { void reset() {
@@ -26,7 +26,7 @@ public class MonsterTest {
@Test @Test
void monster_cannot_move_after_death() { void monster_cannot_move_after_death() {
defaultShade.kill(); defaultShade.kill();
assertThat(false, equalTo(defaultShade.moveTo(new Position(0, 1)))); assertThat(false, equalTo(defaultShade.moveTo(new Position(0, 1), defaultWorld)));
} }
@Test @Test
@@ -62,11 +62,17 @@ public class MonsterTest {
assertThat(legalDestinations, hasItem(defaultShade.getPosition())); assertThat(legalDestinations, hasItem(defaultShade.getPosition()));
} }
//Borde nog ha mer mocks här men tycker det är svårt med flera lager av dom...
@Test @Test
void shade_wont_move_to_same_position_as_player() { void shade_wont_move_to_same_position_as_player() {
worldGenerator.randomWorldGeneration();
World world = worldGenerator.getWorld();
Position destination = new Position(1, 1); Position destination = new Position(1, 1);
when(defaultPlayer.getPosition()).thenReturn(destination); when(defaultPlayer.getPosition()).thenReturn(destination);
assertThat(defaultShade.moveTo(destination), equalTo(false)); world.addEntityToMap(defaultPlayer);
assertThat(defaultShade.moveTo(destination, world), equalTo(false));
} }
@Test @Test

View File

@@ -1,32 +1,31 @@
import Monster.*;
import World.Tile;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class TileTest { class TileTest {
private final String TILE_NAME = "Name test"; private final Biomes BIOME = Biomes.MOUNTAIN;
private final int STAMINA_COST = 1; private final int STAMINA_COST = 1;
private final String ID_STRING = "Mountain test"; private final String ID_STRING = "Mountain test";
@Test @Test
void getTileNameTest() { void getBiomeTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, ID_STRING); Tile tileTest = new Tile(BIOME, STAMINA_COST, ID_STRING);
String tileNameTwo = tileTest.getName(); Biomes tileNameTwo = tileTest.getBiome();
assertEquals(TILE_NAME, tileNameTwo); assertEquals(BIOME, tileNameTwo);
} }
@Test @Test
void getStaminaCostTest() { void getStaminaCostTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, ID_STRING); Tile tileTest = new Tile(BIOME, STAMINA_COST, ID_STRING);
assertEquals(STAMINA_COST, tileTest.getStaminaCost()); assertEquals(STAMINA_COST, tileTest.getStaminaCost());
} }
@Test @Test
void printTileStringTest() { void printTileStringTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, ID_STRING); Tile tileTest = new Tile(BIOME, STAMINA_COST, ID_STRING);
assertEquals(ID_STRING, tileTest.getTileID()); assertEquals(ID_STRING, tileTest.getTileID());
} }
@Test @Test
void setStaminaCostTest() { void setStaminaCostTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, ID_STRING); Tile tileTest = new Tile(BIOME, STAMINA_COST, ID_STRING);
tileTest.setStaminaCost(10); tileTest.setStaminaCost(10);
int newStaminaCost = tileTest.getStaminaCost(); int newStaminaCost = tileTest.getStaminaCost();
assertEquals(10, newStaminaCost); assertEquals(10, newStaminaCost);

View File

@@ -1,5 +1,8 @@
import World.Tile;
import World.World;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import Entity.*;
import java.util.HashMap; import java.util.HashMap;