226 lines
6.2 KiB
GDScript
226 lines
6.2 KiB
GDScript
extends Node
|
|
|
|
## SAVE DATA
|
|
#PERSISTENT DATA
|
|
var last_hit_path
|
|
var player_deaths = 0
|
|
var enemies_killed = 0
|
|
var shots_fired = 0
|
|
|
|
#GAME DATA
|
|
var data_cleared
|
|
var player_loc
|
|
var player_rot
|
|
var player_health
|
|
var player_money
|
|
var held_guns
|
|
var current_gun
|
|
var current_ammo
|
|
var reserve_ammo
|
|
|
|
## SAVE DATA PATHS
|
|
var persistent_save_path = "user://persistent_data.save"
|
|
|
|
func _ready() -> void:
|
|
SignalBus.shot_fired.connect(shot_fired)
|
|
|
|
if player_deaths == null:
|
|
player_deaths = 0
|
|
if enemies_killed == null:
|
|
enemies_killed = 0
|
|
|
|
func save_persistent_data():
|
|
var file = FileAccess.open(persistent_save_path, FileAccess.WRITE)
|
|
print("LAST HIT PATH " + str(last_hit_path))
|
|
file.store_var(last_hit_path)
|
|
file.store_var(player_deaths)
|
|
file.store_var(enemies_killed)
|
|
file.store_var(shots_fired)
|
|
file.store_var(data_cleared)
|
|
|
|
file.close()
|
|
|
|
func load_persistent_data():
|
|
if FileAccess.file_exists(persistent_save_path):
|
|
var file = FileAccess.open(persistent_save_path, FileAccess.READ)
|
|
last_hit_path = file.get_var()
|
|
print("CROWN PARENT : " + str(last_hit_path))
|
|
player_deaths = file.get_var()
|
|
print("PLAYER DEATHS : " + str(player_deaths))
|
|
enemies_killed = file.get_var()
|
|
print("ENEMIES KILLED : " + str(enemies_killed))
|
|
shots_fired = file.get_var()
|
|
print("SHOTS FIRED ",shots_fired)
|
|
data_cleared = file.get_var()
|
|
file.close()
|
|
else:
|
|
print("no data saved...")
|
|
last_hit_path = null
|
|
player_deaths = null
|
|
enemies_killed = null
|
|
|
|
|
|
func save_game_data(level_name):
|
|
var level_control = get_tree().current_scene
|
|
var player = level_control.player
|
|
var game_save_path = str("user://",level_name,"_gamesave.save")
|
|
var file = FileAccess.open(game_save_path, FileAccess.WRITE)
|
|
|
|
#ASSIGN DATA TO VARIABLES
|
|
data_cleared = false
|
|
player_loc = player.global_position
|
|
player_rot = player.global_rotation
|
|
player_health = level_control.health
|
|
player_money = level_control.money
|
|
held_guns = save_resource_path(level_control.held_guns)
|
|
current_gun = level_control.current_gun_index
|
|
current_ammo = level_control.ammo_current
|
|
reserve_ammo = level_control.ammo_reserve
|
|
|
|
#SAVE DATA
|
|
file.store_var(player_health)
|
|
file.store_var(player_money)
|
|
file.store_var(held_guns)
|
|
file.store_var(current_gun)
|
|
file.store_var(current_ammo)
|
|
file.store_var(reserve_ammo)
|
|
|
|
#save enemies
|
|
var objects = get_tree().get_nodes_in_group("persist")
|
|
for object in objects:
|
|
if object.scene_file_path.is_empty():
|
|
print("persistent node '%s' is not an instanced scene, skipped" % object.name)
|
|
continue
|
|
# Check the node has a save function.
|
|
if !object.has_method("save"):
|
|
print("persistent node '%s' is missing a save() function, skipped" % object.name)
|
|
continue
|
|
var object_data = object.call("save")
|
|
var json_string = JSON.stringify(object_data)
|
|
file.store_line(json_string)
|
|
|
|
file.close()
|
|
|
|
save_persistent_data()
|
|
|
|
func clear_save_game_data():
|
|
data_cleared = true
|
|
save_persistent_data()
|
|
|
|
func clear_persistent_data():
|
|
var file = FileAccess.open(persistent_save_path, FileAccess.WRITE)
|
|
file.store_var(null)
|
|
file.store_var(null)
|
|
file.store_var(null)
|
|
file.store_var(null)
|
|
|
|
print("PERSISTENT DATA CLEARED")
|
|
file.close()
|
|
|
|
func check_save_game_exists(level_name):
|
|
var game_save_path = str("user://",level_name,"_gamesave.save")
|
|
if FileAccess.file_exists(game_save_path):
|
|
return true
|
|
else:
|
|
return false
|
|
|
|
func load_save_game_data(level_name):
|
|
var level_control = get_tree().current_scene
|
|
var player = level_control.player
|
|
var game_save_path = str("user://",level_name,"_gamesave.save")
|
|
if FileAccess.file_exists(game_save_path):
|
|
var file = FileAccess.open(game_save_path, FileAccess.READ)
|
|
|
|
#GET DATA
|
|
if !data_cleared:
|
|
player_health = file.get_var()
|
|
player_money = file.get_var()
|
|
var held_guns_encoded = file.get_var()
|
|
held_guns = load_resource_path(held_guns_encoded)
|
|
current_gun = file.get_var()
|
|
current_ammo = file.get_var()
|
|
reserve_ammo = file.get_var()
|
|
var current_nodes = get_tree().get_nodes_in_group("persist")
|
|
for i in current_nodes:
|
|
if i.is_in_group("enemy"):
|
|
i.quiet_remove()
|
|
else:
|
|
i.queue_free()
|
|
|
|
while file.get_position() < file.get_length():
|
|
var json_string = 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())
|
|
continue
|
|
var node_data = json.data
|
|
var new_object = load(node_data["filename"]).instantiate()
|
|
|
|
get_node(node_data["parent"]).add_child(new_object)
|
|
|
|
|
|
#apply transform if available
|
|
if node_data["pos_x"] != null:
|
|
new_object.position = Vector3(node_data["pos_x"],node_data["pos_y"],node_data["pos_z"])
|
|
if node_data["rot_x"] != null:
|
|
new_object.rotation = Vector3(node_data["rot_x"],node_data["rot_y"],node_data["rot_z"])
|
|
|
|
#apply other save data
|
|
for i in node_data.keys():
|
|
if i == "filename" or i == "pos_x" or i == "pos_y" or i == "pos_z":
|
|
continue
|
|
new_object.set(i,node_data[i])
|
|
|
|
|
|
#APPLY DATA
|
|
level_control.health = player_health
|
|
level_control.money = player_money
|
|
level_control.held_guns = held_guns
|
|
if player.gun:
|
|
player.gun.queue_free()
|
|
if current_gun != null:
|
|
level_control.gun_spawn(current_gun)
|
|
level_control.ammo_current = current_ammo
|
|
level_control.ammo_reserve = reserve_ammo
|
|
|
|
file.close()
|
|
await get_tree().create_timer(1).timeout #need to fix this
|
|
SignalBus.emit_signal("game_loaded")
|
|
GameGlobals.game_loaded = true
|
|
else:
|
|
print("no data saved...")
|
|
|
|
func data_validate(file,variable):
|
|
if file.get_var(variable):
|
|
return file.get_var(variable)
|
|
else:
|
|
return null
|
|
|
|
func null_data_check(variable,amount): #checks if value is null, adds the number to variable
|
|
if variable == null:
|
|
variable = amount
|
|
else:
|
|
variable += amount
|
|
|
|
return variable
|
|
|
|
func save_resource_path(array):
|
|
var final_array = []
|
|
for i in array:
|
|
if i is PackedScene:
|
|
var resource_path = i.resource_path
|
|
if resource_path != "":
|
|
final_array.append(resource_path)
|
|
return final_array
|
|
|
|
func load_resource_path(array):
|
|
var final_array = []
|
|
for i in array:
|
|
var i_loaded = load(i)
|
|
final_array.append(i_loaded)
|
|
return final_array
|
|
|
|
func shot_fired():
|
|
null_data_check(shots_fired, 1)
|