diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/TriggerCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/TriggerCell.java index 5072fd9..bcf54d9 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/TriggerCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/TriggerCell.java @@ -30,4 +30,6 @@ public class TriggerCell extends InteractionCell { public boolean getActive() { return isActive; } + + public void setActive(boolean active) { this.isActive = active; } } diff --git a/src/main/java/ruscz/game/gameOfProd/Map/Map.java b/src/main/java/ruscz/game/gameOfProd/Map/Map.java index 8385817..c7c41a8 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/Map.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/Map.java @@ -22,6 +22,7 @@ public class Map { private int idExit; private ImageCell imageCell; private int[] canvasSize; + private WindowOfFreeMove window; private static Scanner scanner; @@ -230,6 +231,8 @@ public class Map { return interactionCells; } + public LinkedList getTriggerCells() { return triggerCells; } + public void setIdExit(int id) { this.idExit = id; } diff --git a/src/main/java/ruscz/game/gameOfProd/Map/MapManager.java b/src/main/java/ruscz/game/gameOfProd/Map/MapManager.java index d26c1b1..455a1c9 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/MapManager.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/MapManager.java @@ -3,6 +3,8 @@ package ruscz.game.gameOfProd.Map; import ruscz.game.gameOfProd.Cells.Block; import ruscz.game.gameOfProd.Cells.EventMoveToCell; import ruscz.game.gameOfProd.Cells.TypeOfCell; +import ruscz.game.gameOfProd.Save.LoadManager; +import ruscz.game.gameOfProd.Save.SaveManager; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import java.awt.*; @@ -14,10 +16,16 @@ public class MapManager { private WindowOfFreeMove.Canvas canvas; private LinkedList maps; private Map actualMap; + private SaveManager saveManager; + private WindowOfFreeMove window; + private LoadManager loadManager; - public MapManager(LinkedList maps, WindowOfFreeMove.Canvas canvas) { + public MapManager(LinkedList maps, WindowOfFreeMove.Canvas canvas, WindowOfFreeMove window) { this.canvas = canvas; this.maps = maps; + this.window = window; + this.saveManager = new SaveManager(this); + this.loadManager = new LoadManager(this); } public void setMap(Map map) { @@ -37,6 +45,11 @@ public class MapManager { return actualMap; } + public LinkedList getMaps() { return maps; } + + public WindowOfFreeMove getWindow() { return window; } + + public LoadManager getLoadManager() { return loadManager; } public void createMap() { for (int i = 0; i < actualMap.getMapArray().length; i++) { @@ -89,6 +102,7 @@ public class MapManager { actualMap.getPerson().set(-1, -1); break; } + saveManager.saveGame(); } } } diff --git a/src/main/java/ruscz/game/gameOfProd/Save/LoadManager.java b/src/main/java/ruscz/game/gameOfProd/Save/LoadManager.java new file mode 100644 index 0000000..f29ce07 --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Save/LoadManager.java @@ -0,0 +1,156 @@ +package ruscz.game.gameOfProd.Save; + +import ruscz.game.gameOfProd.Cells.TriggerCell; +import ruscz.game.gameOfProd.Map.Map; +import ruscz.game.gameOfProd.Map.MapManager; +import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; + +import java.awt.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.LinkedList; +import java.util.Scanner; + +public class LoadManager { + private MapManager mapManager; + private WindowOfFreeMove window; + + public LoadManager(MapManager mapManager) { + this.mapManager = mapManager; + this.window = mapManager.getWindow(); + } + + public void loadNewGame() { + loadMaps(); + } + + public void loadSavedGame() { + loadNewGame(); + loadPerson(); + loadEvents(); + loadVillage(); + loadInventory(); + } + + public void clearSave() { + try { + FileWriter writer = new FileWriter("src/main/resources/save/savedGame/person.txt"); + writer.write(""); + writer = new FileWriter("src/main/resources/save/savedGame/events.txt"); + writer.write(""); + writer = new FileWriter("src/main/resources/save/savedGame/village.txt"); + writer.write(""); + writer = new FileWriter("src/main/resources/save/savedGame/inventory.txt"); + writer.write(""); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void loadMaps() { + try { + Rectangle[] rec = new Rectangle[12]; + int CELL_SIZE = WindowOfFreeMove.CELL_SIZE; + int count = 0; + for (int i = 0; i < rec.length / 4; i++) { + for (int j = 0; j < rec.length / 3; j++) { + rec[count] = new Rectangle(CELL_SIZE * i, CELL_SIZE * j, CELL_SIZE, CELL_SIZE); + count++; + } + } + + LinkedList mapLinkedList = window.getMapLinkedList(); + File mapsFile = new File("src/main/resources/save/newGame/maps.txt"); + Scanner scanner = new Scanner(mapsFile); + + while (scanner.hasNextLine()) { + String mapName = scanner.nextLine(); + Map map = Map.loadMapFromFile(mapName, window); + map.getPerson().setImagePerson("main", rec); + map.setName(mapName); + mapLinkedList.add(map); + } + scanner.close(); + for (Map map : mapLinkedList) { + map.linkMaps(map, window); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + private void loadPerson() { + try { + File personFile = new File("src/main/resources/save/savedGame/person.txt"); + Scanner scanner = new Scanner(personFile); + + while (scanner.hasNextLine()) { + String data = scanner.nextLine(); + switch (data) { + case "": + window.setMapManager(Map.findMapByName(getValue(scanner.nextLine()), window)); + mapManager.getActualMap().getPerson().set(Integer.parseInt(getValue(scanner.nextLine())), Integer.parseInt(getValue(scanner.nextLine()))); + break; + } + } + scanner.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + private void loadEvents() { + try { + File eventFile = new File("src/main/resources/save/savedGame/events.txt"); + Scanner scanner = new Scanner(eventFile); + Map map = null; + + while (scanner.hasNextLine()) { + if (scanner.nextLine().equals("")) { + map = Map.findMapByName(getValue(scanner.nextLine()), window); + System.out.println(map.getName()); + } + else if (scanner.nextLine().contains("")) { + TriggerCell triggerCell = map.findTriggerByXY(Integer.parseInt(getValue(scanner.nextLine())), Integer.parseInt(getValue(scanner.nextLine()))); + triggerCell.setActive(Boolean.parseBoolean(getValue(scanner.nextLine()))); + System.out.println(triggerCell.getX()); + } + else { + scanner.nextLine(); + } + } + scanner.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + private void loadVillage() { + try { + File villageFile = new File("src/main/resources/save/savedGame/village.txt"); + Scanner scanner = new Scanner(villageFile); + scanner.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + //TODO: loadVillage + } + + private void loadInventory() { + try { + File inventoryFile = new File("src/main/resources/save/savedGame/inventory.txt"); + Scanner scanner = new Scanner(inventoryFile); + scanner.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + //TODO: loadInventory + } + + private String getValue(String line) { + return line.substring(line.indexOf(">") + 1, line.lastIndexOf("<")); + } +} diff --git a/src/main/java/ruscz/game/gameOfProd/Save/SaveManager.java b/src/main/java/ruscz/game/gameOfProd/Save/SaveManager.java new file mode 100644 index 0000000..136f3d0 --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Save/SaveManager.java @@ -0,0 +1,95 @@ +package ruscz.game.gameOfProd.Save; + +import ruscz.game.gameOfProd.Cells.Person; +import ruscz.game.gameOfProd.Cells.TriggerCell; +import ruscz.game.gameOfProd.Map.Map; +import ruscz.game.gameOfProd.Map.MapManager; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.LinkedList; + +public class SaveManager { + private MapManager mapManager; + + public SaveManager(MapManager mapManager) { + this.mapManager = mapManager; + } + + public void saveGame() { + savePerson(); + saveEvents(); + saveVillage(); + saveInventory(); + } + + private void savePerson() { + try { + FileWriter writer = new FileWriter("src/main/resources/save/savedGame/person.txt"); + Person person = mapManager.getActualMap().getPerson(); + String location = + "" + System.lineSeparator() + + " " + mapManager.getActualMap().getName() + "" + System.lineSeparator() + + " " + person.getX() + "" + System.lineSeparator() + + " " + person.getY() + "" + System.lineSeparator() + + ""; + writer.write(location); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void saveEvents() { + try { + LinkedList maps = mapManager.getMaps(); + FileWriter writer = new FileWriter("src/main/resources/save/savedGame/events.txt"); + String mapInfo = ""; + for (Map map : maps) { + mapInfo += + "" + System.lineSeparator() + + " " + map.getName() + "" + System.lineSeparator() + + " " + System.lineSeparator(); + for (TriggerCell triggerCell : map.getTriggerCells()) { + String event = + " " + System.lineSeparator() + + " " + triggerCell.getX() + "" + System.lineSeparator() + + " " + triggerCell.getY() + "" + System.lineSeparator() + + " " + triggerCell.getActive() + "" + System.lineSeparator() + + " " + System.lineSeparator(); + mapInfo += event; + } + mapInfo += + " " + System.lineSeparator() + + "" + System.lineSeparator(); + + } + writer.write(mapInfo); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void saveVillage() { + try { + FileWriter writer = new FileWriter("src/main/resources/save/savedGame/village.txt"); + String village = "TODO: Village"; + writer.write(village); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void saveInventory() { + try { + FileWriter writer = new FileWriter("src/main/resources/save/savedGame/inventory.txt"); + String inventory = "TODO: Inventory"; + writer.write(inventory); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index eda6c3d..589d656 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -162,63 +162,26 @@ public class WindowOfFreeMove extends JFrame { } public void start() { - Rectangle[] rec = new Rectangle[12]; - int count = 0; - for (int i = 0; i < rec.length / 4; i++) { - for (int j = 0; j < rec.length / 3; j++) { - rec[count] = new Rectangle(CELL_SIZE * i, CELL_SIZE * j, CELL_SIZE, CELL_SIZE); - count++; - } - } - - Map m1 = Map.loadMapFromFile("m1", this); - m1.getPerson().setImagePerson("main", rec); - m1.setName("m1"); - - Map m2 = Map.loadMapFromFile("m2", this); - m2.getPerson().setImagePerson("main", rec); - m2.setName("m2"); - - Map m3 = Map.loadMapFromFile("m3", this); - m3.getPerson().setImagePerson("main", rec); - m3.setName("m3"); - - Map m4 = Map.loadMapFromFile("m4", this); - m4.getPerson().setImagePerson("main", rec); - m4.setName("m4"); - - Map m5 = Map.loadMapFromFile("m5", this); - m5.getPerson().setImagePerson("main", rec); - m5.setName("m5"); - - Map m6 = Map.loadMapFromFile("m6", this); - m6.getPerson().setImagePerson("main", rec); - m6.setName("m6"); mapLinkedList = new LinkedList<>(); - mapLinkedList.add(m1); - mapLinkedList.add(m2); - mapLinkedList.add(m3); - mapLinkedList.add(m4); - mapLinkedList.add(m5); - mapLinkedList.add(m6); + mapManager = new MapManager(mapLinkedList, canvas, this); - m1.linkMaps(m1, this); - m2.linkMaps(m2, this); - m3.linkMaps(m3, this); - m4.linkMaps(m4, this); - m5.linkMaps(m5, this); - m6.linkMaps(m6, this); + // to load a new game + mapManager.getLoadManager().clearSave(); + mapManager.getLoadManager().loadNewGame(); + Map m5 = Map.findMapByName("m5", this); + setMapManager(m5); - mapManager = new MapManager(mapLinkedList, canvas); + //to load a saved game + //mapManager.getLoadManager().loadSavedGame(); + + Map m6 = Map.findMapByName("m6", this); int startX = 1; int startY = 1; int endX = 8; int endY = 8; - setMapManager(m5); - camera.moveCamera(); mapManager.createMap(); isReadyToPaint = true; diff --git a/src/main/resources/save/newGame/maps.txt b/src/main/resources/save/newGame/maps.txt new file mode 100644 index 0000000..68cfd6b --- /dev/null +++ b/src/main/resources/save/newGame/maps.txt @@ -0,0 +1,6 @@ +m1 +m2 +m3 +m4 +m5 +m6 \ No newline at end of file diff --git a/src/main/resources/save/savedGame/events.txt b/src/main/resources/save/savedGame/events.txt new file mode 100644 index 0000000..5807f8a --- /dev/null +++ b/src/main/resources/save/savedGame/events.txt @@ -0,0 +1,50 @@ + + m1 + + + + + m2 + + + 7 + 2 + true + + + + + m3 + + + 2 + 5 + true + + + + + m4 + + + 3 + 3 + true + + + + + m5 + + + + + m6 + + + 11 + 11 + true + + + diff --git a/src/main/resources/save/savedGame/inventory.txt b/src/main/resources/save/savedGame/inventory.txt new file mode 100644 index 0000000..7082420 --- /dev/null +++ b/src/main/resources/save/savedGame/inventory.txt @@ -0,0 +1 @@ +TODO: Inventory \ No newline at end of file diff --git a/src/main/resources/save/savedGame/person.txt b/src/main/resources/save/savedGame/person.txt new file mode 100644 index 0000000..5249531 --- /dev/null +++ b/src/main/resources/save/savedGame/person.txt @@ -0,0 +1,5 @@ + + m5 + 4 + 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 new file mode 100644 index 0000000..1b4bc7b --- /dev/null +++ b/src/main/resources/save/savedGame/village.txt @@ -0,0 +1 @@ +TODO: Village \ No newline at end of file