feat: add equipment identification and item use
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
extends GutTest
|
||||
|
||||
const ItemDefinitionType = preload("res://scripts/domain/items/item_definition.gd")
|
||||
const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
|
||||
const InventoryStateType = preload("res://scripts/domain/inventory/inventory_state.gd")
|
||||
const EquipmentStateType = preload("res://scripts/domain/inventory/equipment_state.gd")
|
||||
const ItemIdentificationType = preload("res://scripts/domain/items/item_identification.gd")
|
||||
const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
|
||||
|
||||
func _item(data: Dictionary, instance_id: StringName) -> ItemInstance:
|
||||
return ItemInstanceType.new(ItemDefinitionType.new(data), 1, instance_id)
|
||||
|
||||
func test_equipping_uses_declared_slot_recomputes_player_modifiers_and_swaps_previous_item() -> void:
|
||||
var inventory := InventoryStateType.new()
|
||||
var equipment := EquipmentStateType.new()
|
||||
var identification := ItemIdentificationType.new()
|
||||
var player := PlayerStateType.new({"attributes": {"str_": 5, "end": 5}, "skills": {"lockpicking": 2}})
|
||||
var gloves := _item({"id": "gloves", "name": "Gloves", "category": "armor", "equip_slot": "arms", "attr_mods": {"str_": 2}, "skill_mods": {"lockpicking": 3}}, &"gloves")
|
||||
var better_gloves := _item({"id": "better_gloves", "name": "Better Gloves", "category": "armor", "equip_slot": "arms", "attr_mods": {"end": 1}}, &"better-gloves")
|
||||
inventory.add_item(gloves)
|
||||
inventory.add_item(better_gloves)
|
||||
|
||||
var first := equipment.equip(inventory, gloves.instance_id, player, identification)
|
||||
var swapped := equipment.equip(inventory, better_gloves.instance_id, player, identification)
|
||||
|
||||
assert_true(first.accepted)
|
||||
assert_eq(equipment.item_in_slot(&"arms").instance_id, better_gloves.instance_id)
|
||||
assert_eq(player.effective_attribute(&"str_"), 5)
|
||||
assert_eq(player.effective_attribute(&"end"), 6)
|
||||
assert_eq(player.effective_skill(&"lockpicking"), 2)
|
||||
assert_eq(inventory.item_by_id(gloves.instance_id), gloves)
|
||||
assert_true(swapped.accepted)
|
||||
assert_eq(swapped.displaced.instance_id, gloves.instance_id)
|
||||
|
||||
func test_equipment_rejects_items_without_a_supported_slot_without_mutating_inventory_or_stats() -> void:
|
||||
var inventory := InventoryStateType.new()
|
||||
var equipment := EquipmentStateType.new()
|
||||
var player := PlayerStateType.new({"attributes": {"agi": 6}})
|
||||
var trinket := _item({"id": "trinket", "name": "Trinket", "category": "misc", "attr_mods": {"agi": 4}}, &"trinket")
|
||||
inventory.add_item(trinket)
|
||||
|
||||
var result := equipment.equip(inventory, trinket.instance_id, player)
|
||||
|
||||
assert_false(result.accepted)
|
||||
assert_eq(result.reason, &"not_equippable")
|
||||
assert_eq(inventory.item_by_id(trinket.instance_id), trinket)
|
||||
assert_eq(player.effective_attribute(&"agi"), 6)
|
||||
@@ -0,0 +1 @@
|
||||
uid://duvijs5q08uuh
|
||||
@@ -0,0 +1,83 @@
|
||||
extends GutTest
|
||||
|
||||
const ItemDefinitionType = preload("res://scripts/domain/items/item_definition.gd")
|
||||
const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
|
||||
const InventoryStateType = preload("res://scripts/domain/inventory/inventory_state.gd")
|
||||
const ItemIdentificationType = preload("res://scripts/domain/items/item_identification.gd")
|
||||
const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
|
||||
|
||||
func _item(data: Dictionary, instance_id: StringName, quantity: int = 1) -> ItemInstance:
|
||||
return ItemInstanceType.new(ItemDefinitionType.new(data), quantity, instance_id)
|
||||
|
||||
func test_unidentified_items_hide_name_and_stats_until_identified_or_equipped() -> void:
|
||||
var identification := ItemIdentificationType.new()
|
||||
var mysterious_blade := _item({"id": "mysterious_blade", "name": "Sun Blade", "category": "weapon", "equip_slot": "main_hand", "damage": 9, "unidentified": true}, &"mystery")
|
||||
|
||||
assert_false(identification.is_identified(mysterious_blade))
|
||||
assert_eq(identification.display_name(mysterious_blade), "Unidentified weapon")
|
||||
assert_eq(identification.display_stats(mysterious_blade), "Unknown properties")
|
||||
identification.reveal(mysterious_blade)
|
||||
assert_true(identification.is_identified(mysterious_blade))
|
||||
assert_eq(identification.display_name(mysterious_blade), "Sun Blade")
|
||||
assert_string_contains(identification.display_stats(mysterious_blade), "Damage +9")
|
||||
|
||||
func test_using_consumable_auto_reveals_applies_effects_and_consumes_one_from_stack() -> void:
|
||||
var inventory := InventoryStateType.new()
|
||||
var identification := ItemIdentificationType.new()
|
||||
var player := PlayerStateType.new({"hit_points": 10})
|
||||
var medkit := _item({"id": "mystery_medkit", "name": "Mystery Medkit", "category": "consumable", "consumable": true, "stackable": true, "unidentified": true, "on_use": [{"type": "heal", "amount": 20}]}, &"medkit", 2)
|
||||
inventory.add_item(medkit)
|
||||
|
||||
var result := inventory.use_item(medkit.instance_id, player, identification)
|
||||
|
||||
assert_true(result.accepted)
|
||||
assert_true(result.revealed)
|
||||
assert_true(identification.is_identified(medkit))
|
||||
assert_eq(player.hit_points, 30)
|
||||
assert_eq(inventory.item_by_id(medkit.instance_id).quantity, 1)
|
||||
assert_eq(result.effects, [{"type": &"heal", "amount": 20}])
|
||||
|
||||
func test_using_an_actor_state_uses_its_maximum_hit_points_contract() -> void:
|
||||
var inventory := InventoryStateType.new()
|
||||
var identification := ItemIdentificationType.new()
|
||||
var actor := ActorState.new(&"player", Vector2i.ZERO, 12, 3)
|
||||
actor.hit_points = 10
|
||||
actor.max_hit_points = 50
|
||||
var medkit := _item({"id": "medkit", "name": "Medkit", "category": "consumable", "consumable": true, "on_use": [{"type": "heal", "amount": 40}]}, &"actor-medkit")
|
||||
inventory.add_item(medkit)
|
||||
|
||||
var result := inventory.use_item(medkit.instance_id, actor, identification)
|
||||
|
||||
assert_true(result.accepted)
|
||||
assert_eq(actor.hit_points, 50)
|
||||
assert_eq(inventory.item_by_id(medkit.instance_id), null)
|
||||
|
||||
func test_dropping_one_stack_unit_places_it_on_floor_and_keeps_the_remainder() -> void:
|
||||
var inventory := InventoryStateType.new()
|
||||
var floor := FloorState.new(GridMapState.new(Vector2i(3, 3)))
|
||||
var bandage := _item({"id": "bandage", "name": "Bandage", "category": "consumable", "consumable": true, "stackable": true}, &"bandage", 2)
|
||||
inventory.add_item(bandage)
|
||||
|
||||
var result := inventory.drop_item(floor, Vector2i(1, 1), bandage.instance_id)
|
||||
|
||||
assert_true(result.accepted)
|
||||
assert_eq(inventory.item_by_id(bandage.instance_id).quantity, 1)
|
||||
assert_eq(floor.ground_items[Vector2i(1, 1)][0].definition.id, &"bandage")
|
||||
assert_eq(floor.ground_items[Vector2i(1, 1)][0].quantity, 1)
|
||||
|
||||
func test_ally_target_hook_requires_a_target_and_can_apply_to_the_selected_ally() -> void:
|
||||
var inventory := InventoryStateType.new()
|
||||
var identification := ItemIdentificationType.new()
|
||||
var player := PlayerStateType.new({"hit_points": 10})
|
||||
var ally := PlayerStateType.new({"id": "ally", "hit_points": 5})
|
||||
var medkit := _item({"id": "ally_medkit", "name": "Ally Medkit", "category": "consumable", "consumable": true, "target": "ally", "on_use": [{"type": "heal", "amount": 10}]}, &"ally-medkit")
|
||||
inventory.add_item(medkit)
|
||||
|
||||
var pending := inventory.use_item(medkit.instance_id, player, identification)
|
||||
var applied := inventory.use_item(medkit.instance_id, player, identification, ally)
|
||||
|
||||
assert_false(pending.accepted)
|
||||
assert_eq(pending.reason, &"target_required")
|
||||
assert_true(applied.accepted)
|
||||
assert_eq(ally.hit_points, 15)
|
||||
assert_eq(inventory.item_by_id(medkit.instance_id), null)
|
||||
@@ -0,0 +1 @@
|
||||
uid://beghfr5nncngj
|
||||
Reference in New Issue
Block a user