1
0
Fork 0

SaverLoader base

This commit is contained in:
Felipe M 2021-05-22 17:53:59 +02:00
parent ad7cb69355
commit d4acde846c
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
4 changed files with 50 additions and 2 deletions

View File

@ -0,0 +1,36 @@
extends Node
const save_path = "user://savegame.save"
func save_game():
var save_game = File.new()
save_game.open(save_path, File.WRITE)
var persistingNodes = get_tree().get_nodes_in_group("Persists")
for node in persistingNodes:
var nodeData = node.save()
save_game.store_line(to_json(nodeData))
save_game.close()
func load_game():
var save_game = File.new()
if not save_game.file_exists(save_path):
return
var persistingNodes = get_tree().get_nodes_in_group("Persists")
for node in persistingNodes:
node.queue_free()
save_game.open(save_path, File.READ)
while not save_game.eof_reached():
var current_line = parse_json(save_game.get_line())
if current_line != null:
var newNode = load(current_line["filename"]).instance()
get_node(current_line["parent"]).add_child(newNode, true)
newNode.position = Vector2(current_line["position_x"], current_line["position_y"])
for property in current_line.keys():
if property != "filename" and property != "parent" and property != "position_x" and property != "position_y":
newNode.set(property, current_line[property])
save_game.close()

View File

@ -87,7 +87,7 @@ func _physics_process(delta):
if Input.is_action_pressed("fire_missile") and fireBulletTimer.time_left == 0 and PlayerStats.missiles > 0 and PlayerStats.missiles_unlocked:
fire_missile()
func fire_bullet():
var bullet = Utils.instance_scene_on_main(PlayerBullet, muzzle.global_position)
bullet.velocity = Vector2.RIGHT.rotated(playerGun.rotation) * bullet_speed
@ -233,6 +233,15 @@ func wall_detach(delta, wall_axis):
if wall_axis == 0 or is_on_floor():
state = MOVE
func save():
var data = {
"filename": get_filename(),
"parent": get_parent().get_path(),
"position_x": position.x,
"position_y": position.y
}
return data
func _on_Hurtbox_hit(damage):
if not invincible:
PlayerStats.health -= damage

View File

@ -122,7 +122,9 @@ tracks/1/keys = {
} ]
}
[node name="Player" type="KinematicBody2D"]
[node name="Player" type="KinematicBody2D" groups=[
"Persists",
]]
collision_mask = 2
script = ExtResource( 2 )

View File

@ -41,6 +41,7 @@ config/icon="res://icon.png"
Utils="*res://Utils.gd"
ResourceLoader="*res://ResourceLoader.gd"
Events="*res://Events.gd"
SaverLoader="*res://SaverLoader.gd"
[display]