monster #20

Merged
viud3133 merged 39 commits from monster into equipments 2025-10-30 12:05:40 +01:00
2 changed files with 85 additions and 0 deletions
Showing only changes of commit 5e7d100b27 - Show all commits

71
src/main/java/World.java Normal file
View File

@ -0,0 +1,71 @@
import java.util.HashMap;
import java.util.Random;
import java.util.LinkedList;
public class World {
// Används för att printa mappen.
private String[][] map = {
{"@","@","@","@","@","@","@","@","@","@","@"},
{"@","X","|"," ", "|", " ","|"," ","|"," ","@"},
{"@"," ","|"," ", "|", " ","|"," ","|"," ","@"},
{"@"," ","|"," ", "|", " ","|"," ","|"," ","@"},
{"@"," ","|"," ", "|", " ","|"," ","|"," ","@"},
{"@"," ","|"," ", "|", " ","|"," ","|"," ","@"},
{"@","@","@","@","@","@","@","@","@","@","@"}
};
HashMap<Integer, Tile> tileMap = new HashMap<>();
private LinkedList<Entity> entityList = new LinkedList<>();
public Entity addEntityToList(Entity e) {
entityList.add(e);
}
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
X in array is player.
*/
public void printMap() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j]);
}
}
}
public String[][] getMap() {
return map;
}
public LinkedList<Entity> getEntityList() {
return entityList;
}
public Tile getTileAtPosition(int xCoordinate, int yCoordinate) {
int tileKey = xCoordinate * 10 + yCoordinate;
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);
}
}
}
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";
}
}
}

View File

@ -0,0 +1,14 @@
import static org.junit.jupiter.api.Assertions.*;
class WorldTest {
@Test
void addTileToMapTest() {
World test = new World();
Tile tile new Tile();
int xCoordinate = 1;
int yCoordinate = 1;
test.addTile(tile, xCoordinate, yCoordinate);
Tile tileTest = test.getTileAtPosition(1, 1);
assertEquals(tile, tileTest);
}
}