diff --git a/UI/menu.gd b/UI/menu.gd index 6986d64..ddf84f9 100644 --- a/UI/menu.gd +++ b/UI/menu.gd @@ -6,12 +6,23 @@ var destination_map = { signal start signal save +signal load var instanciated_destinations = {} +func _ready() -> void: + $MarginContainer/Panel/VBoxContainer/Load.disabled = not FileAccess.file_exists("user://savegame.save") + func _on_button_pressed() -> void: emit_signal("start") - $MarginContainer/Panel/VBoxContainer/Button.text="continue" + $MarginContainer/Panel/VBoxContainer/Start.text="Continuer" + $MarginContainer/Panel/VBoxContainer/Save.disabled = false func _on_save_pressed() -> void: emit_signal("save") + $MarginContainer/Panel/VBoxContainer/Load.disabled = not FileAccess.file_exists("user://savegame.save") + + +func _on_load_pressed() -> void: + emit_signal("load") + _on_button_pressed() diff --git a/UI/menu.tscn b/UI/menu.tscn index 0ef0124..5a883ec 100644 --- a/UI/menu.tscn +++ b/UI/menu.tscn @@ -48,15 +48,22 @@ vertical_alignment = 1 [node name="HSeparator" type="HSeparator" parent="MarginContainer/Panel/VBoxContainer"] layout_mode = 2 -[node name="Button" type="Button" parent="MarginContainer/Panel/VBoxContainer"] +[node name="Start" type="Button" parent="MarginContainer/Panel/VBoxContainer"] layout_mode = 2 theme = ExtResource("3_feq31") -text = "start" +text = "Démarrer une nouvelle partie" [node name="Save" type="Button" parent="MarginContainer/Panel/VBoxContainer"] layout_mode = 2 theme = ExtResource("3_feq31") -text = "save" +disabled = true +text = "Sauvegarder" -[connection signal="pressed" from="MarginContainer/Panel/VBoxContainer/Button" to="." method="_on_button_pressed"] +[node name="Load" type="Button" parent="MarginContainer/Panel/VBoxContainer"] +layout_mode = 2 +theme = ExtResource("3_feq31") +text = "Charger" + +[connection signal="pressed" from="MarginContainer/Panel/VBoxContainer/Start" to="." method="_on_button_pressed"] [connection signal="pressed" from="MarginContainer/Panel/VBoxContainer/Save" to="." method="_on_save_pressed"] +[connection signal="pressed" from="MarginContainer/Panel/VBoxContainer/Load" to="." method="_on_load_pressed"] diff --git a/caracters/human.gd b/caracters/human.gd index a11f32d..ee1121a 100644 --- a/caracters/human.gd +++ b/caracters/human.gd @@ -83,6 +83,9 @@ func stop_interaction(): func get_feet_global_position(): return global_position + Vector2(0, 43) + +func set_feet_global_position(p : Vector2): + global_position = p - Vector2(0, 43) func pause_or_stop_interaction_clue(): if interactionZone: diff --git a/caracters/player/player_controler.gd b/caracters/player/player_controler.gd index 6bd552e..c303c7a 100644 --- a/caracters/player/player_controler.gd +++ b/caracters/player/player_controler.gd @@ -32,6 +32,7 @@ func _unhandled_input(event: InputEvent) -> void: pathFinder.destination = get_global_mouse_position() func _process(delta) -> void: + GameState.player_positions = human.get_feet_global_position() ray.target_position = human.last_facing_direction * 48 if human.last_facing_direction.y > 0 : diff --git a/game_state.gd b/game_state.gd index adc9ccf..0e92e6e 100644 --- a/game_state.gd +++ b/game_state.gd @@ -6,5 +6,26 @@ var hasChosenToMakeChoices = false var hasCompletedBobDialogue = false var playerIsDeaf = false -var player_positions = {"res://scenes/start.tscn" = Vector2(-171, -253)} -var current_scene = "res://scenes/start.tscn" +var current_scene = "outside" +var player_positions = Vector2(-171, -253) + +func save(): + var ret = {} + var thisScript: GDScript = get_script() + for propertyInfo in thisScript.get_script_property_list(): + var propertyName: String = propertyInfo.name + var propertyValue = get(propertyName) + ret[propertyName] = JSON.stringify(propertyValue) + return ret + +func load_save(data:Dictionary): + isUsingTouch = data.isUsingTouch == "true" + hasChosenNotToMakeChoices = data.hasChosenNotToMakeChoices == "true" + hasChosenToMakeChoices = data.hasChosenToMakeChoices == "true" + hasCompletedBobDialogue = data.hasCompletedBobDialogue == "true" + playerIsDeaf = data.playerIsDeaf == "true" + current_scene = data.current_scene + var strplps = (data.player_positions as String).replace("\"(", "").replace(")\"", "") + var parts = strplps.split(", ") + player_positions = Vector2(float(parts[0]), float(parts[1])) + print(player_positions) diff --git a/scenes/gam6026.tmp b/scenes/gam6026.tmp new file mode 100644 index 0000000..2698341 --- /dev/null +++ b/scenes/gam6026.tmp @@ -0,0 +1,13 @@ +[gd_scene load_steps=3 format=3 uid="uid://c645h6ap7niu1"] + +[ext_resource type="PackedScene" uid="uid://44wla7mlivqm" path="res://UI/menu.tscn" id="1_64gp8"] +[ext_resource type="Script" path="res://scenes/game.gd" id="1_b2ju7"] + +[node name="Game" type="Node2D"] +script = ExtResource("1_b2ju7") + +[node name="Menu" parent="." instance=ExtResource("1_64gp8")] + +[connection signal="load" from="Menu" to="." method="load_game"] +[connection signal="save" from="Menu" to="." method="save_game"] +[connection signal="start" from="Menu" to="." method="start_game"] diff --git a/scenes/game.gd b/scenes/game.gd index 290399a..5286b81 100644 --- a/scenes/game.gd +++ b/scenes/game.gd @@ -1,27 +1,39 @@ extends Node2D -var destination_map = { - "oustide":preload("res://scenes/outside.tscn"), -} - func _unhandled_input(event: InputEvent) -> void: if event.is_action_pressed("menu"): open_menu() # list of scenes -var current_scene = "menu" -var outside = null +var outside:Node2D = null +var dest_outside = preload("res://scenes/outside.tscn") var menu = null func start_game(): menu = get_child(0) if outside == null: - outside = destination_map["oustide"].instantiate() + outside = dest_outside.instantiate() add_child(outside) remove_child(menu) - current_scene = "outside" + GameState.current_scene = "outside" + outside.load_from_game_state() func open_menu(): remove_child(outside) add_child(menu) - current_scene = "menu" + +func load_game(): + var save_file = FileAccess.open("user://savegame.save", FileAccess.READ) + var json_string = "" + while save_file.get_position() < save_file.get_length(): + json_string += save_file.get_line() + var json = JSON.new() + if json.parse(json_string) == OK: + print("load", json.data) + GameState.load_save(json.data) + +func save_game(): + var save_file = FileAccess.open("user://savegame.save", FileAccess.WRITE) + save_file.store_line(JSON.stringify(GameState.save())) + print(OS.get_data_dir()) + save_file.close() diff --git a/scenes/game.tscn b/scenes/game.tscn index 5bdd043..2698341 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -8,4 +8,6 @@ script = ExtResource("1_b2ju7") [node name="Menu" parent="." instance=ExtResource("1_64gp8")] +[connection signal="load" from="Menu" to="." method="load_game"] +[connection signal="save" from="Menu" to="." method="save_game"] [connection signal="start" from="Menu" to="." method="start_game"] diff --git a/scenes/outside.gd b/scenes/outside.gd new file mode 100644 index 0000000..f78be78 --- /dev/null +++ b/scenes/outside.gd @@ -0,0 +1,9 @@ +extends Node2D + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + load_from_game_state() + +func load_from_game_state(): + $world/player.set_feet_global_position(GameState.player_positions) diff --git a/scenes/outside.tscn b/scenes/outside.tscn index 4cec809..8b8a63a 100644 --- a/scenes/outside.tscn +++ b/scenes/outside.tscn @@ -1,5 +1,6 @@ -[gd_scene load_steps=8 format=3 uid="uid://b4ydi1vv8dvwr"] +[gd_scene load_steps=9 format=3 uid="uid://b4ydi1vv8dvwr"] +[ext_resource type="Script" path="res://scenes/outside.gd" id="1_wyh83"] [ext_resource type="PackedScene" uid="uid://d1oqt6sbjvopi" path="res://maps/world.tscn" id="1_ysg4m"] [ext_resource type="PackedScene" uid="uid://bleadp4yrdgj" path="res://caracters/bob/bob.tscn" id="2_vbahy"] [ext_resource type="PackedScene" uid="uid://vclpg4e4ql54" path="res://caracters/player/player.tscn" id="3_bt1tp"] @@ -19,6 +20,7 @@ _data = { point_count = 20 [node name="scene1" type="Node2D"] +script = ExtResource("1_wyh83") [node name="world" parent="." instance=ExtResource("1_ysg4m")]