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

@@ -43,6 +43,7 @@ var engine_time_scale_cache : float = 1.0
# Called when the node enters the scene tree for the first time.
func _ready():
#refresh_scene()
##LOAD DATA
SaveLoad.load_persistent_data()
SaveLoad.load_save_game_data()
@@ -54,7 +55,9 @@ func _ready():
var crown_spawn = crown.instantiate()
if SaveLoad.last_hit_path:
var crown_target = get_node(SaveLoad.last_hit_path)
crown_target.add_child(crown_spawn)
if crown_target:
crown_target.add_child(crown_spawn)
crown_spawn.position = Vector3(0,2,0)
else:
get_tree().get_root().add_child(crown_spawn)
#global randomize function

View File

@@ -103,6 +103,7 @@ func fire():
player.recoil.add_recoil(Vector3(0,recoil_amount.y,recoil_amount.z),10,10)
player.recoil.add_gun_recoil(recoil_amount.x)
player.velocity += player.bullet_ray.global_basis * Vector3(0,0, kick_amount)
SaveLoad.shots_fired += 1
func reload():
if level_control.ammo_current[gun_index] < max_ammo and player.gun.anim_player.get_current_animation() != "reload" and level_control.ammo_reserve[gun_index] > 0:
@@ -115,12 +116,6 @@ func reload():
else:
level_control.ammo_current[gun_index] = 1
#func spawn_mag():
#var instance_mag = mag.instantiate()
#instance_mag.position = mag_ejector.global_position
#instance_mag.transform.basis = mag_ejector.global_transform.basis
#get_tree().get_root().add_child(instance_mag)
func pellet_spawn():
var pellets_remaining = pellets_per_shot
while pellets_remaining > 0:

View File

@@ -1,4 +1,4 @@
extends RigidBody3D
extends RigidbodyGeneric
@export var broken_object : Resource
@export var break_velocity : float = 10

View File

@@ -10,7 +10,18 @@ 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)
pass
func save():
var save_dict = {
"filename" : get_scene_file_path(),
"parent" : get_parent().get_path(),
"pos_x" : position.x,
"pos_y" : position.y,
"pos_z" : position.z,
"rot_x" : rotation.x,
"rot_y" : rotation.y,
"rot_z" : rotation.z,
}
return save_dict

View File

@@ -96,6 +96,7 @@ func shoot(delta):
player.recoil.add_recoil(Vector3(0,recoil_amount.y,recoil_amount.z),10,10)
player.recoil.add_gun_recoil(recoil_amount.x)
#player.velocity += player.bullet_ray.global_basis * Vector3(0,0, kick_amount)
SaveLoad.shots_fired += 1
if fire_mode != 0:
cycle_count -= 1
@@ -118,6 +119,7 @@ func spawn_mag():
var instance_mag = mag.instantiate()
instance_mag.position = mag_ejector.global_position
instance_mag.transform.basis = mag_ejector.global_transform.basis
instance_mag.linear_velocity += transform.basis * Vector3(0, -20, 0) + player.velocity
get_tree().get_root().add_child(instance_mag)
func spawn_casing():

View File

@@ -18,6 +18,7 @@ var player
func _ready():
add_to_group("pickup")
add_to_group("spawned")
add_to_group("persist")
#find player
player = level_control.player
rand_amt = randi_range(25,100)
@@ -69,3 +70,16 @@ func _on_timer_timeout() -> void:
collision_shape.disabled = true
await get_tree().create_timer(1).timeout
self.queue_free()
func save():
var save_dict = {
"filename" : get_scene_file_path(),
"parent" : get_parent().get_path(),
"pos_x" : position.x,
"pos_y" : position.y,
"pos_z" : position.z,
"rot_x" : rotation.x,
"rot_y" : rotation.y,
"rot_z" : rotation.z
}
return save_dict

View File

@@ -4,7 +4,7 @@ extends RigidBody3D
# Called when the node enters the scene tree for the first time.
func _ready():
linear_velocity += transform.basis * Vector3(0, -20, 0)
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):

View File

@@ -487,3 +487,17 @@ func hit(damage, fired_by, target_type):
health_indicator.color = Color(0.471, 0, 0, .25)
await get_tree().create_timer(.15).timeout
health_indicator.color = Color(0.471, 0, 0, 0)
func save():
var save_dict = {
"filename" : get_scene_file_path(),
"parent" : get_parent().get_path(),
"pos_x" : position.x,
"pos_y" : position.y,
"pos_z" : position.z,
"rot_x" : rotation.x,
"rot_y" : rotation.y,
"rot_z" : rotation.z,
"health" : level_control.health
}
return save_dict

View File

@@ -129,7 +129,7 @@ func shoot(delta):
audio_fire.pitch_scale = 1 + rng.randf_range(-fire_pitch_scale_amt,fire_pitch_scale_amt)
audio_fire.play()
anim_player.play("shoot") #actual bullet spawn triggered by animation
#knocks player back, use on bigger guns player.velocity += player.bullet_ray.global_basis * Vector3(0,0, kick_amount)
SaveLoad.shots_fired += 1
if fire_mode != 0:
cycle_count -= 1

View File

@@ -0,0 +1,24 @@
extends RigidBody3D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
add_to_group("persist")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func save():
var save_dict = {
"filename" : get_scene_file_path(),
"parent" : get_parent().get_path(),
"pos_x" : position.x,
"pos_y" : position.y,
"pos_z" : position.z,
"rot_x" : rotation.x,
"rot_y" : rotation.y,
"rot_z" : rotation.z,
}
return save_dict

View File

@@ -95,6 +95,7 @@ func shoot(delta):
instance_bullet.player_position = player.global_position
get_tree().get_root().add_child(instance_bullet)
player.recoil.add_recoil(recoil_amount,10,10)
SaveLoad.shots_fired += 1
if fire_mode != 0:
cycle_count -= 1

View File

@@ -66,6 +66,8 @@ func _process(delta):
for i in enemies:
i.loot_amount = 20 #assign loot to the last enemy drop from this section
i.last_enemy = true
else:
enemy_in_room_killed()
func enemy_in_room_killed():
var enemy_count = 0

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()

View File

@@ -220,5 +220,20 @@ func drop_loot(number_of_drops):
can_die = true
func save():
var save_dict = {
"filename" : get_scene_file_path(),
"parent" : get_parent().get_path(),
"pos_x" : position.x,
"pos_y" : position.y,
"pos_z" : position.z,
"rot_x" : rotation.x,
"rot_y" : rotation.y,
"rot_z" : rotation.z,
"health" : health
}
return save_dict
func _on_area_3d_body_entered(body: Node3D) -> void:
pass # Replace with function body.

View File

@@ -119,6 +119,7 @@ func fire():
spawn_casing()
player.recoil.add_recoil(Vector3(0,recoil_amount.y,recoil_amount.z),10,10)
player.recoil.add_gun_recoil(recoil_amount.x)
SaveLoad.shots_fired += 1
if fire_mode != 0:
cycle_count -= 1

View File

@@ -11,7 +11,7 @@ var gun_already_held = false
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
add_to_group("persist")
# Called every frame. 'delta' is the elapsed time since the previous frame.
@@ -32,3 +32,16 @@ func picked_up():
level_control.player.gun.anim_player.play("swap_out")
level_control.gun_spawn(weapon_id)
queue_free()
func save():
var save_dict = {
"filename" : get_scene_file_path(),
"parent" : get_parent().get_path(),
"pos_x" : position.x,
"pos_y" : position.y,
"pos_z" : position.z,
"rot_x" : rotation.x,
"rot_y" : rotation.y,
"rot_z" : rotation.z,
}
return save_dict