taqin/src/Main.gd

110 lines
2.9 KiB
GDScript
Raw Permalink Normal View History

2019-11-26 13:57:50 +01:00
extends Control
2020-06-03 16:08:31 +02:00
onready var container = $GridContainer
onready var taquin = $GridContainer/Taquin
2020-02-24 22:56:51 +01:00
2020-06-08 18:39:16 +02:00
var _first_popup_call := true
2020-01-04 19:22:11 +01:00
func _ready():
2020-06-04 16:29:32 +02:00
taquin.rect_min_size = get_viewport().get_visible_rect().size * (2.0 / 3.0)
2020-06-03 16:08:31 +02:00
layout_reflow()
2020-06-10 17:15:33 +02:00
if not load_game():
start_fresh()
2020-02-24 22:56:51 +01:00
print("Starting state: ", taquin.current_state_name())
2020-01-12 19:49:25 +01:00
2020-05-20 18:06:35 +02:00
func _notification(what):
if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
save_game()
get_tree().quit() # default behavior
2020-06-03 19:15:38 +02:00
func _gui_input(event):
# Forward events to taquin so we can swipe from anywhere
taquin._gui_input(event)
2020-06-03 16:08:31 +02:00
func layout_reflow():
if container == null:
return
if container.rect_size.x < container.rect_size.y:
# portrait
container.columns = 1
else:
# landscape
container.columns = 2
2020-06-04 16:29:32 +02:00
update()
2020-06-03 16:08:31 +02:00
2020-05-20 18:06:35 +02:00
# https://docs.godotengine.org/en/3.2/tutorials/io/saving_games.html
func save_game():
var save_game = File.new()
save_game.open("user://savegame.save", File.WRITE)
var save_nodes = get_tree().get_nodes_in_group("Persist")
for node in save_nodes:
# Check the node has a save function
if !node.has_method("save"):
print("persistent node '%s' is missing a save() function, skipped" % node.name)
continue
# Call the node's save function
var node_data = node.call("save")
# Augment data with origin
node_data["path"] = node.get_path()
# Store the save dictionary as a new line in the save file
save_game.store_line(to_json(node_data))
save_game.close()
func load_game():
var save_game = File.new()
if not save_game.file_exists("user://savegame.save"):
2020-06-10 17:15:33 +02:00
return false # Error! We don't have a save to load.
2020-05-20 18:06:35 +02:00
# Load the file line by line and process that dictionary to restore
# the object it represents.
save_game.open("user://savegame.save", File.READ)
while save_game.get_position() < save_game.get_len():
# Get the saved dictionary from the next line in the save file
var node_data = parse_json(save_game.get_line())
# Call the node's save function
var node = get_node(node_data["path"])
2020-06-03 16:16:14 +02:00
if node != null:
2020-06-10 17:15:33 +02:00
if node.call("load", node_data) == false:
return false
2020-06-03 16:16:14 +02:00
else:
print("Cannot load node ", node_data["path"])
2020-05-20 18:06:35 +02:00
save_game.close()
2020-06-10 17:15:33 +02:00
return true
func start_fresh():
taquin.start_fresh()
2020-05-20 18:06:35 +02:00
#
# Signals
#
2020-02-24 22:56:51 +01:00
func _on_Taquin_state_changed(previous, new):
print("Taquin state: ", Taquin.State.keys()[previous], " -> ", Taquin.State.keys()[new])
match new:
Taquin.State.WINNING:
2020-01-07 13:54:06 +01:00
print("Solved!")
2020-02-24 22:56:51 +01:00
Taquin.State.GAME_OVER:
2020-04-13 17:54:11 +02:00
pass
func _on_New_game_pressed():
2020-06-08 18:39:16 +02:00
$NewGamePanel.rect_position = Vector2.ZERO
if _first_popup_call:
# We only call popup_centered() once, otherwise the popup shifts to the
# bottom right after each call.
$NewGamePanel.popup_centered_ratio($NewGamePanel.window_scale_factor)
_first_popup_call = false
else:
# After the 1st call, the position should be settled.
$NewGamePanel.popup()
2020-04-13 17:54:11 +02:00
func _on_NewGamePanel_about_to_show():
2020-05-28 19:03:23 +02:00
taquin.set_process_input(false)
2020-04-13 17:54:11 +02:00
func _on_NewGamePanel_popup_hide():
2020-05-28 19:03:23 +02:00
taquin.set_process_input(true)