added save manager and load manager
This commit is contained in:
parent
93472adbb4
commit
aa07964b4c
@ -30,4 +30,6 @@ public class TriggerCell extends InteractionCell {
|
||||
public boolean getActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public void setActive(boolean active) { this.isActive = active; }
|
||||
}
|
||||
|
||||
@ -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<TriggerCell> getTriggerCells() { return triggerCells; }
|
||||
|
||||
public void setIdExit(int id) {
|
||||
this.idExit = id;
|
||||
}
|
||||
|
||||
@ -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<Map> maps;
|
||||
private Map actualMap;
|
||||
private SaveManager saveManager;
|
||||
private WindowOfFreeMove window;
|
||||
private LoadManager loadManager;
|
||||
|
||||
public MapManager(LinkedList<Map> maps, WindowOfFreeMove.Canvas canvas) {
|
||||
public MapManager(LinkedList<Map> 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<Map> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
156
src/main/java/ruscz/game/gameOfProd/Save/LoadManager.java
Normal file
156
src/main/java/ruscz/game/gameOfProd/Save/LoadManager.java
Normal file
@ -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<Map> 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 "<location>":
|
||||
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 = Map.findMapByName(getValue(scanner.nextLine()), window);
|
||||
System.out.println(map.getName());
|
||||
}
|
||||
else if (scanner.nextLine().contains("<event>")) {
|
||||
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("<"));
|
||||
}
|
||||
}
|
||||
95
src/main/java/ruscz/game/gameOfProd/Save/SaveManager.java
Normal file
95
src/main/java/ruscz/game/gameOfProd/Save/SaveManager.java
Normal file
@ -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 =
|
||||
"<location>" + System.lineSeparator() +
|
||||
" <map>" + mapManager.getActualMap().getName() + "</map>" + System.lineSeparator() +
|
||||
" <x>" + person.getX() + "</x>" + System.lineSeparator() +
|
||||
" <y>" + person.getY() + "</y>" + System.lineSeparator() +
|
||||
"</location>";
|
||||
writer.write(location);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void saveEvents() {
|
||||
try {
|
||||
LinkedList<Map> maps = mapManager.getMaps();
|
||||
FileWriter writer = new FileWriter("src/main/resources/save/savedGame/events.txt");
|
||||
String mapInfo = "";
|
||||
for (Map map : maps) {
|
||||
mapInfo +=
|
||||
"<map>" + System.lineSeparator() +
|
||||
" <mapName>" + map.getName() + "</mapName>" + System.lineSeparator() +
|
||||
" <events>" + System.lineSeparator();
|
||||
for (TriggerCell triggerCell : map.getTriggerCells()) {
|
||||
String event =
|
||||
" <event>" + System.lineSeparator() +
|
||||
" <x>" + triggerCell.getX() + "</x>" + System.lineSeparator() +
|
||||
" <y>" + triggerCell.getY() + "</y>" + System.lineSeparator() +
|
||||
" <active>" + triggerCell.getActive() + "</active>" + System.lineSeparator() +
|
||||
" </event>" + System.lineSeparator();
|
||||
mapInfo += event;
|
||||
}
|
||||
mapInfo +=
|
||||
" </events>" + System.lineSeparator() +
|
||||
"</map>" + 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
6
src/main/resources/save/newGame/maps.txt
Normal file
6
src/main/resources/save/newGame/maps.txt
Normal file
@ -0,0 +1,6 @@
|
||||
m1
|
||||
m2
|
||||
m3
|
||||
m4
|
||||
m5
|
||||
m6
|
||||
50
src/main/resources/save/savedGame/events.txt
Normal file
50
src/main/resources/save/savedGame/events.txt
Normal file
@ -0,0 +1,50 @@
|
||||
<map>
|
||||
<mapName>m1</mapName>
|
||||
<events>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>m2</mapName>
|
||||
<events>
|
||||
<event>
|
||||
<x>7</x>
|
||||
<y>2</y>
|
||||
<active>true</active>
|
||||
</event>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>m3</mapName>
|
||||
<events>
|
||||
<event>
|
||||
<x>2</x>
|
||||
<y>5</y>
|
||||
<active>true</active>
|
||||
</event>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>m4</mapName>
|
||||
<events>
|
||||
<event>
|
||||
<x>3</x>
|
||||
<y>3</y>
|
||||
<active>true</active>
|
||||
</event>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>m5</mapName>
|
||||
<events>
|
||||
</events>
|
||||
</map>
|
||||
<map>
|
||||
<mapName>m6</mapName>
|
||||
<events>
|
||||
<event>
|
||||
<x>11</x>
|
||||
<y>11</y>
|
||||
<active>true</active>
|
||||
</event>
|
||||
</events>
|
||||
</map>
|
||||
1
src/main/resources/save/savedGame/inventory.txt
Normal file
1
src/main/resources/save/savedGame/inventory.txt
Normal file
@ -0,0 +1 @@
|
||||
TODO: Inventory
|
||||
5
src/main/resources/save/savedGame/person.txt
Normal file
5
src/main/resources/save/savedGame/person.txt
Normal file
@ -0,0 +1,5 @@
|
||||
<location>
|
||||
<map>m5</map>
|
||||
<x>4</x>
|
||||
<y>3</y>
|
||||
</location>
|
||||
1
src/main/resources/save/savedGame/village.txt
Normal file
1
src/main/resources/save/savedGame/village.txt
Normal file
@ -0,0 +1 @@
|
||||
TODO: Village
|
||||
Loading…
x
Reference in New Issue
Block a user