show sneak peek as hints
This commit is contained in:
parent
774ad1b565
commit
80198db393
5 changed files with 85 additions and 45 deletions
100
src/Taquin.gd
100
src/Taquin.gd
|
@ -23,8 +23,7 @@ export var columns: int = 4
|
|||
export var difficulty: int = 10
|
||||
export(State) var current_state = State.MAIN
|
||||
|
||||
var width: int
|
||||
var height: int
|
||||
var board_size := Vector2.ZERO
|
||||
var interpiece := 4
|
||||
var min_padding := 15
|
||||
var padding := Vector2(min_padding, min_padding)
|
||||
|
@ -48,18 +47,22 @@ var current_touch_slide := Vector2.ZERO
|
|||
var local_min_position := Vector2.ZERO
|
||||
var local_max_position := Vector2.ZERO
|
||||
|
||||
var _hint_active := false
|
||||
|
||||
onready var hint_tween = $HintTween
|
||||
|
||||
func position_for_index(index: Vector2, size: int) -> Vector2:
|
||||
return padding + Vector2(index.x * (size + interpiece), index.y * (size + interpiece))
|
||||
|
||||
func compute_piece_size() -> int:
|
||||
var w_size: int = (width - (2 * min_padding) - ((columns - 1) * interpiece)) / columns
|
||||
var h_size: int = (height - (2 * min_padding) - ((rows - 1) * interpiece)) / rows
|
||||
var w_size: int = (board_size.x - (2 * min_padding) - ((columns - 1) * interpiece)) / columns
|
||||
var h_size: int = (board_size.y - (2 * min_padding) - ((rows - 1) * interpiece)) / rows
|
||||
return int(min(w_size, h_size))
|
||||
|
||||
func compute_padding(piece_size: int) -> Vector2:
|
||||
var p = Vector2(0, 0)
|
||||
p.x = width - columns * piece_size - (columns - 1) * interpiece
|
||||
p.y = height - rows * piece_size - (rows - 1) * interpiece
|
||||
p.x = board_size.x - columns * piece_size - (columns - 1) * interpiece
|
||||
p.y = board_size.y - rows * piece_size - (rows - 1) * interpiece
|
||||
p = p / Vector2(2, 2)
|
||||
return p
|
||||
|
||||
|
@ -68,11 +71,10 @@ func _ready() -> void:
|
|||
$AnimationPlayer/MockPiece.visible = false
|
||||
$Particles2D.emitting = false
|
||||
|
||||
width = min(rect_size.x, rect_size.y)
|
||||
height = width
|
||||
board_size.x = min(rect_size.x, rect_size.y)
|
||||
board_size.y = board_size.x
|
||||
|
||||
$Background.rect_size.x = width
|
||||
$Background.rect_size.y = height
|
||||
$Background.rect_size = board_size
|
||||
$Background.rect_position = (rect_size - $Background.rect_size) / 2
|
||||
$Background.set_anchors_preset(Control.PRESET_CENTER)
|
||||
|
||||
|
@ -84,6 +86,8 @@ func _input(event):
|
|||
if $AnimationPlayer.is_playing():
|
||||
# Disable input during animation
|
||||
return
|
||||
if _hint_active or hint_tween.is_active():
|
||||
return
|
||||
|
||||
match current_state:
|
||||
# If we are in the winning animation, fast-forward to game over screen
|
||||
|
@ -185,7 +189,7 @@ func direction_for_angle(angle: float) -> int:
|
|||
return Direction.DOWN
|
||||
|
||||
func sliding_piece_for_direction(direction) -> Piece:
|
||||
var destination: Vector2 = missing_piece.taquin_index
|
||||
var destination: Vector2 = missing_piece.taquin_current_index
|
||||
match direction:
|
||||
Direction.UP:
|
||||
destination.y += 1
|
||||
|
@ -241,8 +245,8 @@ func commit_slide(audio: bool, check_solved: bool):
|
|||
assert(current_sliding_piece != null)
|
||||
assert(current_origin != Vector2.ZERO)
|
||||
|
||||
var x_delta = missing_piece.taquin_index.x - current_sliding_piece.taquin_index.x
|
||||
var y_delta = missing_piece.taquin_index.y - current_sliding_piece.taquin_index.y
|
||||
var x_delta = missing_piece.taquin_current_index.x - current_sliding_piece.taquin_current_index.x
|
||||
var y_delta = missing_piece.taquin_current_index.y - current_sliding_piece.taquin_current_index.y
|
||||
assert(abs(x_delta) + abs(y_delta) == 1)
|
||||
|
||||
swap_pieces(missing_piece, current_sliding_piece)
|
||||
|
@ -262,13 +266,13 @@ func reset_slide():
|
|||
current_origin = Vector2.ZERO
|
||||
|
||||
func reset_position(p: Piece):
|
||||
p.position = position_for_index(p.taquin_index, p.size)
|
||||
p.position = position_for_index(p.taquin_current_index, p.size)
|
||||
|
||||
func swap_pieces(a: Piece, b: Piece) -> void:
|
||||
var a_index := a.taquin_index
|
||||
a.taquin_index = b.taquin_index
|
||||
pieces[b.taquin_index.x][b.taquin_index.y] = a
|
||||
b.taquin_index = a_index
|
||||
var a_index := a.taquin_current_index
|
||||
a.taquin_current_index = b.taquin_current_index
|
||||
pieces[b.taquin_current_index.x][b.taquin_current_index.y] = a
|
||||
b.taquin_current_index = a_index
|
||||
pieces[a_index.x][a_index.y] = b
|
||||
|
||||
func shuffle(count: int, speed: float) -> void:
|
||||
|
@ -307,8 +311,8 @@ func ensure_validity() -> void:
|
|||
for c in range(columns):
|
||||
for r in range(rows):
|
||||
var piece = pieces[c][r]
|
||||
assert(piece.taquin_index.x == c)
|
||||
assert(piece.taquin_index.y == r)
|
||||
assert(piece.taquin_current_index.x == c)
|
||||
assert(piece.taquin_current_index.y == r)
|
||||
|
||||
func set_pieces_reflection(value: bool) -> void:
|
||||
for c in range(columns):
|
||||
|
@ -365,34 +369,30 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
|
|||
if pieces.size() > 0:
|
||||
for c in range(pieces.size()):
|
||||
for r in range(pieces[c].size()):
|
||||
var piece: Piece = pieces[c][r].queue_free()
|
||||
pieces[c][r].queue_free()
|
||||
pieces.clear()
|
||||
|
||||
for c in range(columns):
|
||||
var pieces_row: Array = []
|
||||
for r in range(rows):
|
||||
var piece = Piece.instance()
|
||||
|
||||
# Uniforms
|
||||
piece.size = piece_size
|
||||
piece.piece_scale = Vector2((float(piece_size) / width), (float(piece_size) / height))
|
||||
|
||||
piece.piece_scale = Vector2(piece_size, piece_size) / board_size
|
||||
|
||||
# order start from top-left (1) and iterate over every row
|
||||
# eg. 1 2 3
|
||||
# 4 5 6
|
||||
# 7 8 9
|
||||
piece.order = pieces_order[c + r * columns]
|
||||
# print("piece at ", c, ", ", r, " -> order: ", piece.order)
|
||||
|
||||
# place inside the taquin as indices (i, j)
|
||||
piece.taquin_index = Vector2(c, r)
|
||||
# place inside the taquin as coordinates (x, y)
|
||||
piece.position = position_for_index(piece.taquin_index, piece.size)
|
||||
piece.taquin_original_index = Vector2((piece.order - 1) % columns, (piece.order - 1) / columns)
|
||||
piece.taquin_original_position = position_for_index(piece.taquin_original_index, piece.size)
|
||||
piece.taquin_current_index = Vector2(c, r)
|
||||
piece.taquin_current_position = position_for_index(piece.taquin_current_index, piece.size)
|
||||
piece.taquin_original_normalized_position = piece.taquin_original_position / board_size
|
||||
|
||||
# position inside the original taquin as normalized coordinates (x, y) (domain: [0, 1])
|
||||
# used for offsetting the texture on pieces
|
||||
var original_index = Vector2((piece.order - 1) % columns, (piece.order - 1) / columns)
|
||||
var original_position = position_for_index(original_index, piece.size)
|
||||
piece.taquin_position = Vector2(float(original_position.x) / width, float(original_position.y) / height)
|
||||
piece.position = piece.taquin_current_position
|
||||
|
||||
if piece.order == hidden_piece:
|
||||
piece.visible = false
|
||||
|
@ -402,6 +402,7 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
|
|||
pieces_row.append(piece)
|
||||
|
||||
pieces.append(pieces_row)
|
||||
assert(missing_piece != null)
|
||||
|
||||
func new_game(difficulty_mode: String) -> void:
|
||||
print_debug("difficulty mode: ", difficulty_mode)
|
||||
|
@ -431,6 +432,30 @@ func new_game(difficulty_mode: String) -> void:
|
|||
init(pieces_order, hidden_piece)
|
||||
shuffle(difficulty, 0.0)
|
||||
|
||||
#
|
||||
# Hints
|
||||
#
|
||||
func show_hints() -> void:
|
||||
_hint_active = true
|
||||
hint_tween.remove_all()
|
||||
for c in range(columns):
|
||||
for r in range(rows):
|
||||
var piece: Piece = pieces[c][r]
|
||||
if piece.taquin_current_index != piece.taquin_original_index:
|
||||
piece.taquin_current_position = piece.position
|
||||
hint_tween.interpolate_property(piece, "position", piece.position, piece.taquin_original_position, 1.0)
|
||||
hint_tween.start()
|
||||
|
||||
func discard_hints() -> void:
|
||||
hint_tween.remove_all()
|
||||
for c in range(columns):
|
||||
for r in range(rows):
|
||||
var piece: Piece = pieces[c][r]
|
||||
if piece.taquin_current_index != piece.taquin_original_index:
|
||||
hint_tween.interpolate_property(piece, "position", piece.position, piece.taquin_current_position, 1.0)
|
||||
hint_tween.start()
|
||||
_hint_active = false
|
||||
|
||||
#
|
||||
# Signals
|
||||
#
|
||||
|
@ -445,3 +470,10 @@ func _on_AnimationPlayer_animation_finished(anim_name):
|
|||
func _on_NewGamePanel_start_triggered(preferences):
|
||||
var difficulty_mode = preferences.get_value("game", "difficulty", "normal")
|
||||
new_game(difficulty_mode)
|
||||
|
||||
|
||||
func _on_Hints_button_down():
|
||||
show_hints()
|
||||
|
||||
func _on_Hints_button_up():
|
||||
discard_hints()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue