refactor: scope active world targets

This commit is contained in:
Rijad Zuzo
2026-08-12 21:58:52 +02:00
parent d12e9515fb
commit acffe6aa0f
8 changed files with 950 additions and 57 deletions
+55 -1
View File
@@ -7,11 +7,13 @@ static var _all: Array = []
@export var action_id: StringName = SimulationIds.ACTION_REST
@export var display_name := "Activity Site"
@export_range(1, 12, 1) var capacity := 1
@export_range(0.1, 100.0, 0.1, "or_greater") var interaction_range := 3.0
@export var debug_label_enabled := true
@onready var interaction_point: Marker3D = $InteractionPoint
var _last_label_text := ""
var _active_world_adapter: ActiveWorldAdapter
func _ready() -> void:
@@ -19,7 +21,12 @@ func _ready() -> void:
push_error("ActivitySite at %s has empty site_id" % get_path())
_update_presentation()
return
var existing: ActivitySite = get_by_id(site_id) as ActivitySite
if not is_interaction_range_valid():
push_error("ActivitySite '%s' must have a positive finite interaction_range" % site_id)
_update_presentation()
return
_active_world_adapter = ActiveWorldAdapter.find_for_node(self)
var existing: ActivitySite = get_by_id_in_context(site_id, _active_world_adapter)
if existing != null:
push_error(
(
@@ -31,11 +38,14 @@ func _ready() -> void:
return
_all.append(self)
add_to_group("activity_sites")
set_notify_transform(true)
_notify_world_adapter()
_update_presentation()
set_process(false)
func _exit_tree() -> void:
_unregister_from_world_adapter()
_all.erase(self)
@@ -52,6 +62,41 @@ func supports_action(candidate_action_id: StringName) -> bool:
)
func is_interaction_range_valid() -> bool:
return is_finite(interaction_range) and interaction_range > 0.0
func _notification(what: int) -> void:
if what == NOTIFICATION_TRANSFORM_CHANGED and is_node_ready():
if is_instance_valid(_active_world_adapter):
_active_world_adapter.refresh_world_target_position(self)
func _notify_world_adapter() -> void:
if not is_inside_tree():
return
if (
not is_instance_valid(_active_world_adapter)
or not _active_world_adapter.owns_target_node(self)
):
_active_world_adapter = ActiveWorldAdapter.find_for_node(self)
if _active_world_adapter != null:
_active_world_adapter.register_world_target(self)
func _bind_active_world_adapter(adapter: ActiveWorldAdapter) -> void:
if adapter != null and adapter.owns_target_node(self):
_active_world_adapter = adapter
func _unregister_from_world_adapter() -> void:
if not is_instance_valid(_active_world_adapter):
_active_world_adapter = ActiveWorldAdapter.find_for_node(self)
if is_instance_valid(_active_world_adapter):
_active_world_adapter.unregister_world_target(self)
_active_world_adapter = null
func _update_presentation() -> void:
if not has_node("DebugLabel"):
return
@@ -78,5 +123,14 @@ static func get_by_id(search_id: StringName):
return null
static func get_by_id_in_context(
search_id: StringName, adapter: ActiveWorldAdapter
) -> ActivitySite:
for node in _all:
if node.site_id == search_id and ActiveWorldAdapter.find_for_node(node) == adapter:
return node as ActivitySite
return null
static func get_all() -> Array:
return _all.duplicate()