diff --git a/game/project.godot b/game/project.godot index 6bc20b7..176455f 100644 --- a/game/project.godot +++ b/game/project.godot @@ -9,6 +9,11 @@ config_version=4 _global_script_classes=[ { +"base": "Node", +"class": "GameState", +"language": "GDScript", +"path": "res://src/GameState.gd" +}, { "base": "Node2D", "class": "Piece", "language": "GDScript", @@ -20,6 +25,7 @@ _global_script_classes=[ { "path": "res://src/Taquin.gd" } ] _global_script_class_icons={ +"GameState": "", "Piece": "", "Taquin": "" } diff --git a/game/src/GameState.gd b/game/src/GameState.gd new file mode 100644 index 0000000..ed1524d --- /dev/null +++ b/game/src/GameState.gd @@ -0,0 +1,25 @@ +extends Node +class_name GameState + +# https://github.com/GDQuest/godot-demos/blob/master/2018/04-24-finite-state-machine/player_v2/state_machine.gd +# http://www.gameprogrammingpatterns.com/state.html + +enum State { MAIN, WINNING, GAME_OVER } +var transitions = { + State.MAIN : [ State.WINNING ], + State.WINNING : [ State.GAME_OVER ], + State.GAME_OVER : [ State.MAIN ] +} + +signal state_changed(previous, current) + +export(State) var current_state = State.MAIN + +func current_state_name() -> String: + return State.keys()[current_state] + +func transition_to(state): + assert(state in transitions[current_state]) + var previous_state = current_state + current_state = state + emit_signal("state_changed", previous_state, current_state) \ No newline at end of file diff --git a/game/src/Main.gd b/game/src/Main.gd index 7819e9e..4d43eef 100644 --- a/game/src/Main.gd +++ b/game/src/Main.gd @@ -2,12 +2,17 @@ extends Control var blur_amount = 3 -enum State {} - func _ready(): $ColorRect.visible = false + print("Starting state: ", $GameState.current_state_name()) -func _on_Taquin_solved(): - print("Solved!") - $ColorRect.visible = true - $ColorRect.material.set_shader_param("blur_amount", blur_amount) +func _on_GameState_state_changed(previous, current): + print("GameState: ", $GameState.State.keys()[previous], " -> ", $GameState.State.keys()[current]) + match current: + GameState.State.WINNING: + print("Solved!") + # TODO: wait for the animation to finish before game over + $GameState.transition_to(GameState.State.GAME_OVER) + GameState.State.GAME_OVER: + $ColorRect.visible = true + $ColorRect.material.set_shader_param("blur_amount", blur_amount) diff --git a/game/src/Main.tscn b/game/src/Main.tscn index 1ac59bb..1093cb8 100644 --- a/game/src/Main.tscn +++ b/game/src/Main.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://src/Main.gd" type="Script" id=1] [ext_resource path="res://src/Taquin.tscn" type="PackedScene" id=2] +[ext_resource path="res://src/GameState.gd" type="Script" id=3] [sub_resource type="Shader" id=1] code = "shader_type canvas_item; @@ -49,4 +50,7 @@ text = "New game" material = SubResource( 2 ) anchor_right = 1.0 anchor_bottom = 1.0 -[connection signal="solved" from="HSplitContainer/Taquin" to="." method="_on_Taquin_solved"] + +[node name="GameState" type="Node" parent="."] +script = ExtResource( 3 ) +[connection signal="state_changed" from="GameState" to="." method="_on_GameState_state_changed"] diff --git a/game/src/Taquin.gd b/game/src/Taquin.gd index 812e9aa..b80a9fe 100644 --- a/game/src/Taquin.gd +++ b/game/src/Taquin.gd @@ -2,8 +2,6 @@ extends Control class_name Taquin tool -signal solved - var Piece = preload("res://src/Piece.tscn") export var rows: int = 4 @@ -149,6 +147,8 @@ func move_piece(direction) -> bool: update() if check_solved(): + var game_state = get_node("/root/Main/GameState") as GameState + game_state.transition_to(GameState.State.WINNING) emit_signal("solved") return true