apply animation to other pieces

This commit is contained in:
Fabien Freling 2019-12-03 13:50:42 +01:00
parent ce3b060487
commit 6753848c8f
4 changed files with 48 additions and 20 deletions

View file

@ -19,13 +19,17 @@ var pieces: Array = []
var missing_piece: Vector2
var rng = RandomNumberGenerator.new()
var current_animation_path: String = "AnimationPlayer/MockPiece:position"
func position_for_index(index: Vector2, size: int) -> Vector2:
return Vector2(padding + index.x * (size + interpiece), padding + index.y * (size + interpiece))
func _ready() -> void:
$ColorRect.rect_size.x = width
$ColorRect.rect_size.y = height
$AnimationPlayer/MockPiece.visible = false
$Background.rect_size.x = width
$Background.rect_size.y = height
rng.randomize()
var piece_size: int = compute_piece_size()
@ -46,7 +50,7 @@ func _ready() -> void:
missing_piece.x = c
missing_piece.y = r
$ColorRect.add_child(piece)
$Background.add_child(piece)
pieces_row.append(piece)
pieces.append(pieces_row)
@ -92,6 +96,25 @@ func move_piece(direction) -> bool:
print("impossible move")
return false
var moving_piece: Piece = pieces[destination.x][destination.y]
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(moving_piece), ":position")
print("new animation path: ", new_animation_path)
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, position_for_index(destination, moving_piece.size))
moving_piece_animation.track_set_key_value(moving_piece_track_index, 1, position_for_index(missing_piece, moving_piece.size))
$AnimationPlayer.play("MovingPiece")
swap(missing_piece, destination)
missing_piece = destination
@ -105,8 +128,8 @@ func swap(src: Vector2, dst: Vector2) -> void:
var tmp: Piece = pieces[src.x][src.y]
pieces[src.x][src.y] = pieces[dst.x][dst.y]
pieces[dst.x][dst.y] = tmp
pieces[src.x][src.y].position = position_for_index(src, tmp.size)
pieces[dst.x][dst.y].position = position_for_index(dst, tmp.size)
# pieces[src.x][src.y].position = position_for_index(src, tmp.size)
# pieces[dst.x][dst.y].position = position_for_index(dst, tmp.size)
func shuffle(count: int) -> void:
while count > 0: