Files
chaussette.sale/scenes/game.gd
Thomas 6517dd744e des bruits de pas
tralala
2025-03-31 17:24:16 +02:00

65 lines
1.6 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_to(GameState.current_scene)
func switch_to(to: String):
call_deferred("switch_scene", to)
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
scene.unload()
call_deferred("add_child", scene)
call_deferred("remove_child", prev)
call_deferred("init_scence", scene)
func init_scence(scene):
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(), " ", true, true))
print(OS.get_data_dir())
save_file.close()