Files
chaussette.sale/scenes/game.gd
Thomas a82237ff28 L'arrivée de la boulangerie
Il n'y a rien dedans, mais elle est ouverte. Bonne visite
2025-03-30 14:38:20 +02:00

58 lines
1.4 KiB
GDScript

extends Node2D
class_name GameControler
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("menu"):
open_menu()
# list of scenes
var outside:Node2D = null
var dest_outside = preload("res://scenes/outside.tscn")
var bakery:Node2D = null
var dest_bakery = preload("res://scenes/bakery_interior.tscn")
var menu = null
func _ready() -> void:
GameState._game = self
func start_game():
menu = get_child(0)
switch_scene(GameState.current_scene)
func switch_scene(to: String):
var prev = get_child(0)
GameState.current_scene = to
var scene
match to:
"outside":
if outside == null:
outside = dest_outside.instantiate()
scene = outside
"bakery":
if bakery == null:
bakery = dest_bakery.instantiate()
scene = bakery
add_child(scene)
remove_child(prev)
scene.load_from_game_state()
func open_menu():
remove_child(get_child(0))
add_child(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()