remove GameState
This commit is contained in:
		
							parent
							
								
									3e881d2868
								
							
						
					
					
						commit
						61efafe9a3
					
				
					 5 changed files with 32 additions and 73 deletions
				
			
		| 
						 | 
					@ -9,11 +9,6 @@
 | 
				
			||||||
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",
 | 
				
			||||||
| 
						 | 
					@ -25,7 +20,6 @@ _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": ""
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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)
 | 
					 | 
				
			||||||
							
								
								
									
										16
									
								
								src/Main.gd
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								src/Main.gd
									
										
									
									
									
								
							| 
						 | 
					@ -2,14 +2,18 @@ extends Control
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export var blur: int = 3
 | 
					export var blur: int = 3
 | 
				
			||||||
export var blur_transition_duration: float = 1
 | 
					export var blur_transition_duration: float = 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var blur_amount: float = 0
 | 
					var blur_amount: float = 0
 | 
				
			||||||
var blur_step: float = 0
 | 
					var blur_step: float = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					onready var taquin = $HSplitContainer/Taquin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _ready():
 | 
					func _ready():
 | 
				
			||||||
	$ColorRect.visible = false
 | 
						$ColorRect.visible = false
 | 
				
			||||||
	blur_amount = 0
 | 
						blur_amount = 0
 | 
				
			||||||
	blur_step = blur / blur_transition_duration
 | 
						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):
 | 
					func _process(delta):
 | 
				
			||||||
	if blur_amount < blur:
 | 
						if blur_amount < blur:
 | 
				
			||||||
| 
						 | 
					@ -17,13 +21,13 @@ func _process(delta):
 | 
				
			||||||
		print("blur: ", blur_amount)
 | 
							print("blur: ", blur_amount)
 | 
				
			||||||
		$ColorRect.material.set_shader_param("blur_amount", blur_amount)
 | 
							$ColorRect.material.set_shader_param("blur_amount", blur_amount)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _on_GameState_state_changed(previous, current):
 | 
					func _on_Taquin_state_changed(previous, new):
 | 
				
			||||||
	print("GameState: ", $GameState.State.keys()[previous], " -> ", $GameState.State.keys()[current])
 | 
						print("Taquin state: ", Taquin.State.keys()[previous], " -> ", Taquin.State.keys()[new])
 | 
				
			||||||
	match current:
 | 
						match new:
 | 
				
			||||||
		GameState.State.WINNING:
 | 
							Taquin.State.WINNING:
 | 
				
			||||||
			print("Solved!")
 | 
								print("Solved!")
 | 
				
			||||||
			# TODO: wait for the animation to finish before game over
 | 
								# TODO: wait for the animation to finish before game over
 | 
				
			||||||
			#$GameState.transition_to(GameState.State.GAME_OVER)
 | 
								#$GameState.transition_to(GameState.State.GAME_OVER)
 | 
				
			||||||
		GameState.State.GAME_OVER:
 | 
							Taquin.State.GAME_OVER:
 | 
				
			||||||
			$ColorRect.visible = true
 | 
								$ColorRect.visible = true
 | 
				
			||||||
			$ColorRect.material.set_shader_param("blur_amount", blur_amount)
 | 
								$ColorRect.material.set_shader_param("blur_amount", blur_amount)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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/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]
 | 
					 | 
				
			||||||
[ext_resource path="res://assets/taqin_theme.tres" type="Theme" id=5]
 | 
					[ext_resource path="res://assets/taqin_theme.tres" type="Theme" id=5]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[sub_resource type="Shader" id=1]
 | 
					[sub_resource type="Shader" id=1]
 | 
				
			||||||
| 
						 | 
					@ -80,8 +79,4 @@ margin_right = 0.510254
 | 
				
			||||||
