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:
|
||||
|
||||
+14
-5
@@ -25,7 +25,10 @@ func _ready() -> void:
|
||||
_sync_enemy_views()
|
||||
hud.refresh(game_state)
|
||||
audio_library.set_ambience_context("exploration")
|
||||
if "--capture" in OS.get_cmdline_user_args():
|
||||
var user_args := OS.get_cmdline_user_args()
|
||||
if "--capture-door" in user_args:
|
||||
submit_test_interact()
|
||||
if "--capture" in user_args or "--capture-door" in user_args:
|
||||
_capture_visual_check()
|
||||
|
||||
func _capture_visual_check() -> void:
|
||||
@@ -62,7 +65,7 @@ func submit_test_action(direction: Vector2i) -> Array[TurnResult]:
|
||||
return _process_action(direction)
|
||||
|
||||
func submit_test_interact() -> Array[TurnResult]:
|
||||
return _refresh_hud_for_context()
|
||||
return _process_interact()
|
||||
|
||||
func get_game_state() -> GameState:
|
||||
return game_state
|
||||
@@ -74,9 +77,15 @@ func get_actor_cell_rect(actor_id: StringName) -> Rect2:
|
||||
return enemy_views[actor_id].get_cell_rect()
|
||||
return Rect2()
|
||||
|
||||
func _refresh_hud_for_context() -> Array[TurnResult]:
|
||||
hud.refresh(game_state)
|
||||
return []
|
||||
func _process_interact() -> Array[TurnResult]:
|
||||
var results := game_state.try_interact()
|
||||
_sync_map_renderer_visibility()
|
||||
player_view.refresh()
|
||||
_sync_enemy_views()
|
||||
_show_damage_feedback(results)
|
||||
_dispatch_audio(results)
|
||||
hud.refresh(game_state, results)
|
||||
return results
|
||||
|
||||
func _process_action(direction: Vector2i) -> Array[TurnResult]:
|
||||
var results := game_state.try_player_action(direction)
|
||||
|
||||
@@ -28,7 +28,7 @@ if [[ -z "${DISPLAY:-}" ]]; then
|
||||
fi
|
||||
|
||||
set +e
|
||||
timeout --foreground 20s "${runner[@]}" "$GODOT_BIN" --path . -- --capture >"$log_path" 2>&1
|
||||
timeout --foreground 20s "${runner[@]}" "$GODOT_BIN" --path . -- --capture-door >"$log_path" 2>&1
|
||||
status=$?
|
||||
set -e
|
||||
if (( status != 0 )); then
|
||||
|
||||
Reference in New Issue
Block a user