1
Fork 0
mirror of https://github.com/Steffo99/swear-jar.git synced 2024-11-21 23:34:18 +00:00

Document collector entity

This commit is contained in:
Steffo 2023-10-01 03:20:47 +02:00
parent e90830cd06
commit a3826b9801
Signed by: steffo
GPG key ID: 2A24051445686895
3 changed files with 33 additions and 16 deletions

View file

@ -1,23 +1,18 @@
extends Node extends Node
class_name Collectible class_name Collectible
## A marker for collectible entities.
##
## Used by [Collector]s to determine which entities to pick up.
enum CollectibleType {
UNSET,
COIN_COPPER,
COIN_SILVER,
COIN_GOLD,
GEM,
DIAMOND,
COAL,
CROWN,
SUPERCROWN,
}
@export var type: CollectibleType ## The type of collectible entity the parent entity represents.
@export var type: StringName
## Emitted when this entity has been collected by a collector.
signal collected signal collected
## Mark this entity as collected.
##
## You'll probably want to connect this to an AudioSource2D, which will disable the node and play a sound, and when the sound is over a new signal will queue_free it.
func collect(): func collect():
emit_signal("collected") emit_signal("collected")
get_parent().queue_free()

View file

@ -1,14 +1,30 @@
extends Area2D extends Area2D
class_name Collector class_name Collector
## Area that will pick up [Collectible]s with a given name, keeping track of the amount collected.
## The current amount of collected entities.
var collected_count: int = 0 var collected_count: int = 0
@export var collecting_types: Array[Collectible.CollectibleType] ## The types of [Collectible]s to pick up.
##
## The strings will match only if exactly the same.
@export var collecting_types: Array[StringName]
## The collision mask to check colliding body against.
@export_flags_2d_physics var collecting_collision_mask: int @export_flags_2d_physics var collecting_collision_mask: int
## The goal amount of entities to collect.
##
## When [collected_count] reaches it, it will be reset to zero, and the "goal" signal will be emitted.
@export var collecting_amount: int
## The collector has picked up an object.
signal collected(what: PhysicsBody2D) signal collected(what: PhysicsBody2D)
## The collector has received its collection goal and is about to reset.
signal goal
func _on_body_entered(body: Node2D): func _on_body_entered(body: Node2D):
if body is PhysicsBody2D: if body is PhysicsBody2D:
@ -18,3 +34,6 @@ func _on_body_entered(body: Node2D):
collected_count += 1 collected_count += 1
collectible.collect() collectible.collect()
emit_signal("collected", body) emit_signal("collected", body)
if collected_count >= collecting_amount:
emit_signal("goal")
collected_count = 0

View file

@ -1,8 +1,9 @@
[gd_scene load_steps=5 format=3 uid="uid://c3kitncwpi42j"] [gd_scene load_steps=6 format=3 uid="uid://c3kitncwpi42j"]
[ext_resource type="PhysicsMaterial" uid="uid://c6kn1an85lccr" path="res://entity/coin_physics_material.tres" id="1_8k46m"] [ext_resource type="PhysicsMaterial" uid="uid://c6kn1an85lccr" path="res://entity/coin_physics_material.tres" id="1_8k46m"]
[ext_resource type="Texture2D" uid="uid://dbdkb4vt7dh85" path="res://entity/coin_copper_4.png" id="1_wedw1"] [ext_resource type="Texture2D" uid="uid://dbdkb4vt7dh85" path="res://entity/coin_copper_4.png" id="1_wedw1"]
[ext_resource type="Texture2D" uid="uid://2vtvoj6ua3cb" path="res://entity/coin_copper_outline_2.png" id="2_2ifq3"] [ext_resource type="Texture2D" uid="uid://2vtvoj6ua3cb" path="res://entity/coin_copper_outline_2.png" id="2_2ifq3"]
[ext_resource type="PackedScene" uid="uid://bk1vvq5rug01m" path="res://collector/collectible.tscn" id="4_yefrx"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_c6byl"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_c6byl"]
size = Vector2(16, 5) size = Vector2(16, 5)
@ -28,3 +29,5 @@ texture = ExtResource("1_wedw1")
z_index = -10 z_index = -10
texture_filter = 1 texture_filter = 1
texture = ExtResource("2_2ifq3") texture = ExtResource("2_2ifq3")
[node name="Collectible" parent="." instance=ExtResource("4_yefrx")]