monster #20

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

20
src/main/java/Tile.java Normal file
View File

@@ -0,0 +1,20 @@
public class Tile {
private String tileName;
private int staminaCost;
private String tilePrintString;
public Tile(String tileName, int staminaCost, String tilePrintString) {
this.tileName = tileName;
this.staminaCost = staminaCost;
this.tilePrintString = tilePrintString;
}
public String getName() {
return tileName;
}
public int getStaminaCost() {
return staminaCost;
}
public String getTilePrintString() {
return tilePrintString;
}
}

View File

@@ -1,6 +1,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.LinkedList;
public class World {
// Används för att printa mappen.
private String[][] map = {
@@ -13,16 +14,28 @@ public class World {
{"@","@","@","@","@","@","@","@","@","@","@"}
};
HashMap<Integer, Tile> tileMap = new HashMap<>();
private LinkedList<Entity> entityList = new LinkedList<>();
public Entity addEntityToList(Entity e) {
entityList.add(e);
private HashMap<Entity, Position> entityMap = new HashMap<>();
public final int TILE_SIZE = 25;
public void addEntityToMap(Entity e) {
entityMap.put(e, e.getPosition());
}
public World() {
autoGenerateTiles();
}
private void autoGenerateTiles() {
Random rand = new Random();
for (int i = 0; i < TILE_SIZE; i++) {
int x = rand.nextInt(4);
}
}
public void addTile(Tile tile, int xCoordinate, int yCoordinate) {
int key = xCoordinate * 10 + yCoordinate;
tileMap.put(key, tile);
}
/*
M in array is monster
C in array is chest
@@ -40,8 +53,8 @@ public class World {
return map;
}
public LinkedList<Entity> getEntityList() {
return entityList;
public HashMap<Entity, Position> getEntityList() {
return entityMap;
}
public Tile getTileAtPosition(int xCoordinate, int yCoordinate) {
@@ -49,23 +62,18 @@ public class World {
return tileMap.get(tileKey);
}
public Entity getEntityAtPosition(int xCoordinate, int yCoordinate) {
if (!entityList.isEmpty()) {
for (int i = 0; i < entityList.size(); i++) {
int xEntityCord = entityList.get(i).getX();
int yEntityCord = entityList.get(i).getY();
if (xCoordinate == xEntityCord && yCoordinate == yEntityCord) {
return entityList.get(i);
}
public Entity getEntityAtPosition(Position position) {
for (HashMap.Entry<Entity, Position> entry : entityMap.entrySet()) {
Position pos = entry.getValue();
Entity e = entry.getKey();
if (pos.equals(position)) {
return e;
}
}
return null;
}
public void changePosition(Object Character, int xCoordinate, int yCoordinate) {
if (Character instanceof Player) {
map[(xCoordinate * 2) + 1][yCoordinate + 1] = "X";
} else if (Character instanceof Enemy) {
map[(xCoordinate * 2) + 1][yCoordinate + 1] = "M";
}
public void changePosition(Entity Character, Position newPosition) {
entityMap.put(Character, newPosition);
}
}

View File

@@ -0,0 +1,27 @@
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 int STAMINA_COST = 1;
private final String PRINT_STRING = "Mountain test";
@Test
void getTileNameTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, PRINT_STRING);
String tileNameTwo = tileTest.getName();
assertEquals(TILE_NAME, tileNameTwo);
}
@Test
void getStaminaCostTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, PRINT_STRING);
assertEquals(STAMINA_COST, tileTest.getStaminaCost());
}
@Test
void printTileStringTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, PRINT_STRING);
assertEquals(PRINT_STRING, tileTest.getTilePrintString());
}
}

View File

@@ -1,14 +1,39 @@
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.*;
class WorldTest {
final int TILE_SIZE = 25;
@Test
void addTileToMapTest() {
World test = new World();
Tile tile new Tile();
Tile tile = new Tile("test", 3, "test");
int xCoordinate = 1;
int yCoordinate = 1;
test.addTile(tile, xCoordinate, yCoordinate);
Tile tileTest = test.getTileAtPosition(1, 1);
assertEquals(tile, tileTest);
}
@Test
void addEntityToMapTest() {
World test = new World();
Entity character = new Player("Name");
character.setPosition(new Position(2, 2));
test.addEntityToMap(character);
}
@Test
void randomTileGeneratorTest() {
World test = new World();
HashMap<Integer, Tile> tileMap = new HashMap<>();
int tileSize = tileMap.size();
assertEquals(TILE_SIZE, tileSize);
}
@Test
void getTileAtPositionTest() {
World test = new World();
HashMap<Integer, Tile> tileMap = new HashMap<>();
}
}