From 0fe5cca6469dfa845f5440bb9cb004716a6541df Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 25 Nov 2019 01:16:06 +0100 Subject: [PATCH] put a number on each piece --- game/src/Piece.gd | 9 ++++++++- game/src/Piece.tscn | 8 +++++++- game/src/Taquin.gd | 9 +++++++-- game/src/Taquin.tscn | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/game/src/Piece.gd b/game/src/Piece.gd index 027692f..c63c7d3 100644 --- a/game/src/Piece.gd +++ b/game/src/Piece.gd @@ -3,6 +3,13 @@ class_name Piece tool export var size: int = 64 +var number: int = 0 func _draw() -> void: - draw_rect(Rect2(0, 0, size, size), Color.yellow) \ No newline at end of file + draw_rect(Rect2(0, 0, size, size), Color.yellow) + +func set_number(num: int) -> void: + number = num + +func _ready() -> void: + $Label.text = str(number) diff --git a/game/src/Piece.tscn b/game/src/Piece.tscn index 9c185d8..a4b700f 100644 --- a/game/src/Piece.tscn +++ b/game/src/Piece.tscn @@ -2,5 +2,11 @@ [ext_resource path="res://src/Piece.gd" type="Script" id=1] -[node name="Node2D" type="Node2D"] +[node name="Piece" type="Node2D"] 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 = "!" diff --git a/game/src/Taquin.gd b/game/src/Taquin.gd index 639340c..a7274a4 100644 --- a/game/src/Taquin.gd +++ b/game/src/Taquin.gd @@ -2,6 +2,8 @@ extends Node2D class_name Taquin tool +var Piece = preload("res://src/Piece.tscn") + export var rows: int = 4 export var columns: int = 4 export var width: int = 512 @@ -18,17 +20,20 @@ func _ready() -> void: var pieces_row: Array = [] for r in range(rows): - var piece = Piece.new() + var piece = Piece.instance() + var padding_w = (width - (piece.size * columns)) / (columns + 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)) + piece.set_number(1 + c + r * columns) piece.translate(initial_position) if r == rows - 1 && c == columns - 1: piece.visible = false missing_piece.x = c missing_piece.y = r - + add_child(piece) pieces_row.append(piece) + pieces.append(pieces_row) \ No newline at end of file diff --git a/game/src/Taquin.tscn b/game/src/Taquin.tscn index 7c06060..1455c92 100644 --- a/game/src/Taquin.tscn +++ b/game/src/Taquin.tscn @@ -2,5 +2,5 @@ [ext_resource path="res://src/Taquin.gd" type="Script" id=1] -[node name="Node2D" type="Node2D"] +[node name="Taquin" type="Node2D"] script = ExtResource( 1 )