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.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.LinkedList;
|
|
||||||
public class World {
|
public class World {
|
||||||
// Används för att printa mappen.
|
// Används för att printa mappen.
|
||||||
private String[][] map = {
|
private String[][] map = {
|
||||||
@@ -13,21 +14,33 @@ public class World {
|
|||||||
{"@","@","@","@","@","@","@","@","@","@","@"}
|
{"@","@","@","@","@","@","@","@","@","@","@"}
|
||||||
};
|
};
|
||||||
HashMap<Integer, Tile> tileMap = new HashMap<>();
|
HashMap<Integer, Tile> tileMap = new HashMap<>();
|
||||||
private LinkedList<Entity> entityList = new LinkedList<>();
|
private HashMap<Entity, Position> entityMap = new HashMap<>();
|
||||||
public Entity addEntityToList(Entity e) {
|
public final int TILE_SIZE = 25;
|
||||||
entityList.add(e);
|
|
||||||
|
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) {
|
public void addTile(Tile tile, int xCoordinate, int yCoordinate) {
|
||||||
int key = xCoordinate * 10 + yCoordinate;
|
int key = xCoordinate * 10 + yCoordinate;
|
||||||
tileMap.put(key, tile);
|
tileMap.put(key, tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
M in array is monster
|
M in array is monster
|
||||||
C in array is chest
|
C in array is chest
|
||||||
X in array is player.
|
X in array is player.
|
||||||
*/
|
*/
|
||||||
public void printMap() {
|
public void printMap() {
|
||||||
for (int i = 0; i < map.length; i++) {
|
for (int i = 0; i < map.length; i++) {
|
||||||
for (int j = 0; j < map[i].length; j++) {
|
for (int j = 0; j < map[i].length; j++) {
|
||||||
@@ -40,8 +53,8 @@ public class World {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinkedList<Entity> getEntityList() {
|
public HashMap<Entity, Position> getEntityList() {
|
||||||
return entityList;
|
return entityMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile getTileAtPosition(int xCoordinate, int yCoordinate) {
|
public Tile getTileAtPosition(int xCoordinate, int yCoordinate) {
|
||||||
@@ -49,23 +62,18 @@ public class World {
|
|||||||
return tileMap.get(tileKey);
|
return tileMap.get(tileKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity getEntityAtPosition(int xCoordinate, int yCoordinate) {
|
public Entity getEntityAtPosition(Position position) {
|
||||||
if (!entityList.isEmpty()) {
|
for (HashMap.Entry<Entity, Position> entry : entityMap.entrySet()) {
|
||||||
for (int i = 0; i < entityList.size(); i++) {
|
Position pos = entry.getValue();
|
||||||
int xEntityCord = entityList.get(i).getX();
|
Entity e = entry.getKey();
|
||||||
int yEntityCord = entityList.get(i).getY();
|
if (pos.equals(position)) {
|
||||||
if (xCoordinate == xEntityCord && yCoordinate == yEntityCord) {
|
return e;
|
||||||
return entityList.get(i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
public void changePosition(Object Character, int xCoordinate, int yCoordinate) {
|
|
||||||
if (Character instanceof Player) {
|
public void changePosition(Entity Character, Position newPosition) {
|
||||||
map[(xCoordinate * 2) + 1][yCoordinate + 1] = "X";
|
entityMap.put(Character, newPosition);
|
||||||
} else if (Character instanceof Enemy) {
|
|
||||||
map[(xCoordinate * 2) + 1][yCoordinate + 1] = "M";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
class WorldTest {
|
class WorldTest {
|
||||||
|
final int TILE_SIZE = 25;
|
||||||
@Test
|
@Test
|
||||||
void addTileToMapTest() {
|
void addTileToMapTest() {
|
||||||
World test = new World();
|
World test = new World();
|
||||||
Tile tile new Tile();
|
Tile tile = new Tile("test", 3, "test");
|
||||||
int xCoordinate = 1;
|
int xCoordinate = 1;
|
||||||
int yCoordinate = 1;
|
int yCoordinate = 1;
|
||||||
test.addTile(tile, xCoordinate, yCoordinate);
|
test.addTile(tile, xCoordinate, yCoordinate);
|
||||||
Tile tileTest = test.getTileAtPosition(1, 1);
|
Tile tileTest = test.getTileAtPosition(1, 1);
|
||||||
assertEquals(tile, tileTest);
|
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