add piece locking sound

This commit is contained in:
Fabien Freling 2020-02-24 14:36:53 +01:00
parent ddd705f661
commit 3e881d2868
2 changed files with 43 additions and 10 deletions

View file

@ -2,15 +2,26 @@ tool
class_name Taquin class_name Taquin
extends Control extends Control
var Piece = preload("res://src/Piece.tscn") enum Direction { UP, DOWN, LEFT, RIGHT }
enum State {
MAIN,
WINNING,
GAME_OVER,
}
const _state_transitions = {
State.MAIN : [ State.WINNING ],
State.WINNING : [ State.GAME_OVER ],
State.GAME_OVER : [ State.MAIN ]
}
const Piece = preload("res://src/Piece.tscn")
export var rows: int = 4 export var rows: int = 4
export var columns: int = 4 export var columns: int = 4
export var width: int = 512 export var width: int = 512
export var height: int = 512 export var height: int = 512
export var difficulty: int = 10 export var difficulty: int = 10
export(State) var current_state = State.MAIN
enum Direction { UP, DOWN, LEFT, RIGHT }
var interpiece := 4 var interpiece := 4
var min_padding := 15 var min_padding := 15
@ -240,11 +251,6 @@ func move_piece(direction) -> void:
moving_piece_animation.track_set_key_value(moving_piece_track_index, 1, missing_piece.position) moving_piece_animation.track_set_key_value(moving_piece_track_index, 1, missing_piece.position)
$AnimationPlayer.play("MovingPiece") $AnimationPlayer.play("MovingPiece")
commit_slide()
update()
check_solved()
func commit_slide(): func commit_slide():
assert(current_sliding_piece != null) assert(current_sliding_piece != null)
assert(current_origin != Vector2.ZERO) assert(current_origin != Vector2.ZERO)
@ -256,6 +262,8 @@ func commit_slide():
swap_pieces(missing_piece, current_sliding_piece) swap_pieces(missing_piece, current_sliding_piece)
reset_position(missing_piece) reset_position(missing_piece)
$AudioStreamPlayer.play()
ensure_validity() ensure_validity()
reset_slide() reset_slide()
check_solved() check_solved()
@ -299,6 +307,18 @@ func ensure_validity() -> void:
assert(piece.taquin_index.x == c) assert(piece.taquin_index.x == c)
assert(piece.taquin_index.y == r) assert(piece.taquin_index.y == r)
func current_state_name() -> String:
return State.keys()[current_state]
func transition_to(state):
if current_state == state:
return
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): func _on_GameState_state_changed(previous, current):
match current: match current:
GameState.State.WINNING: GameState.State.WINNING:
@ -312,3 +332,9 @@ func _on_Timer_timeout():
var game_state = get_node("/root/Main/GameState") as GameState var game_state = get_node("/root/Main/GameState") as GameState
if game_state != null: if game_state != null:
game_state.transition_to(GameState.State.GAME_OVER) game_state.transition_to(GameState.State.GAME_OVER)
func _on_AnimationPlayer_animation_finished(anim_name):
if anim_name == "MovingPiece":
commit_slide()
update()
check_solved()

View file

@ -1,7 +1,9 @@
[gd_scene load_steps=7 format=2] [gd_scene load_steps=9 format=2]
[ext_resource path="res://src/Taquin.gd" type="Script" id=1] [ext_resource path="res://src/Taquin.gd" type="Script" id=1]
[ext_resource path="res://src/Piece.tscn" type="PackedScene" id=2] [ext_resource path="res://src/Piece.tscn" type="PackedScene" id=2]
[ext_resource path="res://assets/sounds/lock_01.wav" type="AudioStream" id=3]
[ext_resource path="res://assets/taqin_theme.tres" type="Theme" id=4]
[sub_resource type="Animation" id=1] [sub_resource type="Animation" id=1]
resource_name = "MovingPiece" resource_name = "MovingPiece"
@ -45,6 +47,7 @@ color_ramp = SubResource( 3 )
[node name="Taquin" type="Control"] [node name="Taquin" type="Control"]
rect_min_size = Vector2( 540, 540 ) rect_min_size = Vector2( 540, 540 )
theme = ExtResource( 4 )
script = ExtResource( 1 ) script = ExtResource( 1 )
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
@ -63,7 +66,7 @@ anims/MovingPiece = SubResource( 1 )
[node name="MockPiece" parent="AnimationPlayer" instance=ExtResource( 2 )] [node name="MockPiece" parent="AnimationPlayer" instance=ExtResource( 2 )]
visible = false visible = false
position = Vector2( 175, 15 ) position = Vector2( 15, 15 )
size = 160 size = 160
[node name="PlaceholderTexture" type="ColorRect" parent="AnimationPlayer/MockPiece"] [node name="PlaceholderTexture" type="ColorRect" parent="AnimationPlayer/MockPiece"]
@ -86,4 +89,8 @@ process_material = SubResource( 4 )
[node name="Timer" type="Timer" parent="."] [node name="Timer" type="Timer" parent="."]
wait_time = 2.0 wait_time = 2.0
one_shot = true one_shot = true
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 3 )
[connection signal="animation_finished" from="AnimationPlayer" to="." method="_on_AnimationPlayer_animation_finished"]
[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"] [connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]