Add equipment unequip flow
This commit is contained in:
@@ -1681,7 +1681,7 @@ func try_equip_item(unit_id: String, item_id: String) -> bool:
|
||||
return false
|
||||
|
||||
var equipment: Dictionary = unit.get("equipment", {}).duplicate(true)
|
||||
var old_item_id := str(equipment.get(slot, ""))
|
||||
var old_item_id := _equipment_item_id_for_slot(equipment, slot)
|
||||
if old_item_id == item_id:
|
||||
_emit_log("%s already has %s equipped." % [unit["name"], item.get("name", item_id)])
|
||||
return false
|
||||
@@ -1700,6 +1700,43 @@ func try_equip_item(unit_id: String, item_id: String) -> bool:
|
||||
return true
|
||||
|
||||
|
||||
func try_unequip_item(unit_id: String, slot: String) -> bool:
|
||||
var unit := get_unit(unit_id)
|
||||
if unit.is_empty() or battle_status != STATUS_ACTIVE:
|
||||
return false
|
||||
if unit.get("team", "") != TEAM_PLAYER or current_team != TEAM_PLAYER:
|
||||
return false
|
||||
if not bool(unit.get("controllable", true)):
|
||||
return false
|
||||
if unit.get("moved", false) or unit.get("acted", false):
|
||||
_emit_log("%s can change equipment before moving or acting." % unit["name"])
|
||||
return false
|
||||
|
||||
var normalized_slot := _normalized_equipment_slot(slot)
|
||||
if normalized_slot.is_empty():
|
||||
return false
|
||||
|
||||
var equipment: Dictionary = unit.get("equipment", {}).duplicate(true)
|
||||
var old_item_id := _equipment_item_id_for_slot(equipment, normalized_slot)
|
||||
if old_item_id.is_empty():
|
||||
_emit_log("%s has no %s equipped." % [unit["name"], normalized_slot])
|
||||
return false
|
||||
|
||||
var old_item := get_item_def(old_item_id)
|
||||
if old_item.is_empty():
|
||||
push_error("Cannot unequip unknown item %s from %s." % [old_item_id, unit["name"]])
|
||||
return false
|
||||
|
||||
equipment.erase(normalized_slot)
|
||||
unit["equipment"] = equipment
|
||||
_apply_equipment_stat_delta(unit, old_item, {})
|
||||
_refresh_attack_range_from_equipment(unit)
|
||||
battle_inventory[old_item_id] = int(battle_inventory.get(old_item_id, 0)) + 1
|
||||
_emit_log("%s unequips %s." % [unit["name"], old_item.get("name", old_item_id)])
|
||||
_notify_changed()
|
||||
return true
|
||||
|
||||
|
||||
func get_equippable_item_ids(unit_id: String) -> Array:
|
||||
var result := []
|
||||
var unit := get_unit(unit_id)
|
||||
@@ -1718,7 +1755,7 @@ func get_equippable_item_ids(unit_id: String) -> Array:
|
||||
if item.is_empty() or _equipment_slot_for_item(item).is_empty():
|
||||
continue
|
||||
var slot := _equipment_slot_for_item(item)
|
||||
if str(equipment.get(slot, "")) == normalized:
|
||||
if _equipment_item_id_for_slot(equipment, slot) == normalized:
|
||||
continue
|
||||
if not _unit_can_equip_item(unit, item):
|
||||
continue
|
||||
@@ -1727,6 +1764,24 @@ func get_equippable_item_ids(unit_id: String) -> Array:
|
||||
return result
|
||||
|
||||
|
||||
func get_equipped_equipment_slots(unit_id: String) -> Array:
|
||||
var result := []
|
||||
var unit := get_unit(unit_id)
|
||||
if unit.is_empty() or unit.get("team", "") != TEAM_PLAYER:
|
||||
return result
|
||||
if not bool(unit.get("controllable", true)):
|
||||
return result
|
||||
var equipment: Dictionary = unit.get("equipment", {})
|
||||
for slot in ["weapon", "armor", "accessory"]:
|
||||
var item_id := _equipment_item_id_for_slot(equipment, slot)
|
||||
if item_id.is_empty():
|
||||
continue
|
||||
if get_item_def(item_id).is_empty():
|
||||
continue
|
||||
result.append(slot)
|
||||
return result
|
||||
|
||||
|
||||
func get_equipment_snapshot(unit_id: String) -> Dictionary:
|
||||
var unit := get_unit(unit_id)
|
||||
if unit.is_empty():
|
||||
@@ -2420,6 +2475,20 @@ func _equipment_slot_for_item(item: Dictionary) -> String:
|
||||
return ""
|
||||
|
||||
|
||||
func _normalized_equipment_slot(slot: String) -> String:
|
||||
var normalized := slot.strip_edges().to_lower()
|
||||
if normalized == "weapon" or normalized == "armor" or normalized == "accessory":
|
||||
return normalized
|
||||
return ""
|
||||
|
||||
|
||||
func _equipment_item_id_for_slot(equipment: Dictionary, slot: String) -> String:
|
||||
var item_id = equipment.get(slot, "")
|
||||
if item_id == null:
|
||||
return ""
|
||||
return str(item_id)
|
||||
|
||||
|
||||
func _unit_can_equip_item(unit: Dictionary, item: Dictionary) -> bool:
|
||||
var slot := _equipment_slot_for_item(item)
|
||||
if slot.is_empty():
|
||||
|
||||
Reference in New Issue
Block a user