__meta__ = {
 | 
					__meta__ = {
 | 
				
			||||||
"_edit_use_anchors_": false
 | 
					"_edit_use_anchors_": false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					[connection signal="state_changed" from="HSplitContainer/Taquin" to="." method="_on_Taquin_state_changed"]
 | 
				
			||||||
[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"]
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,8 @@ tool
 | 
				
			||||||
class_name Taquin
 | 
					class_name Taquin
 | 
				
			||||||
extends Control
 | 
					extends Control
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					signal state_changed(previous, new)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum Direction { UP, DOWN, LEFT, RIGHT }
 | 
					enum Direction { UP, DOWN, LEFT, RIGHT }
 | 
				
			||||||
enum State {
 | 
					enum State {
 | 
				
			||||||
	MAIN,
 | 
						MAIN,
 | 
				
			||||||
| 
						 | 
					@ -105,15 +107,13 @@ func _input(event):
 | 
				
			||||||
		# Disable input during animation
 | 
							# Disable input during animation
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var game_state = get_node_or_null("/root/Main/GameState") as GameState
 | 
						match current_state:
 | 
				
			||||||
	if game_state != null:
 | 
							# If we are in the winning animation, fast-forward to game over screen
 | 
				
			||||||
		match game_state.current_state:
 | 
							State.WINNING:
 | 
				
			||||||
			# If we are in the winning animation, fast-forward to game over screen
 | 
								transition_to(State.GAME_OVER)
 | 
				
			||||||
			GameState.State.WINNING:
 | 
								return
 | 
				
			||||||
				game_state.transition_to(GameState.State.GAME_OVER)
 | 
							State.GAME_OVER:
 | 
				
			||||||
				return
 | 
								return
 | 
				
			||||||
			GameState.State.GAME_OVER:
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#
 | 
						#
 | 
				
			||||||
	# Handle keyboard input
 | 
						# Handle keyboard input
 | 
				
			||||||
| 
						 | 
					@ -294,10 +294,7 @@ func check_solved() -> bool:
 | 
				
			||||||
			if pieces[c][r].order != 1 + c + r * columns:
 | 
								if pieces[c][r].order != 1 + c + r * columns:
 | 
				
			||||||
				return false
 | 
									return false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var game_state = get_node_or_null("/root/Main/GameState") as GameState
 | 
						transition_to(State.WINNING)
 | 
				
			||||||
	if game_state != null:
 | 
					 | 
				
			||||||
		game_state.transition_to(GameState.State.WINNING)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ensure_validity() -> void:
 | 
					func ensure_validity() -> void:
 | 
				
			||||||
| 
						 | 
					@ -316,25 +313,21 @@ func transition_to(state):
 | 
				
			||||||
	assert(state in _state_transitions[current_state])
 | 
						assert(state in _state_transitions[current_state])
 | 
				
			||||||
	var previous_state = current_state
 | 
						var previous_state = current_state
 | 
				
			||||||
	current_state = state
 | 
						current_state = state
 | 
				
			||||||
	emit_signal("state_changed", previous_state, current_state)
 | 
						match current_state:
 | 
				
			||||||
 | 
							State.WINNING:
 | 
				
			||||||
 | 
					 | 
				
			||||||
func _on_GameState_state_changed(previous, current):
 | 
					 | 
				
			||||||
	match current:
 | 
					 | 
				
			||||||
		GameState.State.WINNING:
 | 
					 | 
				
			||||||
			$Particles2D.emitting = true
 | 
								$Particles2D.emitting = true
 | 
				
			||||||
			$Timer.start(-1)
 | 
								$Timer.start(-1)
 | 
				
			||||||
		GameState.State.GAME_OVER:
 | 
							State.GAME_OVER:
 | 
				
			||||||
			$Particles2D.emitting = false
 | 
								$Particles2D.emitting = false
 | 
				
			||||||
			$Timer.stop()
 | 
								$Timer.stop()
 | 
				
			||||||
 | 
						emit_signal("state_changed", previous_state, current_state)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _on_Timer_timeout():
 | 
					func _on_Timer_timeout():
 | 
				
			||||||
	var game_state = get_node("/root/Main/GameState") as GameState
 | 
						transition_to(State.GAME_OVER)
 | 
				
			||||||
	if game_state != null:
 | 
					 | 
				
			||||||
		game_state.transition_to(GameState.State.GAME_OVER)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _on_AnimationPlayer_animation_finished(anim_name):
 | 
					func _on_AnimationPlayer_animation_finished(anim_name):
 | 
				
			||||||
	if anim_name == "MovingPiece":
 | 
						match anim_name:
 | 
				
			||||||
		commit_slide()
 | 
							"MovingPiece":
 | 
				
			||||||
		update()
 | 
								commit_slide()
 | 
				
			||||||
		check_solved()
 | 
								update()
 | 
				
			||||||
 | 
								check_solved()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue