monster #20
20
src/main/java/Tile.java
Normal file
20
src/main/java/Tile.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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,21 +14,33 @@ 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
|
||||
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++) {
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
27
src/test/java/TileTest.java
Normal file
27
src/test/java/TileTest.java
Normal 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());
|
||||
}
|
||||
}
|
||||
@@ -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<>();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user