substantial work on saving and loading and hub mechanic
This commit is contained in:
@@ -12,8 +12,7 @@ var data_cleared
|
||||
var player_loc
|
||||
var player_rot
|
||||
var player_health
|
||||
var player_money
|
||||
var held_guns
|
||||
var player_velocity_cache
|
||||
var current_gun
|
||||
var current_ammo
|
||||
var reserve_ammo
|
||||
@@ -30,27 +29,56 @@ func _ready() -> void:
|
||||
enemies_killed = 0
|
||||
|
||||
func save_persistent_data():
|
||||
var level_control = get_tree().current_scene
|
||||
var player = level_control.player
|
||||
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.velocity)
|
||||
file.store_var(GameGlobals.money)
|
||||
file.store_var(GameGlobals.health)
|
||||
file.store_var(GameGlobals.high_score)
|
||||
file.store_var(player_deaths)
|
||||
file.store_var(enemies_killed)
|
||||
file.store_var(shots_fired)
|
||||
var held_guns = save_resource_path(GameGlobals.held_guns)
|
||||
file.store_var(held_guns)
|
||||
file.store_var(GameGlobals.gun_ammo)
|
||||
file.store_var(GameGlobals.ammo_reserve)
|
||||
file.store_var(GameGlobals.current_gun_index)
|
||||
file.store_var(data_cleared)
|
||||
|
||||
file.close()
|
||||
|
||||
func load_persistent_data():
|
||||
var level_control = get_tree().current_scene
|
||||
var player = level_control.player
|
||||
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()
|
||||
player_velocity_cache = file.get_var()
|
||||
GameGlobals.money = set_nulls_zero(file.get_var())
|
||||
print("MONEY : ", GameGlobals.money)
|
||||
GameGlobals.health = file.get_var()
|
||||
print("HEALTH : ", GameGlobals.health)
|
||||
GameGlobals.high_score = file.get_var()
|
||||
print("HIGH SCORE : ",GameGlobals.high_score)
|
||||
player_deaths = set_nulls_zero(file.get_var())
|
||||
print("PLAYER DEATHS : " + str(player_deaths))
|
||||
enemies_killed = file.get_var()
|
||||
enemies_killed = set_nulls_zero(file.get_var())
|
||||
print("ENEMIES KILLED : " + str(enemies_killed))
|
||||
shots_fired = file.get_var()
|
||||
shots_fired = set_nulls_zero(file.get_var())
|
||||
print("SHOTS FIRED ",shots_fired)
|
||||
|
||||
var held_guns_encoded = file.get_var()
|
||||
GameGlobals.held_guns = load_resource_path(held_guns_encoded)
|
||||
GameGlobals.gun_ammo = file.get_var()
|
||||
GameGlobals.ammo_reserve = file.get_var()
|
||||
GameGlobals.current_gun_index = file.get_var()
|
||||
if GameGlobals.current_gun_index != null:
|
||||
get_tree().current_scene.gun_spawn(GameGlobals.current_gun_index)
|
||||
|
||||
data_cleared = file.get_var()
|
||||
file.close()
|
||||
else:
|
||||
@@ -70,18 +98,6 @@ func save_game_data(level_name):
|
||||
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 = player.gun_ammo
|
||||
reserve_ammo = player.ammo_reserve
|
||||
|
||||
#SAVE DATA
|
||||
file.store_var(player_health)
|
||||
file.store_var(player_money)
|
||||
file.store_var(held_guns)
|
||||
file.store_var(current_gun)
|
||||
|
||||
#save enemies
|
||||
var objects = get_tree().get_nodes_in_group("persist")
|
||||
@@ -111,6 +127,7 @@ func clear_persistent_data():
|
||||
file.store_var(null)
|
||||
file.store_var(null)
|
||||
file.store_var(null)
|
||||
file.store_var(null)
|
||||
|
||||
print("PERSISTENT DATA CLEARED")
|
||||
file.close()
|
||||
@@ -131,11 +148,6 @@ func load_save_game_data(level_name):
|
||||
|
||||
#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()
|
||||
var current_nodes = get_tree().get_nodes_in_group("persist")
|
||||
for i in current_nodes:
|
||||
if i.is_in_group("enemy"):
|
||||
@@ -167,17 +179,7 @@ func load_save_game_data(level_name):
|
||||
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)
|
||||
|
||||
|
||||
file.close()
|
||||
await get_tree().create_timer(1).timeout #need to fix this
|
||||
SignalBus.emit_signal("game_loaded")
|
||||
@@ -191,6 +193,12 @@ func data_validate(file,variable):
|
||||
else:
|
||||
return null
|
||||
|
||||
func set_nulls_zero(variable):
|
||||
if variable == null:
|
||||
return 0
|
||||
else:
|
||||
return variable
|
||||
|
||||
func null_data_check(variable,amount): #checks if value is null, adds the number to variable
|
||||
if variable == null:
|
||||
variable = amount
|
||||
@@ -209,11 +217,14 @@ func save_resource_path(array):
|
||||
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
|
||||
if array != null:
|
||||
var final_array = []
|
||||
for i in array:
|
||||
var i_loaded = load(i)
|
||||
final_array.append(i_loaded)
|
||||
return final_array
|
||||
else:
|
||||
return []
|
||||
|
||||
func shot_fired():
|
||||
shots_fired += 1 #null_data_check(shots_fired, 1)
|
||||
|
||||
Reference in New Issue
Block a user