108 lines
4.9 KiB
GDScript
108 lines
4.9 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] = {}
|
|
var combat: Dictionary[StringName, Variant] = {}
|
|
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])
|
|
_accumulate_combat_modifiers(item, combat)
|
|
if player is PlayerState:
|
|
player.equipment_attribute_modifiers = attributes
|
|
player.equipment_skill_modifiers = skills
|
|
player.normalize_resources()
|
|
elif player is ActorState:
|
|
player.recompute_equipment_modifiers(attributes, skills, combat)
|
|
var main_hand := item_in_slot(&"main_hand")
|
|
if main_hand != null and main_hand.definition != null and StringName(main_hand.definition.raw.get("weapon_kind", "")) != &"ranged":
|
|
player.metadata["melee_weapon_name"] = main_hand.definition.name
|
|
else:
|
|
player.metadata.erase("melee_weapon_name")
|
|
|
|
func _accumulate_combat_modifiers(item: ItemInstance, combat: Dictionary[StringName, Variant]) -> void:
|
|
var raw := item.definition.raw
|
|
if raw.has("armor"):
|
|
combat[&"armor"] = combat.get(&"armor", 0) + int(raw.armor)
|
|
# RangedCombat applies its active weapon immediately around the attack. Its
|
|
# weapon stats must therefore not also become permanent ActorState modifiers.
|
|
if StringName(raw.get("weapon_kind", "")) == &"ranged":
|
|
return
|
|
for stat: StringName in [&"damage", &"damage_variance", &"accuracy", &"penetration"]:
|
|
if raw.has(stat):
|
|
var target_stat: StringName = &"weapon_damage" if stat == &"damage" else stat
|
|
combat[target_stat] = combat.get(target_stat, 0) + int(raw[stat])
|
|
if raw.has("crit_mult"):
|
|
combat[&"crit_mult"] = combat.get(&"crit_mult", 0.0) + (float(raw.crit_mult) - 1.5)
|
|
|
|
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}
|