145 lines
5.7 KiB
GDScript
145 lines
5.7 KiB
GDScript
class_name InventoryState
|
|
extends RefCounted
|
|
|
|
const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
|
|
|
|
var capacity: int
|
|
var items: Array[ItemInstance] = []
|
|
|
|
func _init(new_capacity: int = 20) -> void:
|
|
capacity = maxi(0, new_capacity)
|
|
|
|
func add_item(item: ItemInstance) -> Dictionary:
|
|
if item == null or item.definition == null or not item.definition.is_valid():
|
|
return _result(false, &"invalid_item", item)
|
|
if item.definition.stackable:
|
|
var existing := _stack_for(item.definition.id)
|
|
if existing != null:
|
|
existing.quantity += item.quantity
|
|
return _result(true, &"", item, existing.instance_id)
|
|
if items.size() >= capacity:
|
|
return _result(false, &"capacity", item)
|
|
items.append(item)
|
|
return _result(true, &"", item)
|
|
|
|
func item_by_id(instance_id: StringName) -> ItemInstance:
|
|
for item in items:
|
|
if item.instance_id == instance_id:
|
|
return item
|
|
return null
|
|
|
|
func remove_item(instance_id: StringName, quantity: int = 1) -> Dictionary:
|
|
var item := item_by_id(instance_id)
|
|
if item == null:
|
|
return _result(false, &"missing_item")
|
|
var removed_quantity := clampi(quantity, 1, item.quantity)
|
|
var removed := ItemInstanceType.new(item.definition, removed_quantity, item.instance_id if removed_quantity == item.quantity else &"")
|
|
item.quantity -= removed_quantity
|
|
if item.quantity <= 0:
|
|
items.erase(item)
|
|
return _result(true, &"", removed)
|
|
|
|
func drop_item(floor: FloorState, cell: Vector2i, instance_id: StringName, quantity: int = 1) -> Dictionary:
|
|
if floor == null:
|
|
return _result(false, &"invalid_floor")
|
|
var removed := remove_item(instance_id, quantity)
|
|
if not bool(removed.accepted):
|
|
return removed
|
|
var dropped: ItemInstance = removed.item
|
|
if not floor.ground_items.has(cell):
|
|
floor.ground_items[cell] = []
|
|
floor.ground_items[cell].append(dropped)
|
|
return _result(true, &"", dropped)
|
|
|
|
func use_item(instance_id: StringName, default_target, identification: ItemIdentification, target = null) -> Dictionary:
|
|
var item := item_by_id(instance_id)
|
|
if item == null:
|
|
return _result(false, &"missing_item")
|
|
if not bool(item.definition.raw.get("consumable", false)):
|
|
return _result(false, &"not_consumable", item)
|
|
var requires_ally := StringName(item.definition.raw.get("target", "")) == &"ally"
|
|
if requires_ally and target == null:
|
|
return _result(false, &"target_required", item)
|
|
var recipient: Variant = target if target != null else default_target
|
|
if not _is_valid_recipient(recipient):
|
|
return _result(false, &"invalid_target", item)
|
|
var revealed := identification.reveal(item) if identification != null else false
|
|
var effects: Array[Dictionary] = []
|
|
for effect_data: Dictionary in item.definition.raw.get("on_use", []):
|
|
var effect_type := StringName(effect_data.get("type", ""))
|
|
match effect_type:
|
|
&"heal":
|
|
var amount := maxi(0, int(effect_data.get("amount", 0)))
|
|
recipient.hit_points = mini(_maximum_hit_points(recipient), recipient.hit_points + amount)
|
|
effects.append({"type": effect_type, "amount": amount})
|
|
&"heal_pct":
|
|
var percentage := maxi(0, int(effect_data.get("amount", effect_data.get("percent", 0))))
|
|
var healing := ceili(float(_maximum_hit_points(recipient)) * percentage / 100.0)
|
|
recipient.hit_points = mini(_maximum_hit_points(recipient), recipient.hit_points + healing)
|
|
effects.append({"type": effect_type, "amount": healing})
|
|
&"cure_status":
|
|
var status := StringName(effect_data.get("status", ""))
|
|
recipient.clear_status(status)
|
|
effects.append({"type": effect_type, "status": status})
|
|
&"apply_status":
|
|
var status := StringName(effect_data.get("status", ""))
|
|
recipient.apply_status(StatusEffect.new(status, int(effect_data.get("duration", -1)), int(effect_data.get("magnitude", 0)), item.definition.id))
|
|
effects.append({"type": effect_type, "status": status})
|
|
_:
|
|
effects.append({"type": effect_type})
|
|
var consumed := remove_item(instance_id)
|
|
if not bool(consumed.accepted):
|
|
return consumed
|
|
var result := _result(true, &"", consumed.item)
|
|
result["revealed"] = revealed
|
|
result["effects"] = effects
|
|
return result
|
|
|
|
func _is_valid_recipient(recipient) -> bool:
|
|
return recipient != null and recipient is Object and is_instance_valid(recipient) and "hit_points" in recipient and (recipient.has_method("max_hp") or "max_hit_points" in recipient)
|
|
|
|
func _maximum_hit_points(recipient) -> int:
|
|
if recipient.has_method("max_hp"):
|
|
return recipient.max_hp()
|
|
return int(recipient.max_hit_points)
|
|
|
|
func pick_up_ground_item(floor: FloorState, cell: Vector2i) -> Dictionary:
|
|
if floor == null or not floor.ground_items.has(cell) or floor.ground_items[cell].is_empty():
|
|
return _result(false, &"no_ground_item")
|
|
var item: ItemInstance = floor.ground_items[cell][0]
|
|
var result := add_item(item)
|
|
if not bool(result.accepted):
|
|
return result
|
|
floor.ground_items[cell].remove_at(0)
|
|
if floor.ground_items[cell].is_empty():
|
|
floor.ground_items.erase(cell)
|
|
return result
|
|
|
|
func to_dict() -> Dictionary:
|
|
var serialized: Array[Dictionary] = []
|
|
for item in items:
|
|
serialized.append(item.to_dict())
|
|
return {"capacity": capacity, "items": serialized}
|
|
|
|
static func from_dict(data: Dictionary, catalog: ContentCatalog) -> InventoryState:
|
|
var inventory := InventoryState.new(int(data.get("capacity", 20)))
|
|
for item_data: Dictionary in data.get("items", []):
|
|
var item := ItemInstanceType.from_dict(item_data, catalog)
|
|
if item.definition.is_valid():
|
|
inventory.add_item(item)
|
|
return inventory
|
|
|
|
func _stack_for(item_id: StringName) -> ItemInstance:
|
|
for item in items:
|
|
if item.definition.id == item_id:
|
|
return item
|
|
return null
|
|
|
|
func _result(accepted: bool, reason: StringName = &"", item: ItemInstance = null, stack_merged_into: StringName = &"") -> Dictionary:
|
|
return {
|
|
"accepted": accepted,
|
|
"reason": reason,
|
|
"item": item,
|
|
"stack_merged_into": stack_merged_into,
|
|
}
|