add game state
This commit is contained in:
parent
fb287b8264
commit
efb9a4e9d9
|
@ -9,6 +9,11 @@
|
||||||
config_version=4
|
config_version=4
|
||||||
|
|
||||||
_global_script_classes=[ {
|
_global_script_classes=[ {
|
||||||
|
"base": "Node",
|
||||||
|
"class": "GameState",
|
||||||
|
"language": "GDScript",
|
||||||
|
"path": "res://src/GameState.gd"
|
||||||
|
}, {
|
||||||
"base": "Node2D",
|
"base": "Node2D",
|
||||||
"class": "Piece",
|
"class": "Piece",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
|
@ -20,6 +25,7 @@ _global_script_classes=[ {
|
||||||
"path": "res://src/Taquin.gd"
|
"path": "res://src/Taquin.gd"
|
||||||
} ]
|
} ]
|
||||||
_global_script_class_icons={
|
_global_script_class_icons={
|
||||||
|
"GameState": "",
|
||||||
"Piece": "",
|
"Piece": "",
|
||||||
"Taquin": ""
|
"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
|
var blur_amount = 3
|
||||||
|
|
||||||
enum State {}
|
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
$ColorRect.visible = false
|
$ColorRect.visible = false
|
||||||
|
print("Starting state: ", $GameState.current_state_name())
|
||||||
|
|
||||||
func _on_Taquin_solved():
|
func _on_GameState_state_changed(previous, current):
|
||||||
print("Solved!")
|
print("GameState: ", $GameState.State.keys()[previous], " -> ", $GameState.State.keys()[current])
|
||||||
$ColorRect.visible = true
|
match current:
|
||||||
$ColorRect.material.set_shader_param("blur_amount", blur_amount)
|
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/Main.gd" type="Script" id=1]
|
||||||
[ext_resource path="res://src/Taquin.tscn" type="PackedScene" id=2]
|
[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]
|
[sub_resource type="Shader" id=1]
|
||||||
code = "shader_type canvas_item;
|
code = "shader_type canvas_item;
|
||||||
|
@ -49,4 +50,7 @@ text = "New game"
|
||||||
material = SubResource( 2 )
|
material = SubResource( 2 )
|
||||||
anchor_right = 1.0
|
anchor_right = 1.0
|
||||||
anchor_bottom = 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
|
class_name Taquin
|
||||||
tool
|
tool
|
||||||
|
|
||||||
signal solved
|
|
||||||
|
|
||||||
var Piece = preload("res://src/Piece.tscn")
|
var Piece = preload("res://src/Piece.tscn")
|
||||||
|
|
||||||
export var rows: int = 4
|
export var rows: int = 4
|
||||||
|
@ -149,6 +147,8 @@ func move_piece(direction) -> bool:
|
||||||
|
|
||||||
update()
|
update()
|
||||||
if check_solved():
|
if check_solved():
|
||||||
|
var game_state = get_node("/root/Main/GameState") as GameState
|
||||||
|
game_state.transition_to(GameState.State.WINNING)
|
||||||
emit_signal("solved")
|
emit_signal("solved")
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
Loading…
Reference in a new issue