2024-04-17 02:52:11 +00:00
|
|
|
@icon("res://behaviours/tracker.svg")
|
2024-04-16 00:47:31 +00:00
|
|
|
extends Area2D
|
|
|
|
class_name Tracker
|
|
|
|
|
2024-04-19 02:30:35 +00:00
|
|
|
## [Area2D]s tracking a certain subset of [Node2D]s.
|
2024-04-16 00:47:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
signal tracked(body: Node2D)
|
|
|
|
signal untracked(body: Node2D)
|
2024-04-30 03:49:36 +00:00
|
|
|
signal empty
|
2024-04-16 00:47:31 +00:00
|
|
|
|
|
|
|
var tracking: Array = []
|
|
|
|
|
|
|
|
|
2024-04-19 02:30:35 +00:00
|
|
|
## Start [field tracking] a [Node2D].
|
|
|
|
##
|
|
|
|
## Returns whether the body was added to the [field tracking] array.
|
2024-04-16 00:47:31 +00:00
|
|
|
func track(body: Node2D) -> bool:
|
|
|
|
var act: bool = not body in tracking
|
|
|
|
if act:
|
|
|
|
tracking.push_back(body)
|
|
|
|
tracked.emit(body)
|
2024-04-17 02:52:11 +00:00
|
|
|
# Handle TrackerTracker
|
2024-04-19 01:23:24 +00:00
|
|
|
for tracker_tracker in body.find_children("*", "TrackerTracker", true, false):
|
2024-04-19 02:30:35 +00:00
|
|
|
tracker_tracker.track_if_detected(self)
|
2024-04-16 00:47:31 +00:00
|
|
|
return act
|
|
|
|
|
2024-04-19 02:30:35 +00:00
|
|
|
## Stop [field tracking] a [Node2D].
|
|
|
|
##
|
|
|
|
## Returns whether the body was removed from the [field tracking] array.
|
2024-04-16 00:47:31 +00:00
|
|
|
func untrack(body: Node2D) -> bool:
|
|
|
|
var act: bool = body in tracking
|
|
|
|
if act:
|
|
|
|
tracking.erase(body)
|
|
|
|
untracked.emit(body)
|
2024-04-30 03:49:36 +00:00
|
|
|
if tracking.is_empty():
|
|
|
|
empty.emit()
|
2024-04-17 02:52:11 +00:00
|
|
|
# Handle TrackerTracker
|
2024-04-19 01:23:24 +00:00
|
|
|
for tracker_tracker in body.find_children("*", "TrackerTracker", true, false):
|
2024-04-17 02:52:11 +00:00
|
|
|
tracker_tracker.untrack(self)
|
2024-04-16 00:47:31 +00:00
|
|
|
return act
|
|
|
|
|
|
|
|
func log_tracked(body: Node2D) -> void:
|
|
|
|
Log.p(self, "Tracking a new target: %s" % body)
|
|
|
|
|
|
|
|
func log_untracked(body: Node2D) -> void:
|
|
|
|
Log.p(self, "Not tracking anymore target: %s" % body)
|