feat: add equipment identification and item use
This commit is contained in:
@@ -94,6 +94,9 @@ func apply_status(effect: StatusEffect) -> void:
|
||||
func has_status(key: StringName) -> bool:
|
||||
return status_effects.any(func(effect: StatusEffect) -> bool: return effect.key == key)
|
||||
|
||||
func clear_status(key: StringName) -> void:
|
||||
status_effects = status_effects.filter(func(effect: StatusEffect) -> bool: return effect.key != key)
|
||||
|
||||
func take_damage(amount: int) -> int:
|
||||
var actual := mini(maxi(0, amount), hit_points)
|
||||
hit_points -= actual
|
||||
|
||||
@@ -93,10 +93,13 @@ func as_actor_state(attack_damage: int) -> ActorState:
|
||||
actor.focus = focus
|
||||
return actor
|
||||
|
||||
func _normalize_resources() -> void:
|
||||
func normalize_resources() -> void:
|
||||
hit_points = clampi(hit_points, 0, max_hp())
|
||||
focus = clampi(focus, 0, max_focus())
|
||||
|
||||
func _normalize_resources() -> void:
|
||||
normalize_resources()
|
||||
|
||||
func _status_for(key: StringName) -> StatusEffect:
|
||||
for effect in status_effects:
|
||||
if effect.key == key:
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
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}
|
||||
@@ -0,0 +1 @@
|
||||
uid://c5745hl0ewaym
|
||||
@@ -22,6 +22,87 @@ func add_item(item: ItemInstance) -> Dictionary:
|
||||
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")
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
class_name ItemIdentification
|
||||
extends RefCounted
|
||||
|
||||
var _identified_instance_ids: Dictionary[StringName, bool] = {}
|
||||
|
||||
func is_identified(item: ItemInstance) -> bool:
|
||||
if item == null or item.definition == null:
|
||||
return false
|
||||
return not bool(item.definition.raw.get("unidentified", false)) or _identified_instance_ids.has(item.instance_id)
|
||||
|
||||
func reveal(item: ItemInstance) -> bool:
|
||||
if item == null or item.definition == null:
|
||||
return false
|
||||
var was_identified := is_identified(item)
|
||||
_identified_instance_ids[item.instance_id] = true
|
||||
return not was_identified
|
||||
|
||||
func display_name(item: ItemInstance) -> String:
|
||||
if item == null or item.definition == null:
|
||||
return "Unknown item"
|
||||
if is_identified(item):
|
||||
return item.definition.name
|
||||
return "Unidentified %s" % _display_category(item.definition.category)
|
||||
|
||||
func display_stats(item: ItemInstance) -> String:
|
||||
if item == null or item.definition == null:
|
||||
return "Unknown properties"
|
||||
if not is_identified(item):
|
||||
return "Unknown properties"
|
||||
var raw := item.definition.raw
|
||||
var lines: PackedStringArray = []
|
||||
if raw.has("damage"):
|
||||
lines.append("Damage %+d" % int(raw.damage))
|
||||
if raw.has("armor"):
|
||||
lines.append("Armor %+d" % int(raw.armor))
|
||||
for key: Variant in raw.get("attr_mods", {}):
|
||||
lines.append("%s %+d" % [String(key), int(raw.attr_mods[key])])
|
||||
for key: Variant in raw.get("skill_mods", {}):
|
||||
lines.append("%s %+d" % [String(key), int(raw.skill_mods[key])])
|
||||
return ", ".join(lines) if not lines.is_empty() else "No combat modifiers"
|
||||
|
||||
func _display_category(category: StringName) -> String:
|
||||
match category:
|
||||
&"weapon": return "weapon"
|
||||
&"armor": return "armor"
|
||||
&"consumable": return "consumable"
|
||||
_: return "item"
|
||||
@@ -0,0 +1 @@
|
||||
uid://dls0hme85crpo
|
||||
@@ -16,6 +16,9 @@ var log: Array[GameEvent] = []
|
||||
var gold := 0
|
||||
# D1 owns inventory at run scope so it persists while the player changes floors.
|
||||
var inventory: InventoryState = InventoryState.new()
|
||||
# D3 keeps item identity and equipment with the run so swaps persist across floors.
|
||||
var equipment: EquipmentState = EquipmentState.new()
|
||||
var identification: ItemIdentification = ItemIdentification.new()
|
||||
|
||||
func _init(new_seed: String, new_settings: RunSettings = null) -> void:
|
||||
settings = new_settings if new_settings != null else RunSettings.new(new_seed)
|
||||
|
||||
Reference in New Issue
Block a user