feat: implement door interaction

This commit is contained in:
goose
2026-10-01 15:53:07 -04:00
parent e501dbccc6
commit b469c6decb
10 changed files with 249 additions and 14 deletions
+75
View File
@@ -0,0 +1,75 @@
extends GutTest
const GridMapState = preload("res://scripts/domain/grid_map_state.gd")
const ActorState = preload("res://scripts/domain/actor_state.gd")
const GameState = preload("res://scripts/domain/game_state.gd")
func make_state() -> GameState:
var map := GridMapState.new(Vector2i(8, 6), GridMapState.CellKind.FLOOR)
var player := ActorState.new(&"player", Vector2i(3, 3), 12, 3)
var goblin := ActorState.new(&"goblin", Vector2i(6, 3), 6, 2)
return GameState.new(map, player, {&"goblin": goblin})
func test_try_interact_opens_the_first_closed_door_in_contract_neighbour_order() -> void:
var state := make_state()
var north := state.player.cell + Vector2i.UP
var east := state.player.cell + Vector2i.RIGHT
state.map.set_cell(north, GridMapState.CellKind.CLOSED_DOOR)
state.map.set_cell(east, GridMapState.CellKind.CLOSED_DOOR)
var results: Array[TurnResult] = state.try_interact()
assert_eq(results.size(), 2)
assert_eq(results[0].kind, &"door_opened")
assert_eq(results[0].from_cell, state.player.cell)
assert_eq(results[0].to_cell, north)
assert_eq(state.map.get_cell(north), GridMapState.CellKind.FLOOR)
assert_eq(state.map.get_cell(east), GridMapState.CellKind.CLOSED_DOOR)
assert_eq(state.player.cell, Vector2i(3, 3))
func test_try_interact_without_a_target_only_reports_nothing_and_does_not_take_a_turn() -> void:
var state := make_state()
state.enemies[&"goblin"].cell = Vector2i(4, 3)
var player_cell := state.player.cell
var player_hp := state.player.hit_points
var enemy_cell := state.enemies[&"goblin"].cell
var results: Array[TurnResult] = state.try_interact()
assert_eq(results.size(), 1)
assert_eq(results[0].kind, &"nothing_to_interact")
assert_eq(state.player.cell, player_cell)
assert_eq(state.player.hit_points, player_hp)
assert_eq(state.enemies[&"goblin"].cell, enemy_cell)
func test_opening_a_door_recalculates_fov_and_triggers_exactly_one_enemy_response() -> void:
var map := GridMapState.new(Vector2i(7, 5), GridMapState.CellKind.FLOOR)
var door := Vector2i(2, 2)
var revealed_cell := Vector2i(3, 2)
map.set_cell(door, GridMapState.CellKind.CLOSED_DOOR)
var player := ActorState.new(&"player", Vector2i(1, 2), 12, 3)
var goblin := ActorState.new(&"goblin", Vector2i(5, 2), 6, 2)
var state := GameState.new(map, player, {&"goblin": goblin})
assert_false(state.visible_cells.has(revealed_cell))
var results: Array[TurnResult] = state.try_interact()
var enemy_result_count := 0
for result in results:
if String(result.kind).begins_with("enemy_"):
enemy_result_count += 1
assert_eq(results[0].kind, &"door_opened")
assert_eq(state.map.get_cell(door), GridMapState.CellKind.FLOOR)
assert_true(state.visible_cells.has(revealed_cell))
assert_eq(enemy_result_count, 1)
func test_stairs_below_the_player_take_priority_and_mark_the_exit_reached() -> void:
var state := make_state()
state.map.set_cell(state.player.cell, GridMapState.CellKind.STAIRS)
state.map.set_cell(state.player.cell + Vector2i.UP, GridMapState.CellKind.CLOSED_DOOR)
var results: Array[TurnResult] = state.try_interact()
assert_true(state.exit_reached)
assert_eq(results[0].kind, &"stairs_used")
assert_eq(state.map.get_cell(state.player.cell + Vector2i.UP), GridMapState.CellKind.CLOSED_DOOR)