Lots of work on saving, enemies and object positions are now saved

This commit is contained in:
Derek
2024-12-02 22:52:21 -06:00
parent a9aaed9c6f
commit 299257e0d9
22 changed files with 186 additions and 30 deletions

View File

@@ -3,8 +3,9 @@ extends Node
## SAVE DATA
#PERSISTENT DATA
var last_hit_path
var player_deaths
var enemies_killed
var player_deaths = 0
var enemies_killed = 0
var shots_fired = 0
#GAME DATA
var data_cleared
@@ -34,9 +35,8 @@ func save_persistent_data():
print("LAST HIT PATH " + str(last_hit_path))
file.store_var(last_hit_path)
file.store_var(player_deaths)
print("SAVING PLAYER DEATHS " + str(player_deaths))
file.store_var(enemies_killed)
print("SAVING ENEMIES KILLED " + str(enemies_killed))
file.store_var(shots_fired)
file.close()
@@ -49,6 +49,8 @@ func load_persistent_data():
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)
file.close()
else:
@@ -84,7 +86,20 @@ func save_game_data():
file.store_var(current_gun)
file.store_var(current_ammo)
file.store_var(reserve_ammo)
file.store_var(enemies)
#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()
@@ -122,7 +137,32 @@ func load_save_game_data():
current_gun = file.get_var()
current_ammo = file.get_var()
reserve_ammo = file.get_var()
enemies = file.get_var()
var current_nodes = get_tree().get_nodes_in_group("persist")
for i in current_nodes:
if i.get_class() == "CharacterBody3D":
i.die()
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)
print("NEW OBJECT : ",new_object)
new_object.position = Vector3(node_data["pos_x"],node_data["pos_y"],node_data["pos_z"])
new_object.rotation = Vector3(node_data["rot_x"],node_data["rot_y"],node_data["rot_z"])
print("NEW OBJECT PLACED AT ", new_object.position)
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
player.global_position = player_loc
@@ -146,12 +186,14 @@ func data_validate(file,variable):
else:
return null
func persistent_data_calc(variable,amount):
func null_data_check(variable,amount): #checks if value is null, adds the number to variable
print("VARIABLE ", variable)
if variable == null:
variable = amount
print(str(variable) +" "+ str(amount))
else:
variable += amount
return variable
func load_data():
load_persistent_data()