diff --git a/src/Taquin.gd b/src/Taquin.gd index 14baf18..fe198ea 100644 --- a/src/Taquin.gd +++ b/src/Taquin.gd @@ -2,15 +2,26 @@ tool class_name Taquin 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 columns: int = 4 export var width: int = 512 export var height: int = 512 export var difficulty: int = 10 - -enum Direction { UP, DOWN, LEFT, RIGHT } +export(State) var current_state = State.MAIN var interpiece := 4 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) $AnimationPlayer.play("MovingPiece") - commit_slide() - - update() - check_solved() - func commit_slide(): assert(current_sliding_piece != null) assert(current_origin != Vector2.ZERO) @@ -256,6 +262,8 @@ func commit_slide(): swap_pieces(missing_piece, current_sliding_piece) reset_position(missing_piece) + $AudioStreamPlayer.play() + ensure_validity() reset_slide() check_solved() @@ -299,6 +307,18 @@ func ensure_validity() -> void: assert(piece.taquin_index.x == c) 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): match current: GameState.State.WINNING: @@ -312,3 +332,9 @@ 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) + +func _on_AnimationPlayer_animation_finished(anim_name): + if anim_name == "MovingPiece": + commit_slide() + update() + check_solved() diff --git a/src/Taquin.tscn b/src/Taquin.tscn index a0c3069..39c51ee 100644 --- a/src/Taquin.tscn +++ b/src/Taquin.tscn @@ -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/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] resource_name = "MovingPiece" @@ -45,6 +47,7 @@ color_ramp = SubResource( 3 ) [node name="Taquin" type="Control"] rect_min_size = Vector2( 540, 540 ) +theme = ExtResource( 4 ) script = ExtResource( 1 ) __meta__ = { "_edit_use_anchors_": false @@ -63,7 +66,7 @@ anims/MovingPiece = SubResource( 1 ) [node name="MockPiece" parent="AnimationPlayer" instance=ExtResource( 2 )] visible = false -position = Vector2( 175, 15 ) +position = Vector2( 15, 15 ) size = 160 [node name="PlaceholderTexture" type="ColorRect" parent="AnimationPlayer/MockPiece"] @@ -86,4 +89,8 @@ process_material = SubResource( 4 ) [node name="Timer" type="Timer" parent="."] wait_time = 2.0 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"]