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
+45 -1
View File
@@ -41,6 +41,7 @@ var path_pending := false
var has_reported_navigation_result := true
var _animal_definition: AnimalDefinition
var _presentation_parameters: Dictionary = {}
var _active_world_adapter: ActiveWorldAdapter
func _ready() -> void:
@@ -59,7 +60,8 @@ func _ready() -> void:
initial_enabled = false
_update_presentation()
return
var existing := get_by_id(animal_id)
_active_world_adapter = ActiveWorldAdapter.find_for_node(self)
var existing := get_by_id_in_context(animal_id, _active_world_adapter)
if existing != null:
push_error(
(
@@ -73,12 +75,15 @@ func _ready() -> void:
_all.append(self)
add_to_group("animal_nodes")
add_to_group("grass_interactors")
set_notify_transform(true)
_try_register_with_simulation()
_notify_world_adapter()
_update_presentation()
func _exit_tree() -> void:
navigation_request_id += 1
_unregister_from_world_adapter()
_all.erase(self)
_disconnect_state()
state = null
@@ -105,6 +110,7 @@ func bind_state(animal_state: AnimalStateRecord) -> bool:
call_deferred("_sync_navigation_from_state")
_update_presentation()
hunger_changed.emit(animal_id, state.get_hunger())
_notify_world_adapter()
return true
@@ -126,6 +132,37 @@ func _try_register_with_simulation() -> void:
manager.animal_care.register_node(self)
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 get_interaction_position() -> Vector3:
return interaction_point.global_position if interaction_point != null else global_position
@@ -381,5 +418,12 @@ static func get_by_id(search_id: StringName) -> AnimalNode:
return null
static func get_by_id_in_context(search_id: StringName, adapter: ActiveWorldAdapter) -> AnimalNode:
for node in _all:
if node.animal_id == search_id and ActiveWorldAdapter.find_for_node(node) == adapter:
return node
return null
static func get_all() -> Array[AnimalNode]:
return _all.duplicate()