2023-10-01 01:03:03 +00:00
|
|
|
extends Area2D
|
|
|
|
class_name Collector
|
2023-10-01 01:20:47 +00:00
|
|
|
## Area that will pick up [Collectible]s with a given name, keeping track of the amount collected.
|
2023-10-01 01:03:03 +00:00
|
|
|
|
|
|
|
|
2023-10-01 01:20:47 +00:00
|
|
|
## The current amount of collected entities.
|
2023-10-01 01:03:03 +00:00
|
|
|
var collected_count: int = 0
|
|
|
|
|
2023-10-01 01:20:47 +00:00
|
|
|
## 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.
|
2023-10-01 01:03:03 +00:00
|
|
|
@export_flags_2d_physics var collecting_collision_mask: int
|
|
|
|
|
2023-10-01 01:20:47 +00:00
|
|
|
## 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.
|
2023-10-01 01:03:03 +00:00
|
|
|
signal collected(what: PhysicsBody2D)
|
|
|
|
|
2023-10-01 01:20:47 +00:00
|
|
|
## The collector has received its collection goal and is about to reset.
|
|
|
|
signal goal
|
|
|
|
|
2023-10-01 01:03:03 +00:00
|
|
|
|
|
|
|
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:
|
2023-10-01 09:56:54 +00:00
|
|
|
print("collezionato")
|
2023-10-01 01:03:03 +00:00
|
|
|
collected_count += 1
|
|
|
|
collectible.collect()
|
|
|
|
emit_signal("collected", body)
|
2023-10-01 01:20:47 +00:00
|
|
|
if collected_count >= collecting_amount:
|
2023-10-01 09:56:54 +00:00
|
|
|
print("goal")
|
2023-10-01 01:20:47 +00:00
|
|
|
emit_signal("goal")
|
|
|
|
collected_count = 0
|