50 lines
1.9 KiB
GDScript
50 lines
1.9 KiB
GDScript
class_name InteractionResolver
|
|
extends RefCounted
|
|
|
|
const TurnResolverType = preload("res://scripts/domain/turn_resolver.gd")
|
|
const TurnResultType = preload("res://scripts/domain/turn_result.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
|
|
results.append(TurnResultType.new(state.player.id, &"stairs_used", state.player.cell, state.player.cell, &"", 0, "You descend the stairs."))
|
|
TurnResolverType.append_enemy_response(state, results)
|
|
return results
|
|
|
|
var chest_cell := _find_cell_of_kind(state, GridMapState.CellKind.CHEST)
|
|
if chest_cell.x >= 0:
|
|
state.map.set_cell(chest_cell, GridMapState.CellKind.FLOOR)
|
|
results.append(TurnResultType.new(state.player.id, &"chest_opened", state.player.cell, chest_cell, &"", 0, "You open the chest."))
|
|
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 _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)
|