-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveManager.gd
More file actions
156 lines (133 loc) · 5.17 KB
/
Copy pathSaveManager.gd
File metadata and controls
156 lines (133 loc) · 5.17 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
extends Node
var new_slot_saved_state = null
var saved_state: Dictionary
var current_slot: int = -1
var slot_progress: Array[float] = []
var options: Dictionary[String, float] = {}
var max_total_research_points := -1
func _ready() -> void:
if FileAccess.file_exists("user://misc.save"):
load_meta_from_file()
else:
slot_progress.resize(3)
for i in range(0, 3):
slot_progress[i] = -1.0
func get_slot_progress(slot: int) -> float:
return slot_progress[slot]
func get_filename(slot: int) -> String:
return "user://slot%s.save" % slot
func save_to_file() -> void:
var save_file = FileAccess.open(get_filename(current_slot), FileAccess.WRITE)
var json_string = JSON.stringify(saved_state)
save_file.store_line(json_string)
var player_research = saved_state["player"]["resource_totals"][Player.Res.Research]
slot_progress[current_slot] = float(player_research) / max_total_research_points
save_meta_to_file()
func save_meta_to_file() -> void:
var save_file = FileAccess.open("user://misc.save", FileAccess.WRITE)
var json_string = JSON.stringify({
"progress": slot_progress,
"options": options
})
save_file.store_line(json_string)
func load_meta_from_file() -> void:
var save_file = FileAccess.open("user://misc.save", FileAccess.READ)
var json_string = save_file.get_line()
var json = JSON.new()
json.parse(json_string)
slot_progress.assign(json.data["progress"])
options.assign(json.data.get("options", {}))
func load_from_file() -> void:
if not FileAccess.file_exists(get_filename(current_slot)):
saved_state = new_slot_saved_state
return
var save_file = FileAccess.open(get_filename(current_slot), FileAccess.READ)
var json_string = save_file.get_line()
var json = JSON.new()
var parse_result = json.parse(json_string)
if not parse_result == OK:
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
saved_state = new_slot_saved_state
else:
saved_state = json.data
func delete_slot(slot: int):
DirAccess.remove_absolute(get_filename(slot))
slot_progress[slot] = -1.0
save_meta_to_file()
func save_state(player: Player, fish: Node2D, anomalies: Node2D):
saved_state = {
"player": save_player(player),
"fish": save_fish(fish),
"anomalies": save_anomalies(anomalies),
"fish_study": save_fish_study()
}
if new_slot_saved_state == null:
new_slot_saved_state = saved_state
if max_total_research_points == -1:
calc_total_research_points(fish)
func load_state(player: Player, fish: Node2D, anomalies: Node2D) -> void:
load_player(saved_state["player"], player)
load_fish(saved_state["fish"], fish)
load_anomalies(saved_state["anomalies"], anomalies)
load_fish_study(saved_state["fish_study"])
func save_player(player: Player) -> Dictionary:
return {
"stats": player.stats.duplicate(),
"max_stats": player.max_stats.duplicate(),
"resources": player.resources.duplicate(),
"resource_totals": player.resource_totals.duplicate(),
"upgrade_levels": player.upgrade_levels.duplicate()
}
func load_player(data: Dictionary, player: Player) -> void:
player.stats.assign(data["stats"].duplicate())
player.max_stats.assign(data["max_stats"].duplicate())
player.resources.assign(data["resources"].duplicate())
player.resource_totals.assign(data["resource_totals"].duplicate())
player.upgrade_levels.assign(data["upgrade_levels"].duplicate())
func save_fish(fish_node: Node2D) -> Array[Dictionary]:
var fish_data: Array[Dictionary] = []
for fish: Fish in fish_node.get_children():
fish_data.append({
"name": fish.name,
"studied": fish.studied,
"study_progress": fish.study_progress,
"facing_left": fish.facing_left,
"position": var_to_str(fish.position)
})
return fish_data
func load_fish(data: Array, fish_node: Node2D) -> void:
for fish_data in data:
var fish = fish_node.find_child(fish_data["name"])
fish.studied = fish_data["studied"]
fish.study_progress = fish_data["study_progress"]
fish.facing_left = fish_data["facing_left"]
fish.position = str_to_var(fish_data["position"])
func save_anomalies(anomaly_node: Node2D) -> Array[Dictionary]:
var anomaly_data: Array[Dictionary] = []
for anomaly: Anomaly in anomaly_node.get_children():
anomaly_data.append({
"name": anomaly.name,
"studied": anomaly.studied,
"study_progress": anomaly.study_progress
})
return anomaly_data
func load_anomalies(data: Array, anomaly_node: Node2D) -> void:
for anomaly_data in data:
var anomaly = anomaly_node.find_child(anomaly_data["name"])
anomaly.studied = anomaly_data["studied"]
anomaly.study_progress = anomaly_data["study_progress"]
func save_fish_study() -> Dictionary:
return {
"times_studied": Study.times_studied.duplicate()
}
func load_fish_study(data: Dictionary):
Study.times_studied = {}
for key in data["times_studied"]:
Study.times_studied[int(key)] = int(data["times_studied"][key])
func calc_total_research_points(fish_node: Node2D):
max_total_research_points = 0
var times_studied: Dictionary[FishStudy.FishType, int] = {}
for fish: Fish in fish_node.get_children():
times_studied.set(fish.fish_type, times_studied.get(fish.fish_type, 0) + 1)
max_total_research_points += FishStudy.get_study_reward(fish, times_studied[fish.fish_type])
print(max_total_research_points)