taqin/src/Piece.gd

40 lines
1.5 KiB
GDScript

tool
class_name Piece
extends Node2D
export var size: int = 160
export var texture: Texture
var order: int = 0
var taquin_original_index := Vector2.ZERO
var taquin_original_position := Vector2.ZERO
var taquin_current_index := Vector2.ZERO # place inside the taquin as indices (i, j)
var taquin_current_position := Vector2.ZERO # place inside the taquin as coordinates (x, y)
# position inside the original taquin as normalized coordinates (x, y) (domain: [0, 1])
# used for offsetting the texture on pieces
var taquin_original_normalized_position := Vector2.ZERO
var piece_scale := Vector2(0.25, 0.25)
func _ready() -> void:
$ColorRect.rect_size = Vector2(size, size)
$ColorRect/Label.text = str(order)
# We need a dedicated material to have separate uniform,
# otherwise uniforms will be shared.
var mat = $ColorRect.material.duplicate() as ShaderMaterial
mat.set_shader_param("artwork", texture) # TODO: make it common to all pieces
mat.set_shader_param("scale", Vector3(piece_scale.x, piece_scale.y, 1.0))
mat.set_shader_param("offset", Vector3(taquin_original_normalized_position.x, taquin_original_normalized_position.y, 0.0))
mat.set_shader_param("reflection", false)
$ColorRect.material = mat
func set_reflection(value: bool) -> void:
var mat = $ColorRect.material as ShaderMaterial
mat.set_shader_param("reflection", value)
func debug_print(name: String):
print("%s order: %s" % [name, order])
print("%s taquin index: %s" % [name, taquin_current_index])
print("%s taquin position: %s" % [name, position])