feat: implement door interaction
This commit is contained in:
@@ -2,6 +2,7 @@ class_name GameState
|
||||
extends RefCounted
|
||||
|
||||
const TurnResolverType = preload("res://scripts/domain/turn_resolver.gd")
|
||||
const InteractionResolverType = preload("res://scripts/domain/interaction_resolver.gd")
|
||||
|
||||
var player: ActorState
|
||||
var enemies: Dictionary[StringName, ActorState]
|
||||
@@ -31,6 +32,12 @@ func try_player_action(direction: Vector2i) -> Array[TurnResult]:
|
||||
update_exploration()
|
||||
return results
|
||||
|
||||
func try_interact() -> Array[TurnResult]:
|
||||
var results := InteractionResolverType.resolve(self)
|
||||
if results[0].kind != &"nothing_to_interact":
|
||||
update_exploration()
|
||||
return results
|
||||
|
||||
func compute_visible_cells(game_map: GridMapState, origin: Vector2i, radius: int) -> Dictionary[Vector2i, bool]:
|
||||
var visible: Dictionary[Vector2i, bool] = {}
|
||||
for y in range(origin.y - radius, origin.y + radius + 1):
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
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:
|
||||
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 door_cell := _find_closed_door(state)
|
||||
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_closed_door(state: GameState) -> Vector2i:
|
||||
for offset in TARGET_OFFSETS:
|
||||
var cell := state.player.cell + offset
|
||||
if state.map.get_cell(cell) == GridMapState.CellKind.CLOSED_DOOR:
|
||||
return cell
|
||||
return Vector2i(-1, -1)
|
||||
@@ -17,7 +17,7 @@ static func resolve_player_action(state: GameState, direction: Vector2i) -> Arra
|
||||
state.enemies.erase(enemy.id)
|
||||
results.append(TurnResultType.new(enemy.id, &"enemy_defeated", enemy.cell, enemy.cell, player.id, 0, "The goblin is defeated."))
|
||||
else:
|
||||
_append_enemy_response(state, results)
|
||||
append_enemy_response(state, results)
|
||||
return results
|
||||
if not state.map.is_walkable(destination):
|
||||
results.append(TurnResultType.new(player.id, &"blocked", player.cell, destination, &"", 0, "Your way is blocked."))
|
||||
@@ -25,7 +25,7 @@ static func resolve_player_action(state: GameState, direction: Vector2i) -> Arra
|
||||
var from: Vector2i = player.cell
|
||||
player.cell = destination
|
||||
results.append(TurnResultType.new(player.id, &"moved", from, destination, &"", 0, "You move to (%d, %d)." % [destination.x, destination.y]))
|
||||
_append_enemy_response(state, results)
|
||||
append_enemy_response(state, results)
|
||||
return results
|
||||
|
||||
static func _enemy_at(state: GameState, cell: Vector2i) -> ActorState:
|
||||
@@ -34,7 +34,7 @@ static func _enemy_at(state: GameState, cell: Vector2i) -> ActorState:
|
||||
return enemy
|
||||
return null
|
||||
|
||||
static func _append_enemy_response(state: GameState, results: Array[TurnResult]) -> void:
|
||||
static func append_enemy_response(state: GameState, results: Array[TurnResult]) -> void:
|
||||
for enemy: ActorState in state.enemies.values():
|
||||
var delta: Vector2i = state.player.cell - enemy.cell
|
||||
if abs(delta.x) + abs(delta.y) == 1:
|
||||
|
||||
Reference in New Issue
Block a user