loadMapFromFile tests
This commit is contained in:
parent
5b759f9adc
commit
1f184dfe06
@ -5,6 +5,7 @@ import ruscz.game.gameOfProd.Map.Map;
|
||||
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Block extends Cell {
|
||||
private ImageCell imageCell;
|
||||
@ -51,4 +52,14 @@ public class Block extends Cell {
|
||||
g.drawImage(imageCell.getImagesOfTiles().get(imageCell.getImageFromCellMap("BLOCK")), getX() * getSize(),
|
||||
getY() * getSize(), getSize(), getSize(), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
Block block = (Block) o;
|
||||
return paint == block.paint &&
|
||||
Objects.equals(partial, block.partial);
|
||||
}
|
||||
}
|
||||
@ -35,4 +35,13 @@ public class Cell {
|
||||
public void paint(Graphics2D g) {
|
||||
g.setColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Cell cell = (Cell) o;
|
||||
return x == cell.x &&
|
||||
y == cell.y;
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ChestCell extends InteractionCell {
|
||||
@ -136,6 +137,17 @@ public class ChestCell extends InteractionCell {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
ChestCell cell = (ChestCell) o;
|
||||
return showItem == cell.showItem &&
|
||||
isActive == cell.isActive &&
|
||||
Objects.equals(chestInventory, cell.chestInventory);
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
@ -63,17 +63,15 @@ public class DoorCell extends InteractionCell {
|
||||
}
|
||||
}
|
||||
|
||||
// public void setSoundOpen(String soundOpen) {
|
||||
// this.soundOpen = soundOpen;
|
||||
// }
|
||||
//
|
||||
// public void setSoundClose(String soundClose) {
|
||||
// this.soundClose = soundClose;
|
||||
// }
|
||||
//
|
||||
// public void setSoundLocked(String soundLocked) {
|
||||
// this.soundLocked = soundLocked;
|
||||
// }
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
DoorCell doorCell = (DoorCell) o;
|
||||
return isLocked == doorCell.isLocked &&
|
||||
directionVert == doorCell.directionVert;
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return isOpen;
|
||||
|
||||
@ -16,6 +16,8 @@ public class EventMoveToCell extends Cell {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Map getMap() { return map; }
|
||||
|
||||
public EventMoveToCell(int id, int directionOut, int x, int y, Map map, WindowOfFreeMove window, boolean paint){
|
||||
super(x,y, WindowOfFreeMove.CELL_SIZE, Color.green);
|
||||
this.id = id;
|
||||
@ -51,4 +53,16 @@ public class EventMoveToCell extends Cell {
|
||||
getY() * getSize(), getSize(), getSize(), null);
|
||||
// g.drawString(Integer.toString(getId()), getX()* WindowOfFreeMove.CELL_SIZE, getY()* WindowOfFreeMove.CELL_SIZE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
EventMoveToCell that = (EventMoveToCell) o;
|
||||
return id == that.id &&
|
||||
directionOut == that.directionOut &&
|
||||
paint == that.paint &&
|
||||
map.getName().equals(that.getMap().getName());
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class InteractionCell extends Cell {
|
||||
@ -279,6 +280,16 @@ public class InteractionCell extends Cell {
|
||||
getY() * getSize(), getSize(), getSize(), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
InteractionCell that = (InteractionCell) o;
|
||||
return paint == that.paint &&
|
||||
Objects.equals(message, that.message);
|
||||
}
|
||||
|
||||
public ImageCell getImageCell() {
|
||||
return imageCell;
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Npc extends Person {
|
||||
@ -114,6 +115,23 @@ public class Npc extends Person {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
Npc npc = (Npc) o;
|
||||
return direction == npc.direction &&
|
||||
width == npc.width &&
|
||||
height == npc.height &&
|
||||
offsetX == npc.offsetX &&
|
||||
offsetY == npc.offsetY &&
|
||||
widthCoef == npc.widthCoef &&
|
||||
heightCoef == npc.heightCoef &&
|
||||
Objects.equals(name, npc.name) &&
|
||||
Objects.equals(imageNpc, npc.imageNpc);
|
||||
}
|
||||
|
||||
public void setDirection(int direction) {
|
||||
this.direction = direction;
|
||||
}
|
||||
|
||||
@ -15,5 +15,12 @@ public class RandomEncounterCell extends Cell {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
RandomEncounterCell that = (RandomEncounterCell) o;
|
||||
return level == that.level;
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,6 +32,15 @@ public class TriggerCell extends InteractionCell {
|
||||
getY() * getSize(), getSize(), getSize(), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
TriggerCell that = (TriggerCell) o;
|
||||
return isActive == that.isActive;
|
||||
}
|
||||
|
||||
public boolean getActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
@ -72,7 +72,8 @@ public class Map {
|
||||
map.canvasSize[1] = Integer.parseInt(size[1]);
|
||||
int y = 0;
|
||||
while (scanner.hasNextLine()) {
|
||||
loadMap(map, size, y, window);
|
||||
String data = scanner.nextLine();
|
||||
loadMap(map, data, size, y, window);
|
||||
y++;
|
||||
}
|
||||
return map;
|
||||
@ -82,8 +83,7 @@ public class Map {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void loadMap(Map map, String[] size, int y, WindowOfFreeMove window) {
|
||||
String data = scanner.nextLine();
|
||||
public static void loadMap(Map map, String data, String[] size, int y, WindowOfFreeMove window) {
|
||||
String[] line = data.split(" ");
|
||||
for (int x = 0; x < Integer.parseInt(size[0]); x++) {
|
||||
TypeOfCell cellType;
|
||||
@ -408,6 +408,13 @@ public class Map {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EventMoveToCell findMoveToByXY(int x, int y) {
|
||||
for (EventMoveToCell cell : moveToCells)
|
||||
if (x == cell.getX() && y == cell.getY())
|
||||
return cell;
|
||||
return null;
|
||||
}
|
||||
|
||||
public Npc findNpcByName(String name) {
|
||||
for (Npc npc : npcs) {
|
||||
if (npc.getName().equals(name)) return npc;
|
||||
@ -415,6 +422,17 @@ public class Map {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Cell findCellByXY(int x, int y) {
|
||||
Cell cell;
|
||||
cell = findBlockByXY(x, y);
|
||||
if (cell == null) cell = findInteractionByXY(x, y);
|
||||
if (cell == null) cell = findTriggerByXY(x, y);
|
||||
if (cell == null) cell = findChestByXY(x, y);
|
||||
if (cell == null) cell = findDoorByXY(x, y);
|
||||
if (cell == null) cell = findMoveToByXY(x, y);
|
||||
return cell;
|
||||
}
|
||||
|
||||
public int[] directionSightSwitch(int x, int y, int directionSight) {
|
||||
switch (directionSight) {
|
||||
case 1:
|
||||
|
||||
@ -0,0 +1,65 @@
|
||||
<map>
|
||||
<mapName>tutorial</mapName>
|
||||
<events>
|
||||
<trigger>
|
||||
<x>11</x>
|
||||
<y>6</y>
|
||||
<active>false</active>
|
||||
</trigger>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>openspace</mapName>
|
||||
<events>
|
||||
<trigger>
|
||||
<x>6</x>
|
||||
<y>4</y>
|
||||
<active>true</active>
|
||||
</trigger>
|
||||
<trigger>
|
||||
<x>14</x>
|
||||
<y>16</y>
|
||||
<active>false</active>
|
||||
</trigger>
|
||||
<chest>
|
||||
<x>15</x>
|
||||
<y>7</y>
|
||||
<active>true</active>
|
||||
</chest>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>office</mapName>
|
||||
<events>
|
||||
<trigger>
|
||||
<x>4</x>
|
||||
<y>2</y>
|
||||
<active>false</active>
|
||||
</trigger>
|
||||
<chest>
|
||||
<x>1</x>
|
||||
<y>9</y>
|
||||
<active>true</active>
|
||||
</chest>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>boss</mapName>
|
||||
<events>
|
||||
<trigger>
|
||||
<x>4</x>
|
||||
<y>18</y>
|
||||
<active>true</active>
|
||||
</trigger>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>treasure</mapName>
|
||||
<events>
|
||||
<trigger>
|
||||
<x>6</x>
|
||||
<y>1</y>
|
||||
<active>true</active>
|
||||
</trigger>
|
||||
</events>
|
||||
</map>
|
||||
@ -0,0 +1,6 @@
|
||||
<location>
|
||||
<map>office</map>
|
||||
<x>8</x>
|
||||
<y>7</y>
|
||||
<direction>3</direction>
|
||||
</location>
|
||||
@ -0,0 +1 @@
|
||||
TODO: Village
|
||||
109
src/test/java/ruscz/game/gameOfProd/Map/MapTest.java
Normal file
109
src/test/java/ruscz/game/gameOfProd/Map/MapTest.java
Normal file
@ -0,0 +1,109 @@
|
||||
package ruscz.game.gameOfProd.Map;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import ruscz.game.gameOfProd.Cells.*;
|
||||
import ruscz.game.gameOfProd.Dialog.Dialog;
|
||||
import ruscz.game.gameOfProd.Dialog.DialogBar;
|
||||
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Scanner;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class MapTest {
|
||||
|
||||
@Test
|
||||
public void loadMapFromFile_parseMapFileToMapArray_mapArrayParsedCorrectly() {
|
||||
//Arrange
|
||||
Map testMap;
|
||||
TypeOfCell[][] mapArray;
|
||||
WindowOfFreeMove window;
|
||||
|
||||
//Act
|
||||
mapArray = parseMap("m6");
|
||||
window = new WindowOfFreeMove(new JFrame());
|
||||
window.setCHAPTER("999");
|
||||
testMap = Map.loadMapFromFile("m6", window);
|
||||
|
||||
//Assert
|
||||
for (int i = 0; i < mapArray.length; i++) {
|
||||
for (int j = 0; j < mapArray[0].length; j++) {
|
||||
Assertions.assertEquals(mapArray[i][j], testMap.getMapArray()[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Stream<Arguments> argumentsStream() {
|
||||
WindowOfFreeMove window = new WindowOfFreeMove(new JFrame());
|
||||
Dialog dialog = new Dialog(new DialogBar());
|
||||
Map map = new Map(new TypeOfCell[1][1]);
|
||||
map.setName("test");
|
||||
return Stream.of(
|
||||
Arguments.of(new Block(0, 0, map, "u", true), "BLOCK,u"),
|
||||
Arguments.of(new InteractionCell(0, 0, "test", dialog, map, window, true), "INTER,test"),
|
||||
Arguments.of(new TriggerCell(0, 0, "test", dialog, map, window, false), "TRIGGER,test,false")
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("argumentsStream")
|
||||
public void loadMapFromFile_parseStringToCorrectObject_MapObjectParsedCorrectly(Cell cell, String cellText) {
|
||||
//Arrange
|
||||
Map testMap;
|
||||
WindowOfFreeMove window;
|
||||
|
||||
//Act
|
||||
testMap = new Map(new TypeOfCell[1][1]);
|
||||
String[] size = {"1", "1"};
|
||||
window= new WindowOfFreeMove(new JFrame());
|
||||
Map.loadMap(testMap, cellText, size, 0, window);
|
||||
|
||||
//Assert
|
||||
Assertions.assertTrue(cell.equals(testMap.findCellByXY(0, 0)));
|
||||
}
|
||||
|
||||
|
||||
private TypeOfCell[][] parseMap(String mapName) {
|
||||
try {
|
||||
File file = new File("src/main/resources/maps/999/" + mapName + ".txt");
|
||||
Scanner scanner = new Scanner(file);
|
||||
String[] size = scanner.nextLine().split(" ");
|
||||
TypeOfCell[][] mapArray = new TypeOfCell[Integer.parseInt(size[0])][Integer.parseInt(size[1])];
|
||||
int i = 0;
|
||||
while (scanner.hasNextLine()) {
|
||||
String[] line = scanner.nextLine().split(" ");
|
||||
for (int j = 0; j < line.length; j++) {
|
||||
if (line[j].equals("EMPTY")) {
|
||||
mapArray[j][i] = TypeOfCell.EMPTY;
|
||||
}
|
||||
else if (line[j].toUpperCase().contains("BLOCK")) {
|
||||
mapArray[j][i] = TypeOfCell.BLOCK;
|
||||
}
|
||||
else if (line[j].toUpperCase().contains("INTER") ||
|
||||
line[j].toUpperCase().contains("TRIGGER") ||
|
||||
line[j].toUpperCase().contains("DOOR") ||
|
||||
line[j].toUpperCase().contains("CHEST")) {
|
||||
mapArray[j][i] = TypeOfCell.EVENT;
|
||||
}
|
||||
else if (line[j].toUpperCase().contains("MOVETO")) {
|
||||
mapArray[j][i] = TypeOfCell.MOVETO;
|
||||
}
|
||||
else if (line[j].equals("SPAWN")) {
|
||||
mapArray[j][i] = TypeOfCell.SPAWNPOINT;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return mapArray;
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user