2023-10-01 03:03:03 +02:00
|
|
|
extends Area2D
|
|
|
|
class_name Collector
|
2023-10-01 03:20:47 +02:00
|
|
|
## Area that will pick up [Collectible]s with a given name, keeping track of the amount collected.
|
2023-10-01 03:03:03 +02:00
|
|
|
|
|
|
|
|
2023-10-01 03:20:47 +02:00
|
|
|
## The current amount of collected entities.
|
2023-10-01 03:03:03 +02:00
|
|
|
var collected_count: int = 0
|
|
|
|
|
2023-10-01 03:20:47 +02:00
|
|
|
## The types of [Collectible]s to pick up.
|
|
|
|
##
|
|
|
|
## The strings will match only if exactly the same.
|
|
|
|
@export var collecting_types: Array[StringName]
|
2023-10-14 02:27:55 +02:00
|
|
|
|
|
|
|
## The sound played when an item is collected.
|
|
|
|
@export var sound_absorb: AudioStreamPlayer2D
|
|
|
|
|
2023-10-25 00:02:38 +02:00
|
|
|
@export_range(0.01, 4) var sound_absorb_min_pitch: float = 1.0
|
|
|
|
@export_range(0.01, 4) var sound_absorb_max_pitch: float = 1.0
|
|
|
|
|
2023-10-01 03:20:47 +02: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 21:58:56 +02:00
|
|
|
signal collected(body: PhysicsBody2D)
|
2023-10-01 03:03:03 +02:00
|
|
|
|
2023-10-01 03:20:47 +02:00
|
|
|
## The collector has received its collection goal and is about to reset.
|
|
|
|
signal goal
|
|
|
|
|
2023-10-01 03:03:03 +02:00
|
|
|
|
|
|
|
func _on_body_entered(body: Node2D):
|
|
|
|
if body is PhysicsBody2D:
|
2023-10-02 02:53:12 +02:00
|
|
|
var collectible: Collectible = body.find_child("Collectible")
|
|
|
|
if collectible and collectible.type in collecting_types:
|
|
|
|
collected_count += 1
|
|
|
|
collectible.collect()
|
2023-10-02 16:31:08 +02:00
|
|
|
if sound_absorb:
|
2023-10-25 00:02:38 +02:00
|
|
|
sound_absorb.pitch_scale = sound_absorb_min_pitch + (float(collected_count) / float(collecting_amount)) * (sound_absorb_max_pitch - sound_absorb_min_pitch)
|
2023-10-02 16:31:08 +02:00
|
|
|
sound_absorb.play()
|
2023-10-02 21:25:11 +02:00
|
|
|
collected.emit(body)
|
2023-10-02 02:53:12 +02:00
|
|
|
if collected_count >= collecting_amount:
|
2023-10-03 01:48:59 +02:00
|
|
|
goal.emit()
|
2023-10-02 02:53:12 +02:00
|
|
|
collected_count = 0
|