From 3e0b6fbc003fb35f3fbd5db78e4bb0427c67dae0 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 21 Apr 2024 23:20:02 +0200 Subject: [PATCH] Fix gold collection --- entities/gold.gd | 24 +++++++++++++----------- entities/gold.tscn | 40 +++++++++++++++++++++++++++++++++------- entities/imp.tscn | 7 ++++--- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/entities/gold.gd b/entities/gold.gd index cd5d640..97e8bcb 100644 --- a/entities/gold.gd +++ b/entities/gold.gd @@ -2,18 +2,20 @@ extends Node2D class_name Gold -@export var value: int = 1 +@onready var move_towards: MoveTowards = $"%MoveTowards" +@onready var collect_sound_spawner: Spawner = $"%CollectSoundSpawner" + + +func magnetize(cursor: Cursor) -> void: + move_towards.target = cursor + +func demagnetize() -> void: + move_towards.target = null + +func collect() -> void: + collect_sound_spawner.spawn() + queue_free() func _on_move(movement: Vector2) -> void: position += movement - -func _on_collected(tag: StringName) -> void: - var game = MainGame.get_via_group(self) - - # TODO: Perhaps use a dictionary in game to store multiple currencies? - match tag: - &"Gold": - game.gold_counter.increase(value) - - queue_free() diff --git a/entities/gold.tscn b/entities/gold.tscn index da6ed04..e0617b8 100644 --- a/entities/gold.tscn +++ b/entities/gold.tscn @@ -1,23 +1,49 @@ -[gd_scene load_steps=6 format=3 uid="uid://uoxwjpmgg27a"] +[gd_scene load_steps=10 format=3 uid="uid://uoxwjpmgg27a"] [ext_resource type="Script" path="res://entities/gold.gd" id="1_lbls1"] +[ext_resource type="PackedScene" uid="uid://dj72yshd25ucx" path="res://behaviours/collectible.tscn" id="2_j75yq"] [ext_resource type="Texture2D" uid="uid://bfl0tkg85cvb8" path="res://entities/gold.png" id="2_tt3v6"] -[ext_resource type="PackedScene" uid="uid://dj72yshd25ucx" path="res://behaviours/collectable.tscn" id="3_q0bno"] +[ext_resource type="PackedScene" uid="uid://cbg5kgwxusvxf" path="res://behaviours/cursor_detector.tscn" id="3_ne3sj"] +[ext_resource type="PackedScene" uid="uid://cml7rqvyfuagx" path="res://behaviours/move_towards.tscn" id="5_bcbgf"] [ext_resource type="PackedScene" uid="uid://tx1qi6ahlxjp" path="res://behaviours/spawner.tscn" id="5_t7ey8"] [ext_resource type="PackedScene" uid="uid://eqg6snsgn1kh" path="res://players/gold_pickup_player.tscn" id="6_eoeje"] +[sub_resource type="CircleShape2D" id="CircleShape2D_w1h35"] +radius = 64.0 + +[sub_resource type="CircleShape2D" id="CircleShape2D_jm7yp"] + [node name="Gold" type="Node2D"] script = ExtResource("1_lbls1") +[node name="Collectable" parent="." instance=ExtResource("2_j75yq")] +kind = &"Gold" + [node name="Sprite" type="Sprite2D" parent="."] scale = Vector2(2, 2) texture = ExtResource("2_tt3v6") -[node name="Collectable" parent="." instance=ExtResource("3_q0bno")] -scale = Vector2(0.6, 0.6) -tag = &"Gold" +[node name="MagnetizeArea" parent="." instance=ExtResource("3_ne3sj")] -[node name="Spawner" parent="." instance=ExtResource("5_t7ey8")] +[node name="Shape" type="CollisionShape2D" parent="MagnetizeArea"] +shape = SubResource("CircleShape2D_w1h35") +debug_color = Color(0.337255, 0.611765, 1, 0) + +[node name="CollectArea" parent="." instance=ExtResource("3_ne3sj")] + +[node name="Shape" type="CollisionShape2D" parent="CollectArea"] +shape = SubResource("CircleShape2D_jm7yp") +debug_color = Color(0, 0.388235, 0.815686, 0.219608) + +[node name="MoveTowards" parent="." instance=ExtResource("5_bcbgf")] +unique_name_in_owner = true +speed = 184.0 + +[node name="CollectSoundSpawner" parent="." instance=ExtResource("5_t7ey8")] +unique_name_in_owner = true +scale = Vector2(1.66667, 1.66667) scene = ExtResource("6_eoeje") -[connection signal="collected" from="Collectable" to="." method="_on_collected"] +[connection signal="cursor_entered" from="MagnetizeArea" to="." method="magnetize"] +[connection signal="cursor_entered" from="CollectArea" to="." method="collect" unbinds=1] +[connection signal="move" from="MoveTowards" to="." method="_on_move"] diff --git a/entities/imp.tscn b/entities/imp.tscn index 72075b5..d097666 100644 --- a/entities/imp.tscn +++ b/entities/imp.tscn @@ -44,10 +44,11 @@ right_texture = ExtResource("4_0sckn") [node name="Shape" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_ide4n") -[node name="CoinSpawner" parent="." instance=ExtResource("4_d8lgm")] +[node name="GoldSpawner" parent="." instance=ExtResource("4_d8lgm")] scene = ExtResource("5_yrfoq") -[node name="TimerStddev" parent="CoinSpawner" instance=ExtResource("10_phvea")] +[node name="TimerStddev" parent="GoldSpawner" instance=ExtResource("10_phvea")] +autostart = true mean_secs = 5.0 deviation = 4.0 min_secs = 1.0 @@ -113,7 +114,7 @@ acceptable_diets = Array[StringName]([&"Meat"]) shape = SubResource("CircleShape2D_qhk1q") debug_color = Color(1, 0, 0, 0) -[connection signal="timeout" from="CoinSpawner/TimerStddev" to="CoinSpawner" method="spawn"] +[connection signal="timeout" from="GoldSpawner/TimerStddev" to="GoldSpawner" method="spawn"] [connection signal="ready" from="MovementSampler" to="MovementSampler" method="enable"] [connection signal="changed_direction" from="MovementWander" to="MovementWander/WanderPriority" method="priority_conditional"] [connection signal="move" from="MovementWander" to="." method="_on_move"]