SaverLoader base
This commit is contained in:
parent
ad7cb69355
commit
d4acde846c
|
@ -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()
|
||||||
|
|
|
@ -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:
|
if Input.is_action_pressed("fire_missile") and fireBulletTimer.time_left == 0 and PlayerStats.missiles > 0 and PlayerStats.missiles_unlocked:
|
||||||
fire_missile()
|
fire_missile()
|
||||||
|
|
||||||
func fire_bullet():
|
func fire_bullet():
|
||||||
var bullet = Utils.instance_scene_on_main(PlayerBullet, muzzle.global_position)
|
var bullet = Utils.instance_scene_on_main(PlayerBullet, muzzle.global_position)
|
||||||
bullet.velocity = Vector2.RIGHT.rotated(playerGun.rotation) * bullet_speed
|
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():
|
if wall_axis == 0 or is_on_floor():
|
||||||
state = MOVE
|
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):
|
func _on_Hurtbox_hit(damage):
|
||||||
if not invincible:
|
if not invincible:
|
||||||
PlayerStats.health -= damage
|
PlayerStats.health -= damage
|
||||||
|
|
|
@ -122,7 +122,9 @@ tracks/1/keys = {
|
||||||
} ]
|
} ]
|
||||||
}
|
}
|
||||||
|
|
||||||
[node name="Player" type="KinematicBody2D"]
|
[node name="Player" type="KinematicBody2D" groups=[
|
||||||
|
"Persists",
|
||||||
|
]]
|
||||||
collision_mask = 2
|
collision_mask = 2
|
||||||
script = ExtResource( 2 )
|
script = ExtResource( 2 )
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,7 @@ config/icon="res://icon.png"
|
||||||
Utils="*res://Utils.gd"
|
Utils="*res://Utils.gd"
|
||||||
ResourceLoader="*res://ResourceLoader.gd"
|
ResourceLoader="*res://ResourceLoader.gd"
|
||||||
Events="*res://Events.gd"
|
Events="*res://Events.gd"
|
||||||
|
SaverLoader="*res://SaverLoader.gd"
|
||||||
|
|
||||||
[display]
|
[display]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue