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
+40 -15
View File
@@ -26,6 +26,7 @@ static var _all: Array[ResourceNode] = []
@onready var interaction_point: Marker3D = $InteractionPoint
var state: ResourceStateRecord
var _active_world_adapter: ActiveWorldAdapter
func _ready() -> void:
@@ -34,7 +35,8 @@ func _ready() -> void:
initial_enabled = false
_update_presentation()
return
var existing := get_by_id(node_id)
_active_world_adapter = ActiveWorldAdapter.find_for_node(self)
var existing := get_by_id_in_context(node_id, _active_world_adapter)
if existing != null:
push_error(
(
@@ -49,12 +51,12 @@ func _ready() -> void:
add_to_group("resource_nodes")
set_notify_transform(true)
_try_register_with_simulation()
_notify_world_adapters()
_notify_world_adapter()
_update_presentation()
func _exit_tree() -> void:
_unregister_from_world_adapters()
_unregister_from_world_adapter()
_all.erase(self)
if state != null and state.changed.is_connected(_on_state_changed):
state.changed.disconnect(_on_state_changed)
@@ -77,7 +79,7 @@ func bind_state(resource_state: ResourceStateRecord) -> bool:
state.depleted.connect(_on_state_depleted)
_update_presentation()
amount_changed.emit(node_id, state.get_amount_remaining())
_notify_world_adapters()
_notify_world_adapter()
return true
@@ -92,23 +94,37 @@ func _try_register_with_simulation() -> void:
func _notification(what: int) -> void:
if what == NOTIFICATION_TRANSFORM_CHANGED and is_node_ready():
_notify_world_adapters()
_refresh_world_target_position()
func _notify_world_adapters() -> void:
func _notify_world_adapter() -> void:
if not is_inside_tree():
return
for adapter in get_tree().get_nodes_in_group("active_world_adapter"):
if adapter.has_method("register_resource_node"):
adapter.register_resource_node(self, state)
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_resource_node(self, state)
func _unregister_from_world_adapters() -> void:
if not is_inside_tree():
return
for adapter in get_tree().get_nodes_in_group("active_world_adapter"):
if adapter.has_method("unregister_resource_node"):
adapter.unregister_resource_node(node_id)
func _bind_active_world_adapter(adapter: ActiveWorldAdapter) -> void:
if adapter != null and adapter.owns_target_node(self):
_active_world_adapter = adapter
func _refresh_world_target_position() -> void:
if is_instance_valid(_active_world_adapter):
_active_world_adapter.refresh_world_target_position(self)
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_resource_node(node_id, self)
_active_world_adapter = null
func get_amount_remaining() -> float:
@@ -177,5 +193,14 @@ static func get_by_id(search_id: StringName) -> ResourceNode:
return null
static func get_by_id_in_context(
search_id: StringName, adapter: ActiveWorldAdapter
) -> ResourceNode:
for node in _all:
if node.node_id == search_id and ActiveWorldAdapter.find_for_node(node) == adapter:
return node
return null
static func get_all() -> Array[ResourceNode]:
return _all.duplicate()