2020-04-04 17:37:08 +02:00

157 lines
5.3 KiB
Java

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("<"));
}
}