can save and load player loc/rot and held guns

This commit is contained in:
derek
2024-12-02 14:59:43 -06:00
parent 842cc7ecfd
commit eee708c7f1
9 changed files with 110 additions and 12 deletions

View File

@@ -48,7 +48,6 @@ func _ready():
#Spawn Crown
var crown_spawn = crown.instantiate()
print("LEVELMANAGER LAST HIT " + str(SaveLoad.last_hit_path))
if SaveLoad.last_hit_path:
var crown_target = get_node(SaveLoad.last_hit_path)
crown_target.add_child(crown_spawn)

View File

@@ -3,13 +3,14 @@ extends Node3D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
print("CROWN IN SCENE")
var target = get_node(SaveLoad.last_hit_path)
if target:
global_rotation = Vector3(0,0,0)
global_transform.origin = target.global_position + Vector3(0,2,0)
target_change()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func target_change():
var target = get_node(SaveLoad.last_hit_path)
if target:
global_rotation = Vector3(0,0,0)
global_transform.origin = target.global_position + Vector3(0,2,0)

View File

@@ -18,3 +18,7 @@ func _on_resume_pressed() -> void:
func _on_save__quit_pressed() -> void:
level_control.save_quit()
func _on_load_pressed() -> void:
SaveLoad.load_data()

View File

@@ -425,7 +425,7 @@ func enemy_killed():
SaveLoad.enemies_killed += 1
else:
SaveLoad.enemies_killed = 1
SaveLoad.save_persistent_data()
enemy_killed_audio.play()

View File

@@ -1,13 +1,22 @@
extends Node
## SAVE DATA
#Persistent
#PERSISTENT DATA
var last_hit_path
var player_deaths
var enemies_killed
#GAME DATA
var player_loc
var player_rot
var held_guns
var current_ammo
var reserve_ammo
## SAVE DATA PATHS
var persistent_save_path = "user://persistent_data.save"
var game_save_path = "user://game_save_data.save"
func _ready() -> void:
if player_deaths == null:
@@ -23,6 +32,8 @@ func save_persistent_data():
print("SAVING PLAYER DEATHS " + str(player_deaths))
file.store_var(enemies_killed)
print("SAVING ENEMIES KILLED " + str(enemies_killed))
file.close()
func load_persistent_data():
if FileAccess.file_exists(persistent_save_path):
@@ -33,17 +44,60 @@ func load_persistent_data():
print("PLAYER DEATHS : " + str(player_deaths))
enemies_killed = file.get_var()
print("ENEMIES KILLED : " + str(enemies_killed))
file.close()
else:
print("no data saved...")
last_hit_path = null
player_deaths = null
enemies_killed = null
func save_game_data():
pass
var level_control = get_tree().current_scene
var player = level_control.player
var file = FileAccess.open(game_save_path, FileAccess.WRITE)
#ASSIGN DATA TO VARIABLES
player_loc = player.global_position
player_rot = player.global_rotation
held_guns = save_resource_path(level_control.held_guns)
current_ammo = level_control.ammo_current
reserve_ammo = level_control.ammo_reserve
#SAVE DATA
file.store_var(player_loc)
file.store_var(player_rot)
file.store_var(held_guns)
file.store_var(current_ammo)
file.store_var(reserve_ammo)
file.close()
func load_save_game_data():
pass
var level_control = get_tree().current_scene
var player = level_control.player
if FileAccess.file_exists(game_save_path):
var file = FileAccess.open(game_save_path, FileAccess.READ)
#GET DATA
player_loc = file.get_var()
player_rot = file.get_var()
var held_guns_encoded = file.get_var()
held_guns = load_resource_path(held_guns_encoded)
current_ammo = file.get_var()
reserve_ammo = file.get_var()
#APPLY DATA
player.global_position = player_loc
player.global_rotation = player_rot
level_control.held_guns = held_guns
level_control.ammo_current = current_ammo
level_control.ammo_reserve = reserve_ammo
file.close()
else:
print("no data saved...")
func data_validate(file,variable):
if file.get_var(variable):
@@ -61,3 +115,22 @@ func persistent_data_calc(variable,amount):
func load_data():
load_persistent_data()
load_save_game_data()
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)
print("Resource Path: ", resource_path)
print("FINAL ARRAY ", final_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)
print("loaded ",final_array)
return final_array

View File

@@ -4,3 +4,4 @@ signal switch_changed()
signal switch_timeout()
signal enemy_hit()
signal enemy_killed()
signal king_killed()

View File

@@ -190,6 +190,9 @@ func die():
get_tree().get_root().add_child(particlespawn)
drop_loot(loot_amount)
if SaveLoad.last_hit_path == str(get_path()):
SaveLoad.last_hit_path = null
SignalBus.emit_signal("enemy_killed")
queue_free()