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
+405 -38
View File
@@ -5,31 +5,208 @@ const LoadedResourceSpatialIndexScript := preload(
"res://world/resource_nodes/LoadedResourceSpatialIndex.gd"
)
const DEFAULT_CONTEXT_ID := &"jajce_interior"
const DEFAULT_WORLD_ID := &"regional_bosnia"
const DEFAULT_LOCATION_ID := &"location_jajce"
const TARGET_KIND_RESOURCE := &"resource"
const TARGET_KIND_STORAGE := &"storage"
const TARGET_KIND_ACTIVITY := &"activity"
const TARGET_KIND_ANIMAL := &"animal"
const TARGET_GROUPS: Array[StringName] = [
&"resource_nodes", &"storage_nodes", &"activity_sites", &"animal_nodes"
]
@export var context_id: StringName = DEFAULT_CONTEXT_ID
@export var world_id: StringName = DEFAULT_WORLD_ID
@export var location_id: StringName = DEFAULT_LOCATION_ID
@export var pantry_storage: StorageNode
@export var woodpile_storage: StorageNode
@export_range(4.0, 128.0, 1.0) var resource_cell_size := 24.0
var _resource_index := LoadedResourceSpatialIndexScript.new()
var _target_registry: WorldTargetRegistry
var _target_handles_by_provider_id: Dictionary = {}
func _ready() -> void:
_resource_index.configure(resource_cell_size)
add_to_group("active_world_adapter")
rebuild_resource_index()
_ensure_target_registry()
rebuild_target_registry()
func _exit_tree() -> void:
_resource_index.clear()
_target_handles_by_provider_id.clear()
if _target_registry != null:
_target_registry.clear()
func configure_context_identity(
next_context_id: StringName, next_world_id: StringName, next_location_id: StringName
) -> bool:
if next_context_id.is_empty() or next_world_id.is_empty() or next_location_id.is_empty():
return false
context_id = next_context_id
world_id = next_world_id
location_id = next_location_id
_target_registry = WorldTargetRegistry.new(context_id, world_id, location_id)
_target_handles_by_provider_id.clear()
_resource_index.clear()
if is_node_ready():
rebuild_target_registry()
return true
func get_context_identity() -> Dictionary:
_ensure_target_registry()
return _target_registry.get_identity()
func get_target_registry() -> WorldTargetRegistry:
_ensure_target_registry()
return _target_registry
func get_target_handle(target_id: StringName) -> WorldTargetHandle:
_ensure_target_registry()
return _target_registry.get_handle(target_id)
func resolve_target_handle(handle: WorldTargetHandle) -> WorldTargetDescriptor:
_ensure_target_registry()
return _target_registry.resolve_handle(handle)
func get_target_provider(target_id: StringName, target_kind: StringName = &"") -> Object:
_ensure_target_registry()
var handle := _target_registry.get_handle(target_id)
if handle == null or (not target_kind.is_empty() and handle.get_target_kind() != target_kind):
return null
return _target_registry.get_provider(handle)
func owns_target_node(target: Node) -> bool:
return find_for_node(target) == self
static func find_for_node(target: Node) -> ActiveWorldAdapter:
if target == null:
return null
var current: Node = target
while current != null:
if current is ActiveWorldAdapter:
return current as ActiveWorldAdapter
for child in current.get_children():
if child is ActiveWorldAdapter:
return child as ActiveWorldAdapter
current = current.get_parent()
return null
static func nodes_share_context(first: Node, second: Node) -> bool:
if first == null or second == null:
return false
return find_for_node(first) == find_for_node(second)
func rebuild_target_registry() -> void:
_ensure_target_registry()
_target_registry.clear()
_target_handles_by_provider_id.clear()
_resource_index.clear()
if not is_inside_tree():
return
for group_id in TARGET_GROUPS:
for candidate in get_tree().get_nodes_in_group(group_id):
var provider := candidate as Node
if provider != null and owns_target_node(provider):
register_world_target(provider)
func rebuild_resource_index() -> void:
_resource_index.clear()
for node in ResourceNode.get_all():
_resource_index.register_node(node, node.state)
_ensure_target_registry()
for target_id in _target_registry.get_target_ids(TARGET_KIND_RESOURCE):
var node := get_target_provider(target_id, TARGET_KIND_RESOURCE) as ResourceNode
if node != null:
_resource_index.register_node(node, node.state)
func register_world_target(provider: Node) -> WorldTargetHandle:
if provider == null or not owns_target_node(provider):
return null
_bind_provider(provider)
var descriptor := _descriptor_for_provider(provider)
if descriptor == null:
return null
var handle := _register_descriptor(provider, descriptor)
if handle != null and provider is ResourceNode:
var resource := provider as ResourceNode
if not _resource_index.register_node(resource, resource.state):
unregister_world_target(provider)
return null
return handle
func unregister_world_target(provider: Node) -> bool:
if provider == null:
return false
_ensure_target_registry()
var provider_id := provider.get_instance_id()
var handle := _target_handles_by_provider_id.get(provider_id) as WorldTargetHandle
if handle == null or _target_registry.get_provider(handle) != provider:
_target_handles_by_provider_id.erase(provider_id)
return false
var target_id := handle.get_target_id()
var target_kind := handle.get_target_kind()
var removed := _target_registry.unregister_target(handle)
_target_handles_by_provider_id.erase(provider_id)
if removed and target_kind == TARGET_KIND_RESOURCE:
_resource_index.unregister_node(target_id)
return removed
func refresh_world_target_position(provider: Node) -> bool:
if provider == null or not owns_target_node(provider):
return false
_ensure_target_registry()
var handle := (
_target_handles_by_provider_id.get(provider.get_instance_id()) as WorldTargetHandle
)
if handle == null or not _target_registry.is_handle_valid(handle, true):
return register_world_target(provider) != null
var position := _interaction_position_for(provider)
if not _target_registry.update_target_position(handle, position):
return false
if provider is ResourceNode:
var resource := provider as ResourceNode
return _resource_index.register_node(resource, resource.state)
return true
func register_resource_node(node: ResourceNode, state: ResourceStateRecord = null) -> bool:
return _resource_index.register_node(node, state)
if node == null or not owns_target_node(node):
return false
_bind_provider(node)
var descriptor := _resource_descriptor(node, state)
var handle := _register_descriptor(node, descriptor)
return handle != null and _resource_index.register_node(node, state)
func unregister_resource_node(node_id: StringName) -> void:
_resource_index.unregister_node(node_id)
func unregister_resource_node(node_id: StringName, provider: ResourceNode = null) -> void:
if provider != null:
unregister_world_target(provider)
return
_ensure_target_registry()
var handle := _target_registry.get_handle(node_id)
if handle == null or handle.get_target_kind() != TARGET_KIND_RESOURCE:
return
var registered_provider := _target_registry.get_provider(handle)
if registered_provider != null:
_target_handles_by_provider_id.erase(registered_provider.get_instance_id())
if _target_registry.unregister_target(handle):
_resource_index.unregister_node(node_id)
func get_resource_candidates(action_id: StringName) -> Array[Dictionary]:
@@ -60,31 +237,29 @@ func get_resource_index_stats() -> Dictionary:
func get_animal_candidates(action_id: StringName) -> Array[Dictionary]:
_ensure_target_registry()
var candidates: Array[Dictionary] = []
for node in AnimalNode.get_all():
if not node.supports_action(action_id):
for target_id in _target_registry.get_target_ids(TARGET_KIND_ANIMAL, action_id):
var descriptor := _target_registry.get_descriptor(target_id)
if descriptor == null:
continue
(
candidates
. append(
{
"target_id": String(node.animal_id),
"position": node.get_interaction_position(),
"display_name": node.display_name,
"target_id": String(target_id),
"position": descriptor.get_local_position(),
"display_name": descriptor.get_display_name(),
}
)
)
candidates.sort_custom(
func(first: Dictionary, second: Dictionary) -> bool:
return String(first["target_id"]) < String(second["target_id"])
)
return candidates
func get_animal_routine_sites(species_id: StringName, animal_id: StringName) -> Array[Dictionary]:
var candidates: Array[Dictionary] = []
for site in AnimalRoutineSite.get_all():
if not site.supports_animal(animal_id, species_id):
if not owns_target_node(site) or not site.supports_animal(animal_id, species_id):
continue
(
candidates
@@ -104,10 +279,21 @@ func get_animal_routine_sites(species_id: StringName, animal_id: StringName) ->
func get_animal_nodes_in_radius(origin: Vector3, max_distance: float) -> Array[AnimalNode]:
_ensure_target_registry()
var nodes: Array[AnimalNode] = []
var maximum_distance_squared := maxf(max_distance, 0.0) * maxf(max_distance, 0.0)
for node in AnimalNode.get_all():
if origin.distance_squared_to(node.get_interaction_position()) <= maximum_distance_squared:
for target_id in _target_registry.get_target_ids(TARGET_KIND_ANIMAL):
var descriptor := _target_registry.get_descriptor(target_id)
if (
descriptor == null
or (
origin.distance_squared_to(descriptor.get_local_position())
> maximum_distance_squared
)
):
continue
var node := get_target_provider(target_id, TARGET_KIND_ANIMAL) as AnimalNode
if node != null:
nodes.append(node)
nodes.sort_custom(
func(first: AnimalNode, second: AnimalNode) -> bool:
@@ -142,33 +328,214 @@ func get_activity_target(action_id: StringName, origin: Vector3 = Vector3.ZERO)
func get_activity_candidates(action_id: StringName) -> Array[Dictionary]:
_ensure_target_registry()
var candidates: Array[Dictionary] = []
for candidate in ActivitySite.get_all():
var site := candidate as ActivitySite
if site == null:
for target_id in _target_registry.get_target_ids(TARGET_KIND_ACTIVITY, action_id):
var descriptor := _target_registry.get_descriptor(target_id)
if descriptor == null:
continue
if not site.supports_action(action_id):
continue
candidates.append(
{
"target_id": String(site.site_id),
"position": site.get_interaction_position(),
"capacity": site.capacity
}
(
candidates
. append(
{
"target_id": String(target_id),
"position": descriptor.get_local_position(),
"capacity": int(descriptor.get_metadata_value(&"capacity", 1)),
}
)
)
return candidates
func get_storage_target(storage_id: StringName) -> Dictionary:
var storage_node: StorageNode
match storage_id:
SimulationIds.STORAGE_VILLAGE_PANTRY:
storage_node = pantry_storage
SimulationIds.STORAGE_VILLAGE_WOODPILE:
storage_node = woodpile_storage
if storage_node == null:
_ensure_target_registry()
var descriptor := _target_registry.get_descriptor(storage_id)
if descriptor == null or descriptor.get_target_kind() != TARGET_KIND_STORAGE:
return {}
return {
"target_id": String(storage_node.storage_id),
"position": storage_node.get_interaction_position()
"target_id": String(storage_id),
"position": descriptor.get_local_position(),
}
func _ensure_target_registry() -> void:
if _target_registry == null:
_target_registry = WorldTargetRegistry.new(context_id, world_id, location_id)
func _register_descriptor(provider: Node, descriptor: WorldTargetDescriptor) -> WorldTargetHandle:
_ensure_target_registry()
if provider == null or descriptor == null or not descriptor.is_valid():
return null
var provider_id := provider.get_instance_id()
var handle := _target_handles_by_provider_id.get(provider_id) as WorldTargetHandle
if handle != null and _target_registry.is_handle_valid(handle, true):
if (
handle.get_target_id() == descriptor.get_target_id()
and handle.get_target_kind() == descriptor.get_target_kind()
):
return handle if _target_registry.update_target(handle, descriptor) else null
unregister_world_target(provider)
var existing_handle := _target_registry.get_handle(descriptor.get_target_id())
if existing_handle != null:
if _target_registry.get_provider(existing_handle) != provider:
_target_registry.register_target(descriptor, provider)
return null
handle = existing_handle
if not _target_registry.update_target(handle, descriptor):
return null
else:
handle = _target_registry.register_target(descriptor, provider)
if handle != null:
_target_handles_by_provider_id[provider_id] = handle
return handle
func _bind_provider(provider: Node) -> void:
if provider.has_method("_bind_active_world_adapter"):
provider.call("_bind_active_world_adapter", self)
func _descriptor_for_provider(provider: Node) -> WorldTargetDescriptor:
if provider is ResourceNode:
var resource := provider as ResourceNode
return _resource_descriptor(resource, resource.state)
if provider is StorageNode:
return _storage_descriptor(provider as StorageNode)
if provider is ActivitySite:
return _activity_descriptor(provider as ActivitySite)
if provider is AnimalNode:
return _animal_descriptor(provider as AnimalNode)
return null
func _resource_descriptor(
node: ResourceNode, state: ResourceStateRecord = null
) -> WorldTargetDescriptor:
if node == null or node.node_id.is_empty():
return null
var action_id := state.get_action_id() if state != null else node.action_id
var resource_id := state.get_resource_id() if state != null else node.resource_id
var capability := (
WorldTargetCapability
. new(
action_id,
{
"resource_id": String(resource_id),
"can_npcs_use": node.can_npcs_use,
"can_player_use": node.can_player_use,
}
)
)
return (
WorldTargetDescriptor
. new(
node.node_id,
TARGET_KIND_RESOURCE,
[capability],
_interaction_position_for(node),
String(node.name),
{
"resource_id": String(resource_id),
"safety_risk": state.get_safety_risk() if state != null else node.safety_risk,
"comfort_distance":
state.get_comfort_distance() if state != null else node.comfort_distance,
"discovery_priority":
state.get_discovery_priority() if state != null else node.discovery_priority,
}
)
)
func _storage_descriptor(node: StorageNode) -> WorldTargetDescriptor:
if node == null or node.storage_id.is_empty():
return null
var capability_ids: Array[StringName] = []
if node.accepts_item(SimulationIds.RESOURCE_FOOD):
(
capability_ids
. append_array(
[
SimulationIds.ACTION_EAT,
SimulationIds.ACTION_DEPOSIT_FOOD,
SimulationIds.ACTION_WITHDRAW_FOOD,
]
)
)
if node.accepts_item(SimulationIds.RESOURCE_WOOD):
capability_ids.append(SimulationIds.ACTION_DEPOSIT_WOOD)
var capabilities: Array[WorldTargetCapability] = []
for capability_id in capability_ids:
capabilities.append(WorldTargetCapability.new(capability_id))
var accepted_item_ids: Array[String] = []
for item_id in node.accepted_items:
accepted_item_ids.append(String(item_id))
return WorldTargetDescriptor.new(
node.storage_id,
TARGET_KIND_STORAGE,
capabilities,
_interaction_position_for(node),
node.display_name,
{"accepted_item_ids": accepted_item_ids}
)
func _activity_descriptor(node: ActivitySite) -> WorldTargetDescriptor:
if node == null or node.site_id.is_empty() or not node.is_interaction_range_valid():
return null
var capability_ids: Array[StringName] = [node.action_id]
if node.action_id == SimulationIds.ACTION_PATROL:
capability_ids.append(SimulationIds.ACTION_DEFEND)
var capabilities: Array[WorldTargetCapability] = []
for capability_id in capability_ids:
capabilities.append(
WorldTargetCapability.new(capability_id, {"interaction_range": node.interaction_range})
)
return WorldTargetDescriptor.new(
node.site_id,
TARGET_KIND_ACTIVITY,
capabilities,
_interaction_position_for(node),
node.display_name,
{"capacity": node.capacity}
)
func _animal_descriptor(node: AnimalNode) -> WorldTargetDescriptor:
if node == null or node.animal_id.is_empty():
return null
var capabilities: Array[WorldTargetCapability] = []
for capability_id in node.supported_action_ids:
capabilities.append(WorldTargetCapability.new(capability_id))
return (
WorldTargetDescriptor
. new(
node.animal_id,
TARGET_KIND_ANIMAL,
capabilities,
_interaction_position_for(node),
node.display_name,
{
"animal_definition_id": String(node.animal_definition_id),
"species_id": String(node.species_id),
}
)
)
func _interaction_position_for(provider: Node) -> Vector3:
if provider is ResourceNode:
var resource := provider as ResourceNode
return (
resource.interaction_point.global_position
if resource.interaction_point != null
else resource.global_position
)
if provider is StorageNode:
return (provider as StorageNode).get_interaction_position()
if provider is ActivitySite:
return (provider as ActivitySite).get_interaction_position()
if provider is AnimalNode:
return (provider as AnimalNode).get_interaction_position()
return Vector3.ZERO
+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()
+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()
+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()
+45 -1
View File
@@ -15,6 +15,7 @@ const PRESENTATION_UPDATE_INTERVAL_SECONDS := 0.25
var state: StorageStateRecord
var _last_label_text := ""
var _presentation_elapsed := 0.0
var _active_world_adapter: ActiveWorldAdapter
func _ready() -> void:
@@ -24,7 +25,8 @@ func _ready() -> void:
push_error("StorageNode at %s has empty storage_id" % get_path())
_update_presentation()
return
var existing: StorageNode = get_by_id(storage_id) as StorageNode
_active_world_adapter = ActiveWorldAdapter.find_for_node(self)
var existing: StorageNode = get_by_id_in_context(storage_id, _active_world_adapter)
if existing != null:
push_error(
(
@@ -36,7 +38,9 @@ func _ready() -> void:
return
_all.append(self)
add_to_group("storage_nodes")
set_notify_transform(true)
_try_register_with_simulation()
_notify_world_adapter()
_update_presentation()
set_process(debug_label_enabled)
@@ -50,6 +54,7 @@ func _process(delta: float) -> void:
func _exit_tree() -> void:
_unregister_from_world_adapter()
_all.erase(self)
state = null
@@ -59,6 +64,7 @@ func bind_state(storage_state: StorageStateRecord) -> bool:
return false
state = storage_state
_update_presentation()
_notify_world_adapter()
return true
@@ -79,6 +85,37 @@ func _try_register_with_simulation() -> void:
manager.register_storage_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 _update_presentation() -> void:
if not has_node("DebugLabel"):
return
@@ -112,5 +149,12 @@ static func get_by_id(search_id: StringName):
return null
static func get_by_id_in_context(search_id: StringName, adapter: ActiveWorldAdapter) -> StorageNode:
for node in _all:
if node.storage_id == search_id and ActiveWorldAdapter.find_for_node(node) == adapter:
return node as StorageNode
return null
static func get_all() -> Array:
return _all.duplicate()
+38 -1
View File
@@ -4,6 +4,8 @@ extends RefCounted
var last_error := ""
var _context_id: StringName
var _world_id: StringName
var _location_id: StringName
var _entries_by_id: Dictionary = {}
var _generation_by_id: Dictionary = {}
var _sorted_target_ids: Array[StringName] = []
@@ -11,8 +13,14 @@ var _target_ids_by_kind: Dictionary = {}
var _target_ids_by_capability: Dictionary = {}
func _init(owned_context_id: StringName = &"") -> void:
func _init(
owned_context_id: StringName = &"",
owned_world_id: StringName = &"",
owned_location_id: StringName = &""
) -> void:
_context_id = owned_context_id
_world_id = owned_world_id
_location_id = owned_location_id
func register_target(
@@ -75,6 +83,19 @@ func update_target(handle: WorldTargetHandle, descriptor: WorldTargetDescriptor)
return true
func update_target_position(handle: WorldTargetHandle, local_position: Vector3) -> bool:
last_error = ""
var entry := _get_handle_entry(handle)
if entry.is_empty():
return _fail_bool("Target handle is stale or belongs to another registry")
if not local_position.is_finite():
return _fail_bool("Target position must be finite")
var previous := entry["descriptor"] as WorldTargetDescriptor
entry["descriptor"] = previous.with_local_position(local_position)
_entries_by_id[previous.get_target_id()] = entry
return true
func unregister_target(handle: WorldTargetHandle) -> bool:
last_error = ""
var entry := _get_handle_entry(handle)
@@ -112,6 +133,22 @@ func get_context_id() -> StringName:
return _context_id
func get_world_id() -> StringName:
return _world_id
func get_location_id() -> StringName:
return _location_id
func get_identity() -> Dictionary:
return {
"context_id": _context_id,
"world_id": _world_id,
"location_id": _location_id,
}
func size() -> int:
return _entries_by_id.size()