add game state
This commit is contained in:
parent
fb287b8264
commit
efb9a4e9d9
|
@ -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": ""
|
||||
}
|
||||
|
|
25
game/src/GameState.gd
Normal file
25
game/src/GameState.gd
Normal file
|
@ -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)
|
|
@ -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)
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue