From 4f1766834a45eb79825e1a341003add60a388d05 Mon Sep 17 00:00:00 2001 From: Steven Wroblewski Date: Sat, 9 May 2026 01:07:16 +0200 Subject: [PATCH] refactor(items): strengthen RoomChest types, guard receive_item, expose get_spawned_item --- scripts/objects/room_chest.gd | 14 +++++++++++--- test/unit/test_room_chest.gd | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/scripts/objects/room_chest.gd b/scripts/objects/room_chest.gd index 324a702..5cce863 100644 --- a/scripts/objects/room_chest.gd +++ b/scripts/objects/room_chest.gd @@ -9,7 +9,7 @@ const SPAWN_TWEEN_DURATION: float = 0.3 @export var chest_id: String = "" -var _spawned_items: Array = [] +var _spawned_items: Array[HoldableItem] = [] var _item_configs: Array[ChestItemData] = [] @@ -41,6 +41,8 @@ func spawn_items() -> void: func receive_item(item: HoldableItem) -> void: + if not _spawned_items.has(item): + return _spawned_items.erase(item) if GameState.has_method("set_chest_state"): if _spawned_items.is_empty(): @@ -63,6 +65,12 @@ func get_item_config_count() -> int: return _item_configs.size() +func get_spawned_item(index: int) -> HoldableItem: + if index < 0 or index >= _spawned_items.size(): + return null + return _spawned_items[index] + + func _create_item(config: ChestItemData) -> HoldableItem: var item: HoldableItem if config.item_type == ChestItemData.ItemType.OUTFIT: @@ -75,8 +83,8 @@ func _create_item(config: ChestItemData) -> HoldableItem: return item -func _get_spawned_ids() -> Array: - var ids: Array = [] +func _get_spawned_ids() -> Array[String]: + var ids: Array[String] = [] for item: HoldableItem in _spawned_items: ids.append(item.item_id) return ids diff --git a/test/unit/test_room_chest.gd b/test/unit/test_room_chest.gd index 69ca74a..107d3fc 100644 --- a/test/unit/test_room_chest.gd +++ b/test/unit/test_room_chest.gd @@ -80,6 +80,6 @@ func test_receive_item_decrements_spawned_count() -> void: chest.chest_id = "reception_desk" add_child_autofree(chest) chest.spawn_items() - var item: HoldableItem = chest._spawned_items[0] as HoldableItem + var item: HoldableItem = chest.get_spawned_item(0) chest.receive_item(item) assert_eq(chest.get_spawned_count(), 2)