feat: port item instances and inventory
This commit is contained in:
@@ -57,7 +57,7 @@ func test_unmapped_input_returns_no_player_action() -> void:
|
||||
var router := ActionRouter.new(null)
|
||||
assert_null(router.action_from_input(_action_event(&"not_a_game_action")))
|
||||
|
||||
func test_submit_unimplemented_action_records_action_unavailable_event() -> void:
|
||||
func test_submit_inventory_action_opens_the_inventory_panel() -> void:
|
||||
var game := GameScene.instantiate() as Game
|
||||
add_child_autofree(game)
|
||||
await get_tree().process_frame
|
||||
@@ -66,10 +66,11 @@ func test_submit_unimplemented_action_records_action_unavailable_event() -> void
|
||||
var events: Array[GameEvent] = router.submit(PlayerAction.new(&"inventory"))
|
||||
|
||||
assert_eq(events.size(), 1)
|
||||
assert_eq(events[0].kind, &"action_unavailable")
|
||||
assert_eq(events[0].payload, {"action": &"inventory"})
|
||||
assert_eq(events[0].kind, &"inventory_opened")
|
||||
assert_eq(events[0].payload, {"item_count": 0})
|
||||
assert_eq(events[0].turn, 1)
|
||||
assert_eq(game.get_run_state().recent_events(1)[0], events[0])
|
||||
assert_true((game.get_node("HUD/InventoryPanel") as PanelContainer).visible)
|
||||
assert_eq(game.get_game_state().player.cell, Vector2i(4, 12))
|
||||
|
||||
func _action_event(input_action: StringName) -> InputEventAction:
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
extends GutTest
|
||||
|
||||
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
|
||||
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")
|
||||
|
||||
var catalog: ContentCatalog
|
||||
|
||||
func before_each() -> void:
|
||||
catalog = ContentCatalogType.load_from_directory("res://data/ascii")
|
||||
ItemInstanceType.reset_id_sequence_for_tests()
|
||||
|
||||
func _definition(item_id: StringName) -> ItemDefinition:
|
||||
return ItemDefinitionType.from_catalog(catalog, item_id)
|
||||
|
||||
func test_ascii_stackable_definition_merges_quantity_without_consuming_another_slot() -> void:
|
||||
var inventory := InventoryStateType.new(1)
|
||||
var bandage := _definition(&"consumable_bandage")
|
||||
|
||||
var first := ItemInstanceType.new(bandage, 2)
|
||||
var second := ItemInstanceType.new(bandage, 3)
|
||||
var first_result := inventory.add_item(first)
|
||||
var second_result := inventory.add_item(second)
|
||||
|
||||
assert_true(bandage.stackable)
|
||||
assert_true(first_result.accepted)
|
||||
assert_true(second_result.accepted)
|
||||
assert_eq(inventory.items.size(), 1)
|
||||
assert_eq(inventory.items[0].quantity, 5)
|
||||
assert_eq(inventory.items[0].instance_id, first.instance_id)
|
||||
assert_eq(second_result.stack_merged_into, first.instance_id)
|
||||
|
||||
func test_non_stackable_ascii_definition_needs_a_free_slot_and_refuses_when_full() -> void:
|
||||
var inventory := InventoryStateType.new(1)
|
||||
var sword := _definition(&"weapon_rusty_sword")
|
||||
|
||||
assert_false(sword.stackable)
|
||||
assert_true(inventory.add_item(ItemInstanceType.new(sword)).accepted)
|
||||
var refused := inventory.add_item(ItemInstanceType.new(sword))
|
||||
|
||||
assert_false(refused.accepted)
|
||||
assert_eq(refused.reason, &"capacity")
|
||||
assert_eq(inventory.items.size(), 1)
|
||||
|
||||
func test_instances_have_stable_unique_ids_and_round_trip_with_their_definition_and_quantity() -> void:
|
||||
var bandage := _definition(&"consumable_bandage")
|
||||
var generated := ItemInstanceType.new(bandage, 4)
|
||||
var explicit := ItemInstanceType.new(bandage, 1, &"loot-floor0-7")
|
||||
var restored := ItemInstanceType.from_dict(generated.to_dict(), catalog)
|
||||
|
||||
assert_ne(generated.instance_id, explicit.instance_id)
|
||||
assert_eq(explicit.instance_id, &"loot-floor0-7")
|
||||
assert_eq(restored.instance_id, generated.instance_id)
|
||||
assert_eq(restored.definition.id, &"consumable_bandage")
|
||||
assert_eq(restored.quantity, 4)
|
||||
|
||||
func test_restored_generated_id_reserves_its_sequence_for_the_next_new_instance() -> void:
|
||||
var bandage := _definition(&"consumable_bandage")
|
||||
var restored := ItemInstanceType.from_dict({"instance_id": "item-00000001", "item_id": "consumable_bandage", "quantity": 1}, catalog)
|
||||
var later := ItemInstanceType.new(bandage)
|
||||
|
||||
assert_eq(restored.instance_id, &"item-00000001")
|
||||
assert_ne(later.instance_id, restored.instance_id)
|
||||
|
||||
func test_ground_pickup_transfers_item_then_leaves_it_when_inventory_is_at_capacity() -> void:
|
||||
var inventory := InventoryStateType.new(1)
|
||||
var sword := _definition(&"weapon_rusty_sword")
|
||||
var floor := FloorState.new(GridMapState.new(Vector2i(3, 3)))
|
||||
var cell := Vector2i(1, 1)
|
||||
var first := ItemInstanceType.new(sword, 1, &"ground-first")
|
||||
var second := ItemInstanceType.new(sword, 1, &"ground-second")
|
||||
floor.ground_items[cell] = [first, second]
|
||||
|
||||
var picked := inventory.pick_up_ground_item(floor, cell)
|
||||
var refused := inventory.pick_up_ground_item(floor, cell)
|
||||
|
||||
assert_true(picked.accepted)
|
||||
assert_eq(picked.item.instance_id, &"ground-first")
|
||||
assert_eq(inventory.items[0].instance_id, &"ground-first")
|
||||
assert_eq(floor.ground_items[cell].size(), 1)
|
||||
assert_eq(floor.ground_items[cell][0].instance_id, &"ground-second")
|
||||
assert_false(refused.accepted)
|
||||
assert_eq(refused.reason, &"capacity")
|
||||
assert_eq(floor.ground_items[cell][0].instance_id, &"ground-second")
|
||||
@@ -0,0 +1 @@
|
||||
uid://biqq81digjs5k
|
||||
Reference in New Issue
Block a user