84 lines
3.6 KiB
GDScript
84 lines
3.6 KiB
GDScript
class_name EquipmentState
|
|
extends RefCounted
|
|
|
|
const AttributesType = preload("res://scripts/domain/entities/attributes.gd")
|
|
|
|
const SLOTS: Array[StringName] = [&"main_hand", &"off_hand", &"head", &"torso", &"arms", &"legs", &"accessory1", &"accessory2"]
|
|
|
|
var equipped: Dictionary[StringName, ItemInstance] = {}
|
|
|
|
func item_in_slot(slot: StringName) -> ItemInstance:
|
|
return equipped.get(slot)
|
|
|
|
func equip(inventory: InventoryState, instance_id: StringName, player, identification: ItemIdentification = null) -> Dictionary:
|
|
if inventory == null or player == null:
|
|
return _result(false, &"invalid_context")
|
|
var item := inventory.item_by_id(instance_id)
|
|
if item == null or item.definition == null:
|
|
return _result(false, &"missing_item")
|
|
var slot := StringName(item.definition.raw.get("equip_slot", ""))
|
|
if not SLOTS.has(slot):
|
|
return _result(false, &"not_equippable", item)
|
|
var removed := inventory.remove_item(instance_id)
|
|
if not bool(removed.accepted):
|
|
return removed
|
|
var displaced := item_in_slot(slot)
|
|
equipped[slot] = item
|
|
if displaced != null:
|
|
var restored := inventory.add_item(displaced)
|
|
if not bool(restored.accepted):
|
|
equipped[slot] = displaced
|
|
inventory.add_item(item)
|
|
return _result(false, &"inventory_full", item, displaced)
|
|
if identification != null:
|
|
identification.reveal(item)
|
|
recompute_player(player)
|
|
return _result(true, &"", item, displaced, slot)
|
|
|
|
func unequip(slot: StringName, inventory: InventoryState, player) -> Dictionary:
|
|
var item := item_in_slot(slot)
|
|
if item == null:
|
|
return _result(false, &"empty_slot")
|
|
var restored := inventory.add_item(item)
|
|
if not bool(restored.accepted):
|
|
return _result(false, restored.reason, item)
|
|
equipped.erase(slot)
|
|
recompute_player(player)
|
|
return _result(true, &"", item, null, slot)
|
|
|
|
func recompute_player(player) -> void:
|
|
if player == null:
|
|
return
|
|
var attributes: Dictionary[StringName, int] = {}
|
|
var skills: Dictionary[StringName, int] = {}
|
|
for slot in equipped:
|
|
var item: ItemInstance = equipped[slot]
|
|
if item == null or item.definition == null:
|
|
continue
|
|
for key: Variant in item.definition.raw.get("attr_mods", {}):
|
|
var attribute := StringName(key)
|
|
if AttributesType.is_attribute(attribute):
|
|
attributes[attribute] = attributes.get(attribute, 0) + int(item.definition.raw.attr_mods[key])
|
|
for key: Variant in item.definition.raw.get("skill_mods", {}):
|
|
var skill := StringName(key)
|
|
if AttributesType.is_skill(skill):
|
|
skills[skill] = skills.get(skill, 0) + int(item.definition.raw.skill_mods[key])
|
|
if player is PlayerState:
|
|
player.equipment_attribute_modifiers = attributes
|
|
player.equipment_skill_modifiers = skills
|
|
player.normalize_resources()
|
|
|
|
func comparison_for(item: ItemInstance, identification: ItemIdentification = null) -> String:
|
|
if item == null or item.definition == null:
|
|
return ""
|
|
var slot := StringName(item.definition.raw.get("equip_slot", ""))
|
|
var equipped_item := item_in_slot(slot)
|
|
var candidate_stats := identification.display_stats(item) if identification != null else ""
|
|
if equipped_item == null:
|
|
return "Slot %s is empty. %s" % [slot, candidate_stats]
|
|
var equipped_stats := identification.display_stats(equipped_item) if identification != null else ""
|
|
return "Equipped: %s (%s)\nCandidate: %s" % [identification.display_name(equipped_item) if identification != null else equipped_item.definition.name, equipped_stats, candidate_stats]
|
|
|
|
func _result(accepted: bool, reason: StringName = &"", item: ItemInstance = null, displaced: ItemInstance = null, slot: StringName = &"") -> Dictionary:
|
|
return {"accepted": accepted, "reason": reason, "item": item, "displaced": displaced, "slot": slot}
|