From e90830cd06112eed90a9dbb7eb28a307e29082e8 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 1 Oct 2023 03:03:03 +0200 Subject: [PATCH] Add collector entity --- collector/collectible.gd | 23 +++++++++++++++++++++++ collector/collectible.tscn | 6 ++++++ collector/collector.gd | 20 ++++++++++++++++++++ collector/collector.tscn | 8 ++++++++ 4 files changed, 57 insertions(+) create mode 100644 collector/collectible.gd create mode 100644 collector/collectible.tscn create mode 100644 collector/collector.gd create mode 100644 collector/collector.tscn diff --git a/collector/collectible.gd b/collector/collectible.gd new file mode 100644 index 0000000..941a2f5 --- /dev/null +++ b/collector/collectible.gd @@ -0,0 +1,23 @@ +extends Node +class_name Collectible + +enum CollectibleType { + UNSET, + COIN_COPPER, + COIN_SILVER, + COIN_GOLD, + GEM, + DIAMOND, + COAL, + CROWN, + SUPERCROWN, +} + +@export var type: CollectibleType + +signal collected + + +func collect(): + emit_signal("collected") + get_parent().queue_free() diff --git a/collector/collectible.tscn b/collector/collectible.tscn new file mode 100644 index 0000000..7f6b2a1 --- /dev/null +++ b/collector/collectible.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://bk1vvq5rug01m"] + +[ext_resource type="Script" path="res://collector/collectible.gd" id="1_qilbk"] + +[node name="Collectible" type="Node"] +script = ExtResource("1_qilbk") diff --git a/collector/collector.gd b/collector/collector.gd new file mode 100644 index 0000000..2cc74c3 --- /dev/null +++ b/collector/collector.gd @@ -0,0 +1,20 @@ +extends Area2D +class_name Collector + + +var collected_count: int = 0 + +@export var collecting_types: Array[Collectible.CollectibleType] +@export_flags_2d_physics var collecting_collision_mask: int + +signal collected(what: PhysicsBody2D) + + +func _on_body_entered(body: Node2D): + if body is PhysicsBody2D: + if body.collision_layer & collecting_collision_mask: + var collectible: Collectible = body.get_node("Collectible") + if collectible.type in collecting_types: + collected_count += 1 + collectible.collect() + emit_signal("collected", body) diff --git a/collector/collector.tscn b/collector/collector.tscn new file mode 100644 index 0000000..0273735 --- /dev/null +++ b/collector/collector.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=3 uid="uid://c5w3b55aiui6c"] + +[ext_resource type="Script" path="res://collector/collector.gd" id="1_1xtt5"] + +[node name="Collector" type="Area2D"] +script = ExtResource("1_1xtt5") + +[connection signal="body_entered" from="." to="." method="_on_body_entered"]