put a number on each piece

master
Fabien Freling 2019-11-25 01:16:06 +01:00
parent abaa1f0991
commit 0fe5cca646
4 changed files with 23 additions and 5 deletions

View File

@ -3,6 +3,13 @@ class_name Piece
tool tool
export var size: int = 64 export var size: int = 64
var number: int = 0
func _draw() -> void: func _draw() -> void:
draw_rect(Rect2(0, 0, size, size), Color.yellow) draw_rect(Rect2(0, 0, size, size), Color.yellow)
func set_number(num: int) -> void:
number = num
func _ready() -> void:
$Label.text = str(number)

View File

@ -2,5 +2,11 @@
[ext_resource path="res://src/Piece.gd" type="Script" id=1] [ext_resource path="res://src/Piece.gd" type="Script" id=1]
[node name="Node2D" type="Node2D"] [node name="Piece" type="Node2D"]
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Label" type="Label" parent="."]
margin_right = 40.0
margin_bottom = 14.0
custom_colors/font_color = Color( 0, 0, 0, 1 )
text = "!"

View File

@ -2,6 +2,8 @@ extends Node2D
class_name Taquin class_name Taquin
tool tool
var Piece = preload("res://src/Piece.tscn")
export var rows: int = 4 export var rows: int = 4
export var columns: int = 4 export var columns: int = 4
export var width: int = 512 export var width: int = 512
@ -18,17 +20,20 @@ func _ready() -> void:
var pieces_row: Array = [] var pieces_row: Array = []
for r in range(rows): for r in range(rows):
var piece = Piece.new() var piece = Piece.instance()
var padding_w = (width - (piece.size * columns)) / (columns + 1) var padding_w = (width - (piece.size * columns)) / (columns + 1)
var padding_h = (height - (piece.size * rows)) / (rows + 1) var padding_h = (height - (piece.size * rows)) / (rows + 1)
var initial_position = Vector2(padding_w + c * (piece.size + padding_w), padding_h + r * (piece.size + padding_h)) var initial_position = Vector2(padding_w + c * (piece.size + padding_w), padding_h + r * (piece.size + padding_h))
piece.set_number(1 + c + r * columns)
piece.translate(initial_position) piece.translate(initial_position)
if r == rows - 1 && c == columns - 1: if r == rows - 1 && c == columns - 1:
piece.visible = false piece.visible = false
missing_piece.x = c missing_piece.x = c
missing_piece.y = r missing_piece.y = r
add_child(piece) add_child(piece)
pieces_row.append(piece) pieces_row.append(piece)
pieces.append(pieces_row) pieces.append(pieces_row)

View File

@ -2,5 +2,5 @@
[ext_resource path="res://src/Taquin.gd" type="Script" id=1] [ext_resource path="res://src/Taquin.gd" type="Script" id=1]
[node name="Node2D" type="Node2D"] [node name="Taquin" type="Node2D"]
script = ExtResource( 1 ) script = ExtResource( 1 )