feat: port item instances and inventory

This commit is contained in:
goose
2026-10-03 13:16:00 -04:00
parent 391e95f6f4
commit f9e68e6110
16 changed files with 417 additions and 7 deletions
+6 -4
View File
@@ -20,16 +20,18 @@ func test_input_pipeline_routes_e_interaction_and_movement_through_action_router
assert_eq(state.player.cell, start_cell + Vector2i.DOWN)
assert_eq(game.get_run_state().recent_events(2)[0].kind, &"moved")
func test_input_pipeline_routes_unavailable_input_to_the_journal_and_hud() -> void:
func test_input_pipeline_routes_inventory_input_to_the_live_panel_and_journal() -> void:
var panel := game.get_node("HUD/InventoryPanel") as PanelContainer
var event_log := game.get_node("HUD/EventLog") as RichTextLabel
await _dispatch_key(KEY_I)
var event := game.get_run_state().recent_events(1)[0]
assert_eq(event.kind, &"action_unavailable")
assert_eq(event.payload, {"action": &"inventory"})
assert_eq(event.kind, &"inventory_opened")
assert_eq(event.payload, {"item_count": 0})
assert_eq(event.turn, 1)
assert_string_contains(event_log.get_parsed_text(), "inventory")
assert_true(panel.visible)
assert_string_contains(event_log.get_parsed_text(), "Inventory opened")
func _dispatch_key(keycode: Key) -> void:
Input.parse_input_event(_key_event(keycode, true))
+56
View File
@@ -0,0 +1,56 @@
extends GutTest
const GameScene = preload("res://scenes/Game.tscn")
const ItemDefinitionType = preload("res://scripts/domain/items/item_definition.gd")
const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
var game: Game
func before_each() -> void:
game = GameScene.instantiate()
add_child_autofree(game)
await get_tree().process_frame
func test_real_interact_picks_visible_ground_item_then_inventory_action_opens_live_panel() -> void:
var state := game.get_game_state()
var catalog := game.spell_catalog
var definition := ItemDefinitionType.from_catalog(catalog, &"consumable_bandage")
var ground_item := ItemInstanceType.new(definition, 2, &"e2e-bandage")
state.floor_state.ground_items[state.player.cell] = [ground_item]
game._sync_map_renderer_visibility()
await get_tree().process_frame
var marker: Dictionary = game.get_node("World/MapRenderer").get_ground_item_marker_cells()
assert_true(marker.has(state.player.cell))
await _dispatch_key(KEY_E)
assert_eq(game.get_run_state().inventory.items.size(), 1)
assert_eq(game.get_run_state().inventory.items[0].instance_id, &"e2e-bandage")
assert_eq(game.get_run_state().inventory.items[0].quantity, 2)
assert_false(state.floor_state.ground_items.has(state.player.cell))
var pickup := game.get_run_state().recent_events(1)[0]
assert_eq(pickup.kind, &"item_picked_up")
assert_eq(pickup.payload.item_id, &"consumable_bandage")
assert_eq(pickup.payload.quantity, 2)
await _dispatch_key(KEY_I)
var panel := game.get_node("HUD/InventoryPanel") as PanelContainer
var entries := game.get_node("HUD/InventoryPanel/Entries") as RichTextLabel
assert_true(panel.visible)
assert_string_contains(entries.get_parsed_text(), "Bandage x2")
assert_eq(game.get_run_state().recent_events(1)[0].kind, &"inventory_opened")
func _dispatch_key(keycode: Key) -> void:
Input.parse_input_event(_key_event(keycode, true))
await get_tree().process_frame
Input.parse_input_event(_key_event(keycode, false))
await get_tree().process_frame
func _key_event(keycode: Key, pressed: bool) -> InputEventKey:
var event := InputEventKey.new()
event.keycode = keycode
event.physical_keycode = keycode
event.pressed = pressed
return event
@@ -0,0 +1 @@
uid://db0isbiu6dcuv
+4 -3
View File
@@ -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:
+85
View File
@@ -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")
+1
View File
@@ -0,0 +1 @@
uid://biqq81digjs5k