refactor(items): strengthen RoomChest types, guard receive_item, expose get_spawned_item
This commit is contained in:
@@ -9,7 +9,7 @@ const SPAWN_TWEEN_DURATION: float = 0.3
|
|||||||
|
|
||||||
@export var chest_id: String = ""
|
@export var chest_id: String = ""
|
||||||
|
|
||||||
var _spawned_items: Array = []
|
var _spawned_items: Array[HoldableItem] = []
|
||||||
var _item_configs: Array[ChestItemData] = []
|
var _item_configs: Array[ChestItemData] = []
|
||||||
|
|
||||||
|
|
||||||
@@ -41,6 +41,8 @@ func spawn_items() -> void:
|
|||||||
|
|
||||||
|
|
||||||
func receive_item(item: HoldableItem) -> void:
|
func receive_item(item: HoldableItem) -> void:
|
||||||
|
if not _spawned_items.has(item):
|
||||||
|
return
|
||||||
_spawned_items.erase(item)
|
_spawned_items.erase(item)
|
||||||
if GameState.has_method("set_chest_state"):
|
if GameState.has_method("set_chest_state"):
|
||||||
if _spawned_items.is_empty():
|
if _spawned_items.is_empty():
|
||||||
@@ -63,6 +65,12 @@ func get_item_config_count() -> int:
|
|||||||
return _item_configs.size()
|
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:
|
func _create_item(config: ChestItemData) -> HoldableItem:
|
||||||
var item: HoldableItem
|
var item: HoldableItem
|
||||||
if config.item_type == ChestItemData.ItemType.OUTFIT:
|
if config.item_type == ChestItemData.ItemType.OUTFIT:
|
||||||
@@ -75,8 +83,8 @@ func _create_item(config: ChestItemData) -> HoldableItem:
|
|||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
func _get_spawned_ids() -> Array:
|
func _get_spawned_ids() -> Array[String]:
|
||||||
var ids: Array = []
|
var ids: Array[String] = []
|
||||||
for item: HoldableItem in _spawned_items:
|
for item: HoldableItem in _spawned_items:
|
||||||
ids.append(item.item_id)
|
ids.append(item.item_id)
|
||||||
return ids
|
return ids
|
||||||
|
|||||||
@@ -80,6 +80,6 @@ func test_receive_item_decrements_spawned_count() -> void:
|
|||||||
chest.chest_id = "reception_desk"
|
chest.chest_id = "reception_desk"
|
||||||
add_child_autofree(chest)
|
add_child_autofree(chest)
|
||||||
chest.spawn_items()
|
chest.spawn_items()
|
||||||
var item: HoldableItem = chest._spawned_items[0] as HoldableItem
|
var item: HoldableItem = chest.get_spawned_item(0)
|
||||||
chest.receive_item(item)
|
chest.receive_item(item)
|
||||||
assert_eq(chest.get_spawned_count(), 2)
|
assert_eq(chest.get_spawned_count(), 2)
|
||||||
|
|||||||
Reference in New Issue
Block a user