From f8f9fa6984239eb324815bf77ee347a99b91b114 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 24 Apr 2024 04:55:31 +0200 Subject: [PATCH] Add `GoldCounter` --- entities/gold.tscn | 1 - scenes/game/cursor.gd | 5 ++- scenes/game/cursor.tscn | 6 ++- scenes/game/gold_counter.gd | 16 ++++++++ scenes/game/gold_counter.tscn | 69 +++++++++++++++++++++++++++++++++++ scenes/game/main_game.gd | 7 ++++ scenes/game/main_game.tscn | 1 + 7 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 scenes/game/gold_counter.gd create mode 100644 scenes/game/gold_counter.tscn diff --git a/entities/gold.tscn b/entities/gold.tscn index 2678b77..f3c55ea 100644 --- a/entities/gold.tscn +++ b/entities/gold.tscn @@ -15,7 +15,6 @@ radius = 96.0 [node name="Gold" type="Node2D"] script = ExtResource("1_lbls1") -speed_up_factor = 8.0 [node name="Collectible" parent="." instance=ExtResource("2_j75yq")] unique_name_in_owner = true diff --git a/scenes/game/cursor.gd b/scenes/game/cursor.gd index d211b0f..35d791d 100644 --- a/scenes/game/cursor.gd +++ b/scenes/game/cursor.gd @@ -8,12 +8,13 @@ signal dragged(node: Draggable) signal dropped(node: Draggable) -@onready var game := MainGame.get_via_group(self) +@onready var game: MainGame = MainGame.get_via_group(self) +@onready var gold_counter: GoldCounter = %"GoldCounter" var dragging: Draggable = null -static func get_via_group(node: Node) -> MainGame: +static func get_via_group(node: Node) -> Cursor: var result = node.get_tree().get_nodes_in_group("cursor") if result.is_empty(): return null diff --git a/scenes/game/cursor.tscn b/scenes/game/cursor.tscn index a85fb27..81f0942 100644 --- a/scenes/game/cursor.tscn +++ b/scenes/game/cursor.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=3 format=3 uid="uid://col1q3elvkfwk"] +[gd_scene load_steps=4 format=3 uid="uid://col1q3elvkfwk"] [ext_resource type="Script" path="res://scenes/game/cursor.gd" id="1_1og6v"] +[ext_resource type="PackedScene" uid="uid://cu750c7yd57qa" path="res://scenes/game/gold_counter.tscn" id="2_ofjhi"] [sub_resource type="CircleShape2D" id="CircleShape2D_j2mj5"] radius = 4.0 @@ -14,3 +15,6 @@ script = ExtResource("1_1og6v") [node name="Shape" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_j2mj5") debug_color = Color(1, 1, 1, 1) + +[node name="GoldCounter" parent="." instance=ExtResource("2_ofjhi")] +unique_name_in_owner = true diff --git a/scenes/game/gold_counter.gd b/scenes/game/gold_counter.gd new file mode 100644 index 0000000..f277d4a --- /dev/null +++ b/scenes/game/gold_counter.gd @@ -0,0 +1,16 @@ +extends PanelContainer +class_name GoldCounter + + +@onready var label: Label = %"Label" +@onready var animator: AnimationPlayer = %"Animator" + + +func set_text(value: int) -> void: + label.text = "%d €" % value + + +func display(value: int): + set_text(value) + animator.stop() + animator.play(&"collect") diff --git a/scenes/game/gold_counter.tscn b/scenes/game/gold_counter.tscn new file mode 100644 index 0000000..575d9b4 --- /dev/null +++ b/scenes/game/gold_counter.tscn @@ -0,0 +1,69 @@ +[gd_scene load_steps=6 format=3 uid="uid://cu750c7yd57qa"] + +[ext_resource type="Script" path="res://scenes/game/gold_counter.gd" id="1_koxhc"] + +[sub_resource type="Animation" id="Animation_3gb4u"] +resource_name = "RESET" +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath(".:scale") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Vector2(1, 1)] +} + +[sub_resource type="Animation" id="Animation_8n0bl"] +resource_name = "collect" +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath(".:scale") +tracks/0/interp = 2 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0, 1), +"transitions": PackedFloat32Array(1, 1), +"update": 0, +"values": [Vector2(1.5, 1.5), Vector2(1, 1)] +} + +[sub_resource type="AnimationLibrary" id="AnimationLibrary_bj0k7"] +_data = { +"RESET": SubResource("Animation_3gb4u"), +"collect": SubResource("Animation_8n0bl") +} + +[sub_resource type="LabelSettings" id="LabelSettings_1jb4h"] + +[node name="GoldCounter" type="PanelContainer"] +offset_left = 16.0 +offset_top = 24.0 +offset_right = 56.0 +offset_bottom = 51.0 +script = ExtResource("1_koxhc") + +[node name="Animator" type="AnimationPlayer" parent="."] +unique_name_in_owner = true +libraries = { +"": SubResource("AnimationLibrary_bj0k7") +} +next/RESET = &"RESET" + +[node name="Margin" type="MarginContainer" parent="."] +layout_mode = 2 +theme_override_constants/margin_left = 2 +theme_override_constants/margin_top = 2 +theme_override_constants/margin_right = 2 +theme_override_constants/margin_bottom = 2 + +[node name="Label" type="Label" parent="Margin"] +unique_name_in_owner = true +layout_mode = 2 +text = "0 €" +label_settings = SubResource("LabelSettings_1jb4h") +horizontal_alignment = 2 diff --git a/scenes/game/main_game.gd b/scenes/game/main_game.gd index c6612e2..3881b08 100644 --- a/scenes/game/main_game.gd +++ b/scenes/game/main_game.gd @@ -5,6 +5,7 @@ class_name MainGame @onready var camera: GameCamera = $"GameCamera" @onready var default_spawn_parent: Node2D = %"DefaultSpawnParent" @onready var inventory: Inventory = %"Inventory" +@onready var cursor: Cursor = %"Cursor" static func get_via_group(node: Node) -> MainGame: @@ -12,3 +13,9 @@ static func get_via_group(node: Node) -> MainGame: if result.is_empty(): return null return result[0] + + +func _ready(): + var gold_counter = inventory.get_counter(&"Gold") + gold_counter.changed.connect(cursor.gold_counter.display.unbind(1)) + cursor.gold_counter.set_text(gold_counter.value) diff --git a/scenes/game/main_game.tscn b/scenes/game/main_game.tscn index f30cc0f..66c084b 100644 --- a/scenes/game/main_game.tscn +++ b/scenes/game/main_game.tscn @@ -831,6 +831,7 @@ layer_0/tile_data = PackedInt32Array(720915, 196608, 9, 655379, 65536, 10, 58984 [node name="GameCamera" parent="." instance=ExtResource("2_db5xs")] [node name="Cursor" parent="." instance=ExtResource("5_g504x")] +unique_name_in_owner = true [node name="Walls" type="Node2D" parent="."]