-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveGame.java
105 lines (95 loc) · 3.23 KB
/
SaveGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.File;
import java.util.Scanner;
import java.io.PrintStream;
import java.io.FileNotFoundException;
import java.util.TreeMap;
import java.util.Map;
public class SaveGame{
//saves a player
public static void save(Player p, String fileName) throws FileNotFoundException{
try{
File file = new File(fileName);
PrintStream stream = new PrintStream(file);
//save the player name
stream.println(p.name);
//save the playerClass
stream.println(p.characterClass);
//save the level
stream.println(p.level);
//save the experience
stream.println(p.xp);
//save the food
stream.println(p.food);
//save the gold
stream.println(p.gold);
//save a list of the items
for(Map.Entry<Integer, Item> entry : p.inventory.itemList.entrySet()){
stream.print(entry.getKey() + " ");
}
stream.println();
//save a list of the equipped items
for(Map.Entry<Integer, Item> entry : p.inventory.itemList.entrySet()){
if(entry.getValue().equipped ){
stream.print(entry.getKey() + " ");
}
}
}
catch (FileNotFoundException e){
throw new FileNotFoundException();
}
}
public static Player load(String fileName) throws FileNotFoundException{
try{
File file = new File(fileName);
Scanner input = new Scanner(file);
//read the player name
String name = input.nextLine();
//read the player class
String characterClass = input.nextLine();
//make the player
Player p = new Player(name, characterClass);
//Load the level
int level = input.nextInt();
for(int i = 1; i < level; i++){
p.giveXP(100000000);
}
//load the current xp
int xp = input.nextInt();
p.giveXP(xp);
//load the food
int food = input.nextInt();
p.giveFood(food);
//load the gold
int gold = input.nextInt();
p.giveGold(gold);
//Initialize a loot list
Loot L = new Loot();
//Get a list of items add them to the player's inventory
String itemString = "";
input.nextLine();
if(input.hasNextLine()){
itemString = input.nextLine();
}
Scanner items = new Scanner(itemString);
while(items.hasNextInt()){
int uniqueID = items.nextInt();
p.inventory.add(L.getItem(uniqueID));
}
//equip items
String equippedString = "";
if(input.hasNextLine()){
equippedString = input.nextLine();
}
Scanner equipped = new Scanner(equippedString);
while(equipped.hasNextInt()){
int uniqueID = equipped.nextInt();
p.inventory.equip(uniqueID, p);
}
p.inventory.list();
return p;
}
catch (FileNotFoundException e){
throw new FileNotFoundException();
}
}
}