class_name InteractionResolver extends RefCounted const TurnResolverType = preload("res://scripts/domain/turn_resolver.gd") const TurnResultType = preload("res://scripts/domain/turn_result.gd") const LootResolverType = preload("res://scripts/domain/items/loot_resolver.gd") const WorldChestType = preload("res://scripts/domain/items/world_chest.gd") const TARGET_OFFSETS: Array[Vector2i] = [ Vector2i.ZERO, Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT, Vector2i(1, -1), Vector2i(1, 1), Vector2i(-1, 1), Vector2i(-1, -1), ] static func resolve(state: GameState) -> Array[TurnResult]: var results: Array[TurnResult] = [] if state.map.get_cell(state.player.cell) == GridMapState.CellKind.STAIRS and not state.exit_reached: state.exit_reached = true var stairs_message := "You ascend the stairs." if state.floor_state.floor_number > 0 and state.player.cell == state.floor_state.entrance else "You descend the stairs." results.append(TurnResultType.new(state.player.id, &"stairs_used", state.player.cell, state.player.cell, &"", 0, stairs_message)) TurnResolverType.append_enemy_response(state, results) return results var chest_cell := _find_cell_of_kind(state, GridMapState.CellKind.CHEST) if chest_cell.x >= 0: var chest_data: Dictionary = state.floor_state.chests.get(chest_cell, {}) var chest := WorldChestType.new(chest_data) if chest.is_locked(): results.append(TurnResultType.new(state.player.id, &"chest_locked", state.player.cell, chest_cell, &"", 0, "Coffre verrouillé.", {"lock": chest.lock_id})) return results var reward := chest.reward if reward.is_empty(): var table := state.content_catalog.get_loot_table(chest.loot_table_id) if state.content_catalog != null else {} var reward_seed := String(chest_data.get("reward_seed", "%s:%d:%d" % [state.floor_state.seed, chest_cell.x, chest_cell.y])) reward = LootResolverType.resolve(table, reward_seed, "%d:%d" % [chest_cell.x, chest_cell.y]) reward["trap"] = chest.trap_id if not chest.open(reward): results.append(TurnResultType.new(state.player.id, &"nothing_to_interact", state.player.cell, chest_cell, &"", 0, "Nothing to interact with.")) return results # Record the complete opening result before changing the tile, preserving # the chest reward for the panel and cached floor state. state.floor_state.chests[chest_cell] = chest.to_dict() results.append(TurnResultType.new(state.player.id, &"chest_opened", state.player.cell, chest_cell, &"", 0, _chest_message(reward, state.content_catalog), reward)) state.map.set_cell(chest_cell, GridMapState.CellKind.FLOOR) TurnResolverType.append_enemy_response(state, results) return results var door_cell := _find_cell_of_kind(state, GridMapState.CellKind.CLOSED_DOOR) if door_cell.x >= 0: state.map.set_cell(door_cell, GridMapState.CellKind.FLOOR) results.append(TurnResultType.new(state.player.id, &"door_opened", state.player.cell, door_cell, &"", 0, "You open the door.")) TurnResolverType.append_enemy_response(state, results) return results results.append(TurnResultType.new(state.player.id, &"nothing_to_interact", state.player.cell, state.player.cell, &"", 0, "Nothing to interact with.")) return results static func _chest_message(reward: Dictionary, catalog: ContentCatalog = null) -> String: if bool(reward.get("empty", false)): return "Coffre ouvert : il est vide." var parts: PackedStringArray = [] var gold := int(reward.get("gold", 0)) if gold > 0: parts.append("+%d or" % gold) for item: Dictionary in reward.get("items", []): var item_id := StringName(item.get("item_id", "")) var definition := catalog.get_item(item_id) if catalog != null else {} parts.append("%s x%d" % [String(definition.get("name", item_id)), int(item.get("quantity", 1))]) return "Coffre ouvert : " + ", ".join(parts) + "." static func _find_cell_of_kind(state: GameState, kind: GridMapState.CellKind) -> Vector2i: for offset in TARGET_OFFSETS: var cell := state.player.cell + offset if state.map.get_cell(cell) == kind: return cell return Vector2i(-1, -1)