do not check for solved during initial shuffle

master
Fabien Freling 2020-05-22 17:24:58 +02:00
parent 547e00ec09
commit f0c0c6d12e
2 changed files with 38 additions and 19 deletions

View File

@ -64,6 +64,7 @@ func compute_padding(piece_size: int) -> Vector2:
return p return p
func _ready() -> void: func _ready() -> void:
print_debug("")
$AnimationPlayer/MockPiece.visible = false $AnimationPlayer/MockPiece.visible = false
$Particles2D.emitting = false $Particles2D.emitting = false
@ -71,10 +72,7 @@ func _ready() -> void:
$Background.rect_size.y = height $Background.rect_size.y = height
rng.randomize() rng.randomize()
# var piece_size: int = compute_piece_size()
# padding = compute_padding(piece_size)
# print("piece size: ", piece_size)
# print("padding: ", padding)
var pieces_order: Array = [] var pieces_order: Array = []
for order in range(1, rows * columns + 1): for order in range(1, rows * columns + 1):
pieces_order.append(order) pieces_order.append(order)
@ -143,7 +141,7 @@ func _input(event):
var current_position = current_sliding_piece.position var current_position = current_sliding_piece.position
if current_position.distance_to(current_origin) > current_position.distance_to(current_goal): if current_position.distance_to(current_origin) > current_position.distance_to(current_goal):
current_sliding_piece.position = current_goal current_sliding_piece.position = current_goal
commit_slide(true) commit_slide(true, true)
else: else:
reset_position(current_sliding_piece) reset_position(current_sliding_piece)
reset_slide() reset_slide()
@ -209,11 +207,11 @@ func sliding_piece_for_direction(direction) -> Piece:
return piece return piece
func move_piece(direction, speed: float) -> void: func move_piece(direction, speed: float) -> bool:
current_sliding_piece = sliding_piece_for_direction(direction) current_sliding_piece = sliding_piece_for_direction(direction)
if current_sliding_piece == null: if current_sliding_piece == null:
reset_slide() reset_slide()
return return false
if speed > 0.0: if speed > 0.0:
var moving_piece_animation: Animation = $AnimationPlayer.get_animation("MovingPiece") var moving_piece_animation: Animation = $AnimationPlayer.get_animation("MovingPiece")
@ -233,11 +231,12 @@ func move_piece(direction, speed: float) -> 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")
else: else:
commit_slide(false) commit_slide(false, false)
update() update()
check_solved()
func commit_slide(audio: bool): return true
func commit_slide(audio: bool, check_solved: bool):
assert(current_sliding_piece != null) assert(current_sliding_piece != null)
assert(current_origin != Vector2.ZERO) assert(current_origin != Vector2.ZERO)
@ -253,7 +252,8 @@ func commit_slide(audio: bool):
ensure_validity() ensure_validity()
reset_slide() reset_slide()
check_solved() if check_solved:
check_solved()
func reset_slide(): func reset_slide():
current_sliding_piece = null current_sliding_piece = null
@ -270,10 +270,27 @@ func swap_pieces(a: Piece, b: Piece) -> void:
pieces[a_index.x][a_index.y] = b pieces[a_index.x][a_index.y] = b
func shuffle(count: int, speed: float) -> void: func shuffle(count: int, speed: float) -> void:
print_debug("")
var previous_direction: int = Direction.DOWN
while count > 0: while count > 0:
count -= 1
var direction = rng.randi_range(Direction.UP, Direction.RIGHT) var direction = rng.randi_range(Direction.UP, Direction.RIGHT)
move_piece(direction, speed)
# Avoid reversing the previous move
if direction == Direction.UP and previous_direction == Direction.DOWN:
continue
if direction == Direction.DOWN and previous_direction == Direction.UP:
continue
if direction == Direction.RIGHT and previous_direction == Direction.LEFT:
continue
if direction == Direction.LEFT and previous_direction == Direction.RIGHT:
continue
# Retry until the move is valid
if move_piece(direction, speed):
previous_direction = direction
count -= 1
debug_print_direction(direction)
func check_solved() -> bool: func check_solved() -> bool:
for c in range(columns): for c in range(columns):
@ -337,6 +354,7 @@ func load(saved_state) -> void:
init(saved_state["pieces"], saved_state["hidden_piece"]) init(saved_state["pieces"], saved_state["hidden_piece"])
func init(pieces_order: Array, hidden_piece: int) -> void: func init(pieces_order: Array, hidden_piece: int) -> void:
print_debug("")
var piece_size: int = compute_piece_size() var piece_size: int = compute_piece_size()
padding = compute_padding(piece_size) padding = compute_padding(piece_size)
print("piece size: ", piece_size) print("piece size: ", piece_size)
@ -344,11 +362,9 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
if pieces.size() > 0: if pieces.size() > 0:
for c in range(pieces.size()): for c in range(pieces.size()):
for r in range(pieces[0].size()): for r in range(pieces[c].size()):
var piece: Piece = pieces[c][r] var piece: Piece = pieces[c][r].queue_free()
$Background.remove_child(piece) pieces.clear()
piece.queue_free()
pieces.clear()
for c in range(columns): for c in range(columns):
var pieces_row: Array = [] var pieces_row: Array = []
@ -394,7 +410,7 @@ func _on_Timer_timeout():
func _on_AnimationPlayer_animation_finished(anim_name): func _on_AnimationPlayer_animation_finished(anim_name):
match anim_name: match anim_name:
"MovingPiece": "MovingPiece":
commit_slide(true) commit_slide(true, true)
update() update()
check_solved() check_solved()

View File

@ -60,6 +60,9 @@ difficulty = 0
margin_right = 512.0 margin_right = 512.0
margin_bottom = 512.0 margin_bottom = 512.0
color = Color( 0.12549, 0.235294, 0.337255, 1 ) color = Color( 0.12549, 0.235294, 0.337255, 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="AnimationPlayer" type="AnimationPlayer" parent="."] [node name="AnimationPlayer" type="AnimationPlayer" parent="."]
anims/MovingPiece = SubResource( 1 ) anims/MovingPiece = SubResource( 1 )