From 61efafe9a328bd9a045d041e21c673a69b9c8940 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 24 Feb 2020 22:56:51 +0100 Subject: [PATCH] remove GameState --- project.godot | 6 ------ src/GameState.gd | 27 --------------------------- src/Main.gd | 16 ++++++++++------ src/Main.tscn | 9 ++------- src/Taquin.gd | 47 ++++++++++++++++++++--------------------------- 5 files changed, 32 insertions(+), 73 deletions(-) delete mode 100644 src/GameState.gd diff --git a/project.godot b/project.godot index ed25242..c67b1b0 100644 --- a/project.godot +++ b/project.godot @@ -9,11 +9,6 @@ config_version=4 _global_script_classes=[ { -"base": "Node", -"class": "GameState", -"language": "GDScript", -"path": "res://src/GameState.gd" -}, { "base": "Node2D", "class": "Piece", "language": "GDScript", @@ -25,7 +20,6 @@ _global_script_classes=[ { "path": "res://src/Taquin.gd" } ] _global_script_class_icons={ -"GameState": "", "Piece": "", "Taquin": "" } diff --git a/src/GameState.gd b/src/GameState.gd deleted file mode 100644 index 9ced048..0000000 --- a/src/GameState.gd +++ /dev/null @@ -1,27 +0,0 @@ -class_name GameState -extends Node - -signal state_changed(previous, new) - -# 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 ] -} - -export(State) var current_state = State.MAIN - -func current_state_name() -> String: - return State.keys()[current_state] - -func transition_to(state): - if current_state == state: - return - assert(state in transitions[current_state]) - var previous_state = current_state - current_state = state - emit_signal("state_changed", previous_state, current_state) diff --git a/src/Main.gd b/src/Main.gd index 84b282c..e6bc50e 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -2,14 +2,18 @@ extends Control export var blur: int = 3 export var blur_transition_duration: float = 1 + var blur_amount: float = 0 var blur_step: float = 0 +onready var taquin = $HSplitContainer/Taquin + func _ready(): $ColorRect.visible = false blur_amount = 0 blur_step = blur / blur_transition_duration - print("Starting state: ", $GameState.current_state_name()) + print("Starting state: ", taquin.current_state_name()) + taquin.connect("state_changed", self, "_on_Taquin_state_changed") func _process(delta): if blur_amount < blur: @@ -17,13 +21,13 @@ func _process(delta): print("blur: ", blur_amount) $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: +func _on_Taquin_state_changed(previous, new): + print("Taquin state: ", Taquin.State.keys()[previous], " -> ", Taquin.State.keys()[new]) + match new: + Taquin.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: + Taquin.State.GAME_OVER: $ColorRect.visible = true $ColorRect.material.set_shader_param("blur_amount", blur_amount) diff --git a/src/Main.tscn b/src/Main.tscn index e33d5a1..7d3e127 100644 --- a/src/Main.tscn +++ b/src/Main.tscn @@ -1,8 +1,7 @@ -[gd_scene load_steps=7 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] [ext_resource path="res://assets/taqin_theme.tres" type="Theme" id=5] [sub_resource type="Shader" id=1] @@ -80,8 +79,4 @@ margin_right = 0.510254 __meta__ = { "_edit_use_anchors_": false } - -[node name="GameState" type="Node" parent="."] -script = ExtResource( 3 ) -[connection signal="state_changed" from="GameState" to="HSplitContainer/Taquin" method="_on_GameState_state_changed"] -[connection signal="state_changed" from="GameState" to="." method="_on_GameState_state_changed"] +[connection signal="state_changed" from="HSplitContainer/Taquin" to="." method="_on_Taquin_state_changed"] diff --git a/src/Taquin.gd b/src/Taquin.gd index fe198ea..c7ba8a8 100644 --- a/src/Taquin.gd +++ b/src/Taquin.gd @@ -2,6 +2,8 @@ tool class_name Taquin extends Control +signal state_changed(previous, new) + enum Direction { UP, DOWN, LEFT, RIGHT } enum State { MAIN, @@ -105,15 +107,13 @@ func _input(event): # Disable input during animation return - var game_state = get_node_or_null("/root/Main/GameState") as GameState - if game_state != null: - match game_state.current_state: - # If we are in the winning animation, fast-forward to game over screen - GameState.State.WINNING: - game_state.transition_to(GameState.State.GAME_OVER) - return - GameState.State.GAME_OVER: - return + match current_state: + # If we are in the winning animation, fast-forward to game over screen + State.WINNING: + transition_to(State.GAME_OVER) + return + State.GAME_OVER: + return # # Handle keyboard input @@ -294,10 +294,7 @@ func check_solved() -> bool: if pieces[c][r].order != 1 + c + r * columns: return false - var game_state = get_node_or_null("/root/Main/GameState") as GameState - if game_state != null: - game_state.transition_to(GameState.State.WINNING) - + transition_to(State.WINNING) return true func ensure_validity() -> void: @@ -316,25 +313,21 @@ func transition_to(state): assert(state in _state_transitions[current_state]) var previous_state = current_state current_state = state - emit_signal("state_changed", previous_state, current_state) - - -func _on_GameState_state_changed(previous, current): - match current: - GameState.State.WINNING: + match current_state: + State.WINNING: $Particles2D.emitting = true $Timer.start(-1) - GameState.State.GAME_OVER: + State.GAME_OVER: $Particles2D.emitting = false $Timer.stop() + emit_signal("state_changed", previous_state, current_state) func _on_Timer_timeout(): - var game_state = get_node("/root/Main/GameState") as GameState - if game_state != null: - game_state.transition_to(GameState.State.GAME_OVER) + transition_to(State.GAME_OVER) func _on_AnimationPlayer_animation_finished(anim_name): - if anim_name == "MovingPiece": - commit_slide() - update() - check_solved() + match anim_name: + "MovingPiece": + commit_slide() + update() + check_solved()