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
-- Mage
- Spells
- World generation
- World.World generation
-- Terrain types
- Loot
- Equipment

View File

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

View File

@@ -1,7 +1,6 @@
package Monster;
import Entity.Player;
import Entity.Position;
import Entity.*;
import World.*;
import java.util.*;
public class Shade extends Monster implements CanMove, CanAttack {
@@ -62,11 +61,24 @@ public class Shade extends Monster implements CanMove, CanAttack {
return true;
}
public boolean moveTo(Position position) {
public boolean moveTo(Position position, World world) {
if (isDead()) {
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;
}
@@ -82,4 +94,14 @@ public class Shade extends Monster implements CanMove, CanAttack {
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 Entity.*;
import Monster.Biomes;
public class MapGenerator {
private int x;
private int y;
@@ -19,24 +24,24 @@ public class MapGenerator {
for (int j = 0; j < y; j++) {
int tileDice = rand.nextInt(4);
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));
} 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));
} 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));
} 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));
}
}
}
}
public void addTile(String name, int staminaCost, String description, Position position) {
Tile newTile = new Tile(name, staminaCost, description);
public void addTile(Biomes biome, int staminaCost, String description, Position position) {
Tile newTile = new Tile(biome, staminaCost, description);
gameWorld.addTile(newTile, position);
}

View File

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

View File

@@ -1,7 +1,8 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
package World;
import Entity.*;
import java.util.*;
public class World {
private HashMap<Position, Tile> tileMap = new HashMap<>();
@@ -91,6 +92,25 @@ public class World {
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) {
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 java.util.HashMap;
@@ -19,7 +24,7 @@ class MapGeneratorTest {
@Test
void addTileAtPositionTest() {
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();
assertTrue(testWorld.getTileAtPosition(new Position(2, 2)) instanceof Tile);
assertFalse(testWorld.getTileAtPosition(new Position(2, 1)) instanceof Tile);

View File

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

View File

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

View File

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