Make decor occupy individual inventory slots
This commit is contained in:
parent
f2239d248a
commit
4196cda1d5
9 changed files with 516 additions and 79 deletions
|
|
@ -4,6 +4,7 @@ extends Node
|
|||
const ItemCatalogType = preload("res://items/item_catalog.gd")
|
||||
const ItemDataType = preload("res://items/item_data.gd")
|
||||
const OwnedItemType = preload("res://items/owned_item.gd")
|
||||
const DecorCatalogType = preload("res://homes/decor_catalog.gd")
|
||||
|
||||
const DEFAULT_UNLOCKED_BAIT_IDS: Array[StringName] = [&"worms"]
|
||||
const MIN_STORAGE_SLOT_COUNT: int = 15
|
||||
|
|
@ -62,9 +63,35 @@ func add_item(item_id: StringName, quantity: int = 1) -> bool:
|
|||
|
||||
func add_item_to_storage(item_id: StringName, quantity: int = 1) -> bool:
|
||||
var existing: OwnedItemType = get_owned_item(item_id)
|
||||
var item: ItemDataType = _resolve_available_item(item_id)
|
||||
if item != null and DecorCatalogType.has_product(item_id):
|
||||
if (
|
||||
not _can_own_quantity(item, existing, quantity)
|
||||
or _inventory_layout == null
|
||||
or not _inventory_layout.can_accept_item_in_storage(
|
||||
item_id, quantity
|
||||
)
|
||||
):
|
||||
return false
|
||||
for _unit: int in quantity:
|
||||
if not _inventory_layout.prepare_new_item_in_first_free(
|
||||
item_id,
|
||||
PlayerInventoryLayout.InventoryContainer.STORAGE,
|
||||
):
|
||||
_inventory_layout.cancel_prepared_item_placement(item_id)
|
||||
return false
|
||||
if existing != null:
|
||||
existing.quantity += quantity
|
||||
else:
|
||||
var owned := OwnedItemType.new()
|
||||
owned.item_id = item_id
|
||||
owned.quantity = quantity
|
||||
owned.storage_slot = _first_available_storage_slot(item, _items)
|
||||
_items.append(owned)
|
||||
contents_changed.emit()
|
||||
return true
|
||||
if existing != null:
|
||||
return add_item(item_id, quantity)
|
||||
var item: ItemDataType = _resolve_available_item(item_id)
|
||||
if (
|
||||
item == null
|
||||
or quantity <= 0
|
||||
|
|
@ -87,6 +114,14 @@ func can_add_item(item_id: StringName, quantity: int = 1) -> bool:
|
|||
if item == null or quantity <= 0:
|
||||
return false
|
||||
var existing: OwnedItemType = get_owned_item(item_id)
|
||||
if DecorCatalogType.has_product(item_id):
|
||||
return (
|
||||
_can_own_quantity(item, existing, quantity)
|
||||
and (
|
||||
_inventory_layout == null
|
||||
or _inventory_layout.can_accept_item(item_id, quantity)
|
||||
)
|
||||
)
|
||||
if existing != null:
|
||||
return (
|
||||
item.stackable
|
||||
|
|
@ -226,7 +261,7 @@ func replace_all_items(items: Array[OwnedItemType]) -> bool:
|
|||
var item: ItemDataType = _resolve_valid_item(owned.item_id)
|
||||
if item == null:
|
||||
return false
|
||||
var maximum: int = item.max_stack if item.stackable else 1
|
||||
var maximum: int = _maximum_owned_quantity(item)
|
||||
if owned.quantity > maximum:
|
||||
return false
|
||||
seen[owned.item_id] = true
|
||||
|
|
@ -319,3 +354,22 @@ func _resolve_valid_item(item_id: StringName) -> ItemDataType:
|
|||
func _resolve_available_item(item_id: StringName) -> ItemDataType:
|
||||
var item: ItemDataType = _resolve_valid_item(item_id)
|
||||
return item if item != null and item.active else null
|
||||
|
||||
|
||||
func _can_own_quantity(
|
||||
item: ItemDataType,
|
||||
existing: OwnedItemType,
|
||||
quantity: int,
|
||||
) -> bool:
|
||||
if item == null or quantity <= 0:
|
||||
return false
|
||||
var current: int = existing.quantity if existing != null else 0
|
||||
return current + quantity <= _maximum_owned_quantity(item)
|
||||
|
||||
|
||||
func _maximum_owned_quantity(item: ItemDataType) -> int:
|
||||
if item == null:
|
||||
return 0
|
||||
if DecorCatalogType.has_product(item.item_id):
|
||||
return mini(item.max_stack, DecorCatalogType.MAX_OWNED_PER_PRODUCT)
|
||||
return item.max_stack if item.stackable else 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue