apply animation to other pieces

master
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

@ -2,14 +2,14 @@ extends Node2D
class_name Piece
tool
export var size: int = 64
export var size: int = 160
var number: int = 0
var taquin_position = Vector2(0, 0)
var piece_scale = Vector2(0.25, 0.25)
func _ready() -> void:
$ColorRect.rect_size.x = size
$ColorRect.rect_size.y = size
$ColorRect.rect_size = Vector2(size, size)
$ColorRect/Label.text = str(number)
# We need a dedicated material to have separate uniform,

View File

@ -110,6 +110,7 @@ shader_param/offset = Vector3( 0, 0, 0 )
[node name="Piece" type="Node2D"]
script = ExtResource( 1 )
size = 64
[node name="ColorRect" type="ColorRect" parent="."]
material = SubResource( 10 )

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:

View File

@ -1,12 +1,15 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=4 format=2]
[ext_resource path="res://src/Taquin.gd" type="Script" id=1]
[ext_resource path="res://src/Piece.tscn" type="PackedScene" id=2]
[sub_resource type="Animation" id=1]
resource_name = "MovingPiece"
length = 0.2
step = 0.01
tracks/0/type = "value"
tracks/0/path = NodePath("AnimationPlayer/MockPiece:rect_position")
tracks/0/interp = 2
tracks/0/path = NodePath("AnimationPlayer/MockPiece:position")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/imported = false
tracks/0/enabled = true
@ -14,7 +17,7 @@ tracks/0/keys = {
"times": PoolRealArray( 0, 0.2 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 0,
"values": [ Vector2( 176, 338 ), Vector2( 338, 338 ) ]
"values": [ Vector2( 15, 15 ), Vector2( 175, 15 ) ]
}
[node name="Taquin" type="Node2D"]
@ -23,7 +26,7 @@ rows = 3
columns = 3
difficulty = 0
[node name="ColorRect" type="ColorRect" parent="."]
[node name="Background" type="ColorRect" parent="."]
margin_right = 512.0
margin_bottom = 512.0
color = Color( 0.254902, 0.329412, 0.45098, 1 )
@ -31,10 +34,11 @@ color = Color( 0.254902, 0.329412, 0.45098, 1 )
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
anims/MovingPiece = SubResource( 1 )
[node name="MockPiece" type="ColorRect" parent="AnimationPlayer"]
visible = false
margin_left = 176.0
margin_top = 338.0
margin_right = 336.001
margin_bottom = 498.0
color = Color( 0.345098, 0.980392, 0.113725, 1 )
[node name="MockPiece" parent="AnimationPlayer" instance=ExtResource( 2 )]
position = Vector2( 175, 15 )
size = 160
[node name="PlaceholderTexture" type="ColorRect" parent="AnimationPlayer/MockPiece"]
margin_right = 160.0
margin_bottom = 160.0
color = Color( 0.137255, 0.976471, 0.0196078, 1 )