From 1f184dfe06c8383de1ba70a8be4e7cf47d735b60 Mon Sep 17 00:00:00 2001 From: musilto9 Date: Wed, 27 May 2020 17:51:07 +0200 Subject: [PATCH] loadMapFromFile tests --- .../ruscz/game/gameOfProd/Cells/Block.java | 11 ++ .../ruscz/game/gameOfProd/Cells/Cell.java | 9 ++ .../game/gameOfProd/Cells/ChestCell.java | 12 ++ .../ruscz/game/gameOfProd/Cells/DoorCell.java | 20 ++-- .../gameOfProd/Cells/EventMoveToCell.java | 14 +++ .../gameOfProd/Cells/InteractionCell.java | 11 ++ .../java/ruscz/game/gameOfProd/Cells/Npc.java | 18 +++ .../gameOfProd/Cells/RandomEncounterCell.java | 9 +- .../game/gameOfProd/Cells/TriggerCell.java | 9 ++ .../java/ruscz/game/gameOfProd/Map/Map.java | 24 +++- src/main/resources/save/savedGame/events.txt | 65 +++++++++++ src/main/resources/save/savedGame/person.txt | 6 + src/main/resources/save/savedGame/village.txt | 1 + .../ruscz/game/gameOfProd/Map/MapTest.java | 109 ++++++++++++++++++ 14 files changed, 303 insertions(+), 15 deletions(-) create mode 100644 src/test/java/ruscz/game/gameOfProd/Map/MapTest.java diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Block.java b/src/main/java/ruscz/game/gameOfProd/Cells/Block.java index 91753f9..a460492 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Block.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Block.java @@ -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); + } } \ No newline at end of file diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Cell.java b/src/main/java/ruscz/game/gameOfProd/Cells/Cell.java index f230b03..7e4536c 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Cell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Cell.java @@ -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; + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/ChestCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/ChestCell.java index a48592e..ec870ae 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/ChestCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/ChestCell.java @@ -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; } diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/DoorCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/DoorCell.java index b92a853..eedda3e 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/DoorCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/DoorCell.java @@ -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; diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/EventMoveToCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/EventMoveToCell.java index 6b00360..d287bf5 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/EventMoveToCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/EventMoveToCell.java @@ -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()); + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java index 5eb6d3e..3d67b6e 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java @@ -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; } diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java index 630b152..e5ee78e 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java @@ -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; } diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/RandomEncounterCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/RandomEncounterCell.java index 3fd63e1..bf3db18 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/RandomEncounterCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/RandomEncounterCell.java @@ -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; + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/TriggerCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/TriggerCell.java index b354943..74b255a 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/TriggerCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/TriggerCell.java @@ -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; } diff --git a/src/main/java/ruscz/game/gameOfProd/Map/Map.java b/src/main/java/ruscz/game/gameOfProd/Map/Map.java index 9792b09..103c2d0 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/Map.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/Map.java @@ -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: diff --git a/src/main/resources/save/savedGame/events.txt b/src/main/resources/save/savedGame/events.txt index e69de29..a18be6b 100644 --- a/src/main/resources/save/savedGame/events.txt +++ b/src/main/resources/save/savedGame/events.txt @@ -0,0 +1,65 @@ + + tutorial + + + 11 + 6 + false + + + + + openspace + + + 6 + 4 + true + + + 14 + 16 + false + + + 15 + 7 + true + + + + + office + + + 4 + 2 + false + + + 1 + 9 + true + + + + + boss + + + 4 + 18 + true + + + + + treasure + + + 6 + 1 + true + + + diff --git a/src/main/resources/save/savedGame/person.txt b/src/main/resources/save/savedGame/person.txt index e69de29..2c7cd7c 100644 --- a/src/main/resources/save/savedGame/person.txt +++ b/src/main/resources/save/savedGame/person.txt @@ -0,0 +1,6 @@ + + office + 8 + 7 + 3 + \ No newline at end of file diff --git a/src/main/resources/save/savedGame/village.txt b/src/main/resources/save/savedGame/village.txt index e69de29..1b4bc7b 100644 --- a/src/main/resources/save/savedGame/village.txt +++ b/src/main/resources/save/savedGame/village.txt @@ -0,0 +1 @@ +TODO: Village \ No newline at end of file diff --git a/src/test/java/ruscz/game/gameOfProd/Map/MapTest.java b/src/test/java/ruscz/game/gameOfProd/Map/MapTest.java new file mode 100644 index 0000000..5cd0d71 --- /dev/null +++ b/src/test/java/ruscz/game/gameOfProd/Map/MapTest.java @@ -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 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; + } +}