D1: Port item instances and inventory #30
@@ -74,6 +74,55 @@ script = ExtResource("4_hud")
|
||||
|
||||
[node name="SpellTargetingOverlay" parent="HUD" instance=ExtResource("10_spell_targeting")]
|
||||
|
||||
[node name="InventoryPanel" type="PanelContainer" parent="HUD"]
|
||||
visible = false
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -190.0
|
||||
offset_top = -130.0
|
||||
offset_right = 190.0
|
||||
offset_bottom = 130.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme_override_styles/panel = SubResource("StyleBox_hud_panel")
|
||||
|
||||
[node name="Title" type="Label" parent="HUD/InventoryPanel"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 10
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.0
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.0
|
||||
offset_left = -160.0
|
||||
offset_top = 16.0
|
||||
offset_right = 160.0
|
||||
offset_bottom = 44.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 0
|
||||
theme_override_colors/font_color = Color(1, 0.9, 0.5, 1)
|
||||
theme_override_font_sizes/font_size = 22
|
||||
text = "INVENTORY"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="Entries" type="RichTextLabel" parent="HUD/InventoryPanel"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = 24.0
|
||||
offset_top = 58.0
|
||||
offset_right = -24.0
|
||||
offset_bottom = -24.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
bbcode_enabled = false
|
||||
theme_override_colors/default_color = Color(0.9, 0.94, 1, 1)
|
||||
theme_override_font_sizes/normal_font_size = 18
|
||||
|
||||
[node name="ResponsiveLayout" type="Control" parent="HUD"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
class_name InventoryState
|
||||
extends RefCounted
|
||||
|
||||
const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
|
||||
|
||||
var capacity: int
|
||||
var items: Array[ItemInstance] = []
|
||||
|
||||
func _init(new_capacity: int = 20) -> void:
|
||||
capacity = maxi(0, new_capacity)
|
||||
|
||||
func add_item(item: ItemInstance) -> Dictionary:
|
||||
if item == null or item.definition == null or not item.definition.is_valid():
|
||||
return _result(false, &"invalid_item", item)
|
||||
if item.definition.stackable:
|
||||
var existing := _stack_for(item.definition.id)
|
||||
if existing != null:
|
||||
existing.quantity += item.quantity
|
||||
return _result(true, &"", item, existing.instance_id)
|
||||
if items.size() >= capacity:
|
||||
return _result(false, &"capacity", item)
|
||||
items.append(item)
|
||||
return _result(true, &"", item)
|
||||
|
||||
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")
|
||||
var item: ItemInstance = floor.ground_items[cell][0]
|
||||
var result := add_item(item)
|
||||
if not bool(result.accepted):
|
||||
return result
|
||||
floor.ground_items[cell].remove_at(0)
|
||||
if floor.ground_items[cell].is_empty():
|
||||
floor.ground_items.erase(cell)
|
||||
return result
|
||||
|
||||
func to_dict() -> Dictionary:
|
||||
var serialized: Array[Dictionary] = []
|
||||
for item in items:
|
||||
serialized.append(item.to_dict())
|
||||
return {"capacity": capacity, "items": serialized}
|
||||
|
||||
static func from_dict(data: Dictionary, catalog: ContentCatalog) -> InventoryState:
|
||||
var inventory := InventoryState.new(int(data.get("capacity", 20)))
|
||||
for item_data: Dictionary in data.get("items", []):
|
||||
var item := ItemInstanceType.from_dict(item_data, catalog)
|
||||
if item.definition.is_valid():
|
||||
inventory.add_item(item)
|
||||
return inventory
|
||||
|
||||
func _stack_for(item_id: StringName) -> ItemInstance:
|
||||
for item in items:
|
||||
if item.definition.id == item_id:
|
||||
return item
|
||||
return null
|
||||
|
||||
func _result(accepted: bool, reason: StringName = &"", item: ItemInstance = null, stack_merged_into: StringName = &"") -> Dictionary:
|
||||
return {
|
||||
"accepted": accepted,
|
||||
"reason": reason,
|
||||
"item": item,
|
||||
"stack_merged_into": stack_merged_into,
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://rb7b80vbdoge
|
||||
@@ -0,0 +1,26 @@
|
||||
class_name ItemDefinition
|
||||
extends RefCounted
|
||||
|
||||
# Immutable gameplay-facing view of one ASCII catalog entry. The raw catalog data
|
||||
# remains available so later item systems can consume fields without reloading JSON.
|
||||
var id: StringName
|
||||
var name: String
|
||||
var category: StringName
|
||||
var stackable: bool
|
||||
var raw: Dictionary
|
||||
|
||||
func _init(data: Dictionary = {}) -> void:
|
||||
id = StringName(data.get("id", ""))
|
||||
name = String(data.get("name", id))
|
||||
category = StringName(data.get("category", ""))
|
||||
stackable = bool(data.get("stackable", false))
|
||||
raw = data.duplicate(true)
|
||||
|
||||
static func from_catalog(catalog: ContentCatalog, item_id: StringName) -> ItemDefinition:
|
||||
return ItemDefinition.new(catalog.get_item(item_id))
|
||||
|
||||
func is_valid() -> bool:
|
||||
return not id.is_empty()
|
||||
|
||||
func to_dict() -> Dictionary:
|
||||
return raw.duplicate(true)
|
||||
@@ -0,0 +1 @@
|
||||
uid://bi5ccuxorw2qu
|
||||
@@ -0,0 +1,48 @@
|
||||
class_name ItemInstance
|
||||
extends RefCounted
|
||||
|
||||
const ItemDefinitionType = preload("res://scripts/domain/items/item_definition.gd")
|
||||
|
||||
static var _next_generated_id := 1
|
||||
|
||||
var instance_id: StringName
|
||||
var definition: ItemDefinition
|
||||
var quantity: int
|
||||
|
||||
func _init(new_definition: ItemDefinition, new_quantity: int = 1, new_instance_id: StringName = &"") -> void:
|
||||
definition = new_definition
|
||||
quantity = maxi(1, new_quantity)
|
||||
instance_id = new_instance_id if not new_instance_id.is_empty() else _new_instance_id()
|
||||
_reserve_generated_id(instance_id)
|
||||
|
||||
static func from_dict(data: Dictionary, catalog: ContentCatalog) -> ItemInstance:
|
||||
var definition := ItemDefinitionType.from_catalog(catalog, StringName(data.get("item_id", data.get("id", ""))))
|
||||
return ItemInstance.new(
|
||||
definition,
|
||||
int(data.get("quantity", 1)),
|
||||
StringName(data.get("instance_id", ""))
|
||||
)
|
||||
|
||||
static func reset_id_sequence_for_tests() -> void:
|
||||
_next_generated_id = 1
|
||||
|
||||
func to_dict() -> Dictionary:
|
||||
return {
|
||||
"instance_id": String(instance_id),
|
||||
"item_id": String(definition.id),
|
||||
"quantity": quantity,
|
||||
}
|
||||
|
||||
static func _new_instance_id() -> StringName:
|
||||
var generated := StringName("item-%08d" % _next_generated_id)
|
||||
_next_generated_id += 1
|
||||
return generated
|
||||
|
||||
static func _reserve_generated_id(candidate: StringName) -> void:
|
||||
var text := String(candidate)
|
||||
if not text.begins_with("item-"):
|
||||
return
|
||||
var suffix := text.trim_prefix("item-")
|
||||
if not suffix.is_valid_int():
|
||||
return
|
||||
_next_generated_id = maxi(_next_generated_id, suffix.to_int() + 1)
|
||||
@@ -0,0 +1 @@
|
||||
uid://bf6cp1md55ba1
|
||||
@@ -12,6 +12,8 @@ var floors: Dictionary[int, FloorState] = {}
|
||||
var floor_deadlines: Dictionary[int, FloorDeadline] = {}
|
||||
var current_turn := 0
|
||||
var log: Array[GameEvent] = []
|
||||
# D1 owns inventory at run scope so it persists while the player changes floors.
|
||||
var inventory: InventoryState = InventoryState.new()
|
||||
|
||||
func _init(new_seed: String, new_settings: RunSettings = null) -> void:
|
||||
settings = new_settings if new_settings != null else RunSettings.new(new_seed)
|
||||
|
||||
@@ -28,6 +28,8 @@ const TrapEngine = preload("res://scripts/domain/world/trap_engine.gd")
|
||||
@onready var hud: GameHud = $HUD
|
||||
@onready var targeting_overlay: TargetingOverlay = $HUD/TargetingOverlay
|
||||
@onready var spell_targeting_overlay: SpellTargetingOverlay = $HUD/SpellTargetingOverlay
|
||||
@onready var inventory_panel: PanelContainer = $HUD/InventoryPanel
|
||||
@onready var inventory_entries: RichTextLabel = $HUD/InventoryPanel/Entries
|
||||
@onready var audio_library = $AudioLibrary
|
||||
var game_state: GameState
|
||||
var run_state: RunState
|
||||
@@ -183,6 +185,9 @@ func submit_player_action(action: PlayerAction) -> Array[GameEvent]:
|
||||
&"rest":
|
||||
last_turn_results = _process_rest()
|
||||
return run_state.recent_events(last_turn_results.size())
|
||||
&"inventory":
|
||||
last_turn_results = []
|
||||
return _toggle_inventory_panel()
|
||||
&"ranged":
|
||||
return _begin_targeting()
|
||||
&"ranged_target":
|
||||
@@ -342,6 +347,10 @@ func get_actor_cell_rect(actor_id: StringName) -> Rect2:
|
||||
return Rect2()
|
||||
|
||||
func _process_interact() -> Array[TurnResult]:
|
||||
# Ground pickup has precedence at the actor's cell; chests retain their D2
|
||||
# behavior and are deliberately not used as item sources in this slice.
|
||||
if game_state.floor_state.ground_items.has(game_state.player.cell):
|
||||
return _process_ground_pickup()
|
||||
if _is_down_stairs() and not run_state.current_floor_deadline().can_descend():
|
||||
var deadline := run_state.current_floor_deadline()
|
||||
var blocked := _deadline_result(
|
||||
@@ -358,6 +367,49 @@ func _process_interact() -> Array[TurnResult]:
|
||||
_transition_floor(results[0].from_cell)
|
||||
return results
|
||||
|
||||
func _process_ground_pickup() -> Array[TurnResult]:
|
||||
var pickup := run_state.inventory.pick_up_ground_item(game_state.floor_state, game_state.player.cell)
|
||||
var item: ItemInstance = pickup.item
|
||||
var result: TurnResult
|
||||
if bool(pickup.accepted):
|
||||
result = TurnResult.new(
|
||||
game_state.player.id,
|
||||
&"item_picked_up",
|
||||
game_state.player.cell,
|
||||
game_state.player.cell,
|
||||
&"",
|
||||
0,
|
||||
"Picked up %s x%d." % [item.definition.name, item.quantity],
|
||||
{"item_id": item.definition.id, "instance_id": item.instance_id, "quantity": item.quantity, "stack_merged_into": pickup.stack_merged_into}
|
||||
)
|
||||
else:
|
||||
result = TurnResult.new(
|
||||
game_state.player.id,
|
||||
&"pickup_refused",
|
||||
game_state.player.cell,
|
||||
game_state.player.cell,
|
||||
&"",
|
||||
0,
|
||||
"Inventory is full.",
|
||||
{"reason": pickup.reason}
|
||||
)
|
||||
_resolve_player_action([result], false)
|
||||
return [result]
|
||||
|
||||
func _toggle_inventory_panel() -> Array[GameEvent]:
|
||||
inventory_panel.visible = not inventory_panel.visible
|
||||
if inventory_panel.visible:
|
||||
_refresh_inventory_panel()
|
||||
var kind: StringName = &"inventory_opened" if inventory_panel.visible else &"inventory_closed"
|
||||
var message := "Inventory opened." if inventory_panel.visible else "Inventory closed."
|
||||
return _record_ranged_event(kind, &"", {"item_count": run_state.inventory.items.size()}, message)
|
||||
|
||||
func _refresh_inventory_panel() -> void:
|
||||
var lines: PackedStringArray = []
|
||||
for item in run_state.inventory.items:
|
||||
lines.append("%s x%d" % [item.definition.name, item.quantity])
|
||||
inventory_entries.text = "\n".join(lines) if not lines.is_empty() else "Inventory is empty."
|
||||
|
||||
func _process_rest() -> Array[TurnResult]:
|
||||
if not _is_bed():
|
||||
var refused := _deadline_result(&"rest_refused", "Il faut un lit pour se reposer. Cherchez-en un dans une salle sécurisée.")
|
||||
@@ -552,6 +604,7 @@ func _sync_camera_to_player() -> void:
|
||||
func _sync_map_renderer_visibility() -> void:
|
||||
map_renderer.set_visibility(game_state.visible_cells, game_state.explored_cells)
|
||||
map_renderer.set_special_markers(game_state.floor_state.traps, game_state.floor_state.shrines, game_state.floor_state.quest_boards)
|
||||
map_renderer.set_ground_items(game_state.floor_state.ground_items)
|
||||
|
||||
func _sync_enemy_views() -> void:
|
||||
for enemy_id in enemy_views.keys():
|
||||
|
||||
@@ -24,6 +24,7 @@ var _explored_cells: Dictionary[Vector2i, bool] = {}
|
||||
var _traps: Dictionary[Vector2i, Dictionary] = {}
|
||||
var _shrines: Dictionary[Vector2i, Dictionary] = {}
|
||||
var _quest_boards: Dictionary[Vector2i, Dictionary] = {}
|
||||
var _ground_items: Dictionary[Vector2i, Array] = {}
|
||||
|
||||
func get_cell_rect(cell: Vector2i) -> Rect2:
|
||||
return Rect2(Vector2(cell * TILE_SIZE), Vector2(TILE_SIZE))
|
||||
@@ -43,6 +44,17 @@ func set_special_markers(traps: Dictionary[Vector2i, Dictionary], shrines: Dicti
|
||||
_quest_boards = quest_boards.duplicate(true)
|
||||
queue_redraw()
|
||||
|
||||
func set_ground_items(ground_items: Dictionary[Vector2i, Array]) -> void:
|
||||
_ground_items = ground_items.duplicate()
|
||||
queue_redraw()
|
||||
|
||||
func get_ground_item_marker_cells() -> Dictionary[Vector2i, bool]:
|
||||
var cells: Dictionary[Vector2i, bool] = {}
|
||||
for cell in _ground_items:
|
||||
if _visible_cells.has(cell) and not _ground_items[cell].is_empty():
|
||||
cells[cell] = true
|
||||
return cells
|
||||
|
||||
func get_cell_visibility_state(cell: Vector2i) -> StringName:
|
||||
if _visible_cells.has(cell):
|
||||
return &"visible"
|
||||
@@ -88,3 +100,11 @@ func _draw_special_marker(cell: Vector2i, tile_rect: Rect2) -> void:
|
||||
draw_circle(tile_rect.get_center(), 5.0, Color(0.45, 0.85, 1.0, 0.95))
|
||||
if _quest_boards.has(cell):
|
||||
draw_rect(Rect2(tile_rect.position + Vector2(10, 7), Vector2(12, 18)), Color(0.95, 0.78, 0.32, 0.95))
|
||||
if _ground_items.has(cell) and not _ground_items[cell].is_empty():
|
||||
# Gold diamond: visible only inside current FOV, matching all world markers.
|
||||
var center := tile_rect.get_center()
|
||||
var diamond := PackedVector2Array([
|
||||
center + Vector2(0, -7), center + Vector2(7, 0),
|
||||
center + Vector2(0, 7), center + Vector2(-7, 0),
|
||||
])
|
||||
draw_colored_polygon(diamond, Color(1.0, 0.82, 0.22, 0.96))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
@@ -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