2020-04-15 23:11:08 +02:00

191 lines
7.1 KiB
Java

package ruscz.game.gameOfProd.Save;
import ruscz.game.gameOfProd.Cells.ChestCell;
import ruscz.game.gameOfProd.Cells.Npc;
import ruscz.game.gameOfProd.Cells.TriggerCell;
import ruscz.game.gameOfProd.Inventory.InventoryManager;
import ruscz.game.gameOfProd.Inventory.Item;
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();
loadNpcs();
}
public void loadSavedGame() {
loadNewGame();
loadPerson();
loadEvents();
loadInventory();
loadVillage();
}
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 loadNpcs() {
try {
File npcsFile = new File("src/main/resources/save/newGame/npcs.txt");
Scanner scanner = new Scanner(npcsFile);
while (scanner.hasNextLine()) {
String name = scanner.nextLine();
File npcFile = new File("src/main/resources/characters/" + name + ".txt");
Scanner sc = new Scanner(npcFile);
String[] position = sc.nextLine().split(" ");
Npc npc = new Npc(Integer.parseInt(position[0]), Integer.parseInt(position[1]), name, Map.findMapByName(position[2], window), mapManager);
Map.findMapByName(position[2], window).setNpcs(npc);
}
} 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())));
mapManager.getActualMap().getPerson().setDirectionSight(Integer.parseInt(getValue(scanner.nextLine())));
mapManager.getActualMap().setIdExit(100);
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()) {
String data = scanner.nextLine();
if (data.equals("<map>")) {
map = Map.findMapByName(getValue(scanner.nextLine()), window);
}
else if (data.contains("<trigger>")) {
TriggerCell triggerCell = map.findTriggerByXY(Integer.parseInt(getValue(scanner.nextLine())), Integer.parseInt(getValue(scanner.nextLine())));
triggerCell.setActive(Boolean.parseBoolean(getValue(scanner.nextLine())));
}
else if (data.contains("<chest>")) {
ChestCell chestCell = map.findChestByXY(Integer.parseInt(getValue(scanner.nextLine())), Integer.parseInt(getValue(scanner.nextLine())));
chestCell.setActive(Boolean.parseBoolean(getValue(scanner.nextLine())));
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void loadInventory() {
try {
File inventoryFile = new File("src/main/resources/save/savedGame/inventory.txt");
Scanner scanner = new Scanner(inventoryFile);
while (scanner.hasNextLine()) {
String data = scanner.nextLine();
switch (data) {
case "<item>":
Item item = new Item(getValue(scanner.nextLine()), Integer.parseInt(getValue(scanner.nextLine())), Integer.parseInt(getValue(scanner.nextLine())), getValue(scanner.nextLine()));
InventoryManager.addItem(item);
break;
}
}
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 String getValue(String line) {
return line.substring(line.indexOf(">") + 1, line.lastIndexOf("<"));
}
}