move pieces instantaneously and without sound at init

master
Fabien Freling 2020-05-22 15:56:32 +02:00
parent 6d788465ab
commit 111b2f7125
2 changed files with 36 additions and 31 deletions

View File

@ -44,6 +44,7 @@ func _on_NewGamePanel_about_to_show():
"hard":
$Panel/HBoxContainer/Hard.pressed = true
_:
assert("Invalid value")
$Panel/HBoxContainer/Normal.pressed = true
$Panel/Start.grab_focus()

View File

@ -81,8 +81,7 @@ func _ready() -> void:
var hidden_piece = rows * columns # Last piece is hidden
init(pieces_order, hidden_piece)
shuffle(difficulty)
shuffle(difficulty, 0.0)
func _input(event):
if $AnimationPlayer.is_playing():
@ -101,13 +100,13 @@ func _input(event):
# Handle keyboard input
#
if event.is_action_pressed("ui_up"):
move_piece(Direction.UP)
move_piece(Direction.UP, 1.0)
if event.is_action_pressed("ui_down"):
move_piece(Direction.DOWN)
move_piece(Direction.DOWN, 1.0)
if event.is_action_pressed("ui_left"):
move_piece(Direction.LEFT)
move_piece(Direction.LEFT, 1.0)
if event.is_action_pressed("ui_right"):
move_piece(Direction.RIGHT)
move_piece(Direction.RIGHT, 1.0)
#
# Handle touch input
@ -144,7 +143,7 @@ func _input(event):
var current_position = current_sliding_piece.position
if current_position.distance_to(current_origin) > current_position.distance_to(current_goal):
current_sliding_piece.position = current_goal
commit_slide()
commit_slide(true)
else:
reset_position(current_sliding_piece)
reset_slide()
@ -210,30 +209,35 @@ func sliding_piece_for_direction(direction) -> Piece:
return piece
func move_piece(direction) -> void:
func move_piece(direction, speed: float) -> void:
current_sliding_piece = sliding_piece_for_direction(direction)
if current_sliding_piece == null:
reset_slide()
return
var moving_piece_animation: Animation = $AnimationPlayer.get_animation("MovingPiece")
assert(moving_piece_animation != null)
assert(moving_piece_animation.get_track_count() > 0)
var moving_piece_track_index: int = moving_piece_animation.find_track(current_animation_path)
assert(moving_piece_track_index != -1)
var new_animation_path: String = str($AnimationPlayer.get_parent().get_path_to(current_sliding_piece), ":position")
moving_piece_animation.track_set_path(moving_piece_track_index, new_animation_path)
current_animation_path = new_animation_path
if speed > 0.0:
var moving_piece_animation: Animation = $AnimationPlayer.get_animation("MovingPiece")
moving_piece_animation.track_set_key_value(moving_piece_track_index, 0, current_sliding_piece.position)
moving_piece_animation.track_set_key_value(moving_piece_track_index, 1, missing_piece.position)
$AnimationPlayer.play("MovingPiece")
assert(moving_piece_animation != null)
assert(moving_piece_animation.get_track_count() > 0)
var moving_piece_track_index: int = moving_piece_animation.find_track(current_animation_path)
assert(moving_piece_track_index != -1)
var new_animation_path: String = str($AnimationPlayer.get_parent().get_path_to(current_sliding_piece), ":position")
moving_piece_animation.track_set_path(moving_piece_track_index, new_animation_path)
current_animation_path = new_animation_path
moving_piece_animation.track_set_key_value(moving_piece_track_index, 0, current_sliding_piece.position)
moving_piece_animation.track_set_key_value(moving_piece_track_index, 1, missing_piece.position)
$AnimationPlayer.play("MovingPiece")
else:
commit_slide(false)
update()
check_solved()
func commit_slide():
func commit_slide(audio: bool):
assert(current_sliding_piece != null)
assert(current_origin != Vector2.ZERO)
@ -244,7 +248,8 @@ func commit_slide():
swap_pieces(missing_piece, current_sliding_piece)
reset_position(missing_piece)
$AudioStreamPlayer.play()
if audio:
$AudioStreamPlayer.play()
ensure_validity()
reset_slide()
@ -264,11 +269,11 @@ func swap_pieces(a: Piece, b: Piece) -> void:
b.taquin_index = a_index
pieces[a_index.x][a_index.y] = b
func shuffle(count: int) -> void:
func shuffle(count: int, speed: float) -> void:
while count > 0:
count -= 1
var direction = rng.randi_range(Direction.UP, Direction.RIGHT)
move_piece(direction)
move_piece(direction, speed)
func check_solved() -> bool:
for c in range(columns):
@ -380,8 +385,6 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
pieces.append(pieces_row)
shuffle(difficulty)
#
# Signals
#
@ -391,7 +394,7 @@ func _on_Timer_timeout():
func _on_AnimationPlayer_animation_finished(anim_name):
match anim_name:
"MovingPiece":
commit_slide()
commit_slide(true)
update()
check_solved()
@ -411,6 +414,7 @@ func _on_NewGamePanel_start_triggered(preferences):
columns = 5
difficulty = 30
_:
assert("Invalid value")
rows = 4
columns = 4
difficulty = 10
@ -420,4 +424,4 @@ func _on_NewGamePanel_start_triggered(preferences):
pieces_order.append(order)
var hidden_piece = rows * columns # Last piece is hidden
init(pieces_order, hidden_piece)
shuffle(difficulty)
shuffle(difficulty, 0.0)