Compare commits

...
3 Commits
19 changed files with 291 additions and 29 deletions
+6 -1
View File
@@ -25,10 +25,15 @@ func _init(new_map: GridMapState, new_player: ActorState, new_enemies: Dictionar
func bind_run_state(run_state: RunState) -> void:
run_state.set_floor_state(floor_state)
floor_state = run_state.current_floor_state()
set_floor_state(run_state.current_floor_state())
func set_floor_state(new_floor_state: FloorState) -> void:
floor_state = new_floor_state
map = floor_state.map
enemies = floor_state.enemies
explored_cells = floor_state.explored_cells
exit_reached = false
update_exploration()
func get_context_target() -> StringName:
if exit_reached:
+4 -1
View File
@@ -20,13 +20,16 @@ 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."))
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:
state.map.set_cell(chest_cell, GridMapState.CellKind.FLOOR)
if state.floor_state.chests.has(chest_cell):
state.floor_state.chests[chest_cell]["opened"] = true
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
+53
View File
@@ -0,0 +1,53 @@
class_name FloorTransition
extends RefCounted
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
const SurfaceGeneratorType = preload("res://scripts/domain/world/surface_generator.gd")
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
var catalog: ContentCatalog
func _init(new_catalog: ContentCatalog) -> void:
catalog = new_catalog
func initial_floor(run: RunState) -> FloorState:
if not run.floors.has(0):
run.set_floor_state(SurfaceGeneratorType.generate(run.seed))
run.current_floor = 0
return run.current_floor_state()
# Returns the cached/generated destination and the stairs cell on which the
# player arrives. Floor 0's sole stairs descends, while every procedural floor
# has an up stair at entrance and down stair at exit.
func transition(run: RunState, source: FloorState, stairs_cell: Vector2i) -> Dictionary:
var destination_number := _destination_floor_number(source, stairs_cell)
var destination := _floor_for(run, destination_number)
var arrival := destination.entrance if destination_number > source.floor_number else destination.exit
run.current_floor = destination_number
return {"floor": destination, "arrival": arrival}
func _destination_floor_number(source: FloorState, stairs_cell: Vector2i) -> int:
if source.floor_number == 0:
return 1
if stairs_cell == source.entrance:
return source.floor_number - 1
return source.floor_number + 1
func _floor_for(run: RunState, floor_number: int) -> FloorState:
if run.floors.has(floor_number):
return run.floors[floor_number]
if floor_number == 0:
return initial_floor(run)
var floor := FloorGeneratorType.generate(run.seed, floor_number, catalog)
# B3 only marked the down stair. B5 makes the entry stair explicit so a
# return route is visible and interaction direction is unambiguous.
floor.map.set_cell(floor.entrance, GridMapStateType.CellKind.STAIRS)
_materialize_unopened_chests(floor)
floor.metadata["hand_authored"] = false
run.set_floor_state(floor)
return floor
func _materialize_unopened_chests(floor: FloorState) -> void:
for cell in floor.chests:
if not bool(floor.chests[cell].get("opened", false)):
floor.map.set_cell(cell, GridMapStateType.CellKind.CHEST)
@@ -0,0 +1 @@
uid://dld2hudsse86m
+2 -2
View File
@@ -49,7 +49,7 @@ func tree_distances(start_index: int) -> Dictionary[int, int]:
var distances: Dictionary[int, int] = {start_index: 0}
var frontier: Array[int] = [start_index]
while not frontier.is_empty():
var current := frontier.pop_front()
var current: int = frontier.pop_front()
for neighbor in adjacency.get(current, []):
if not distances.has(neighbor):
distances[neighbor] = distances[current] + 1
@@ -61,7 +61,7 @@ func farthest_tree_room(start_index: int) -> int:
var farthest_index := start_index
var farthest_distance := -1
for room_index in range(distances.size()):
var distance := distances.get(room_index, -1)
var distance: int = distances.get(room_index, -1)
if distance > farthest_distance:
farthest_index = room_index
farthest_distance = distance
+2 -2
View File
@@ -25,7 +25,7 @@ static func events(catalog: ContentCatalog) -> Array[Dictionary]:
return _definitions(catalog, &"event")
static func npc_for_role(catalog: ContentCatalog, role: StringName) -> Dictionary:
var preferred_id := {
var preferred_id: StringName = {
&"shop": &"npc_wandering_merchant",
&"guild": &"npc_guild_keeper",
&"sanctuary": &"npc_shelter_survivor",
@@ -39,7 +39,7 @@ static func npc_for_role(catalog: ContentCatalog, role: StringName) -> Dictionar
return available[0] if not available.is_empty() else {}
static func event_for_role(catalog: ContentCatalog, role: StringName) -> Dictionary:
var preferred_id := {
var preferred_id: StringName = {
&"narrative": &"event_narrative_note",
&"mystery": &"event_vending_machine",
&"puzzle": &"event_puzzle_pressure_plates",
+43
View File
@@ -0,0 +1,43 @@
class_name SurfaceGenerator
extends RefCounted
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
const FloorStateType = preload("res://scripts/domain/world/floor_state.gd")
const ActorStateType = preload("res://scripts/domain/actor_state.gd")
# Floor zero deliberately remains an authored tutorial surface. Its geometry is
# not derived from the run seed; RunState supplies persistence by caching it.
static func generate(_seed: String = "") -> FloorState:
var map := GridMapStateType.new(Vector2i(30, 20), GridMapStateType.CellKind.WALL)
_carve_room(map, Rect2i(2, 12, 6, 5))
_carve_room(map, Rect2i(2, 4, 7, 7))
_carve_room(map, Rect2i(10, 2, 10, 6))
_carve_room(map, Rect2i(21, 5, 7, 8))
_carve_vertical_corridor(map, 4, 10, 12)
_carve_horizontal_corridor(map, 8, 10, 6)
_carve_horizontal_corridor(map, 18, 21, 6)
_carve_vertical_corridor(map, 18, 4, 6)
map.set_cell(Vector2i(4, 11), GridMapStateType.CellKind.CLOSED_DOOR)
map.set_cell(Vector2i(4, 7), GridMapStateType.CellKind.CHEST)
map.set_cell(Vector2i(18, 4), GridMapStateType.CellKind.STAIRS)
var floor := FloorStateType.new(map, 0, 0)
floor.entrance = Vector2i(4, 12)
floor.exit = Vector2i(18, 4)
floor.enemies[&"goblin"] = ActorStateType.new(&"goblin", Vector2i(24, 8), 6, 2)
floor.chests[Vector2i(4, 7)] = {"opened": false}
floor.metadata = {"hand_authored": true, "theme": "surface"}
return floor
static func _carve_room(map: GridMapState, room: Rect2i) -> void:
for y in range(room.position.y, room.end.y):
for x in range(room.position.x, room.end.x):
map.set_cell(Vector2i(x, y), GridMapStateType.CellKind.FLOOR)
static func _carve_horizontal_corridor(map: GridMapState, from_x: int, to_x: int, y: int) -> void:
for x in range(from_x, to_x + 1):
map.set_cell(Vector2i(x, y), GridMapStateType.CellKind.FLOOR)
static func _carve_vertical_corridor(map: GridMapState, x: int, from_y: int, to_y: int) -> void:
for y in range(from_y, to_y + 1):
map.set_cell(Vector2i(x, y), GridMapStateType.CellKind.FLOOR)
@@ -0,0 +1 @@
uid://cieqsjogiiein
+23 -6
View File
@@ -14,6 +14,8 @@ const GameEventType = preload("res://scripts/domain/run/game_event.gd")
const PlayerActionType = preload("res://scripts/domain/run/player_action.gd")
const ActionRouterType = preload("res://scripts/game/action_router.gd")
const ActorViewType = preload("res://scripts/presentation/actor_view.gd")
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
const FloorTransitionType = preload("res://scripts/domain/run/floor_transition.gd")
@onready var event_presenter: EventPresenter = $EventPresenter
@onready var map_renderer: MapRenderer = $World/MapRenderer
@onready var player_view: ActorView = $World/Player
@@ -29,6 +31,7 @@ var action_router: ActionRouter
var pending_run_settings: RunSettings
var pending_player_name := "Sans-Nom"
var pending_base_archetype: StringName = &"vanguard"
var floor_transition: FloorTransition
func configure_new_run(settings: RunSettings, player_name: String, base_archetype: StringName) -> void:
pending_run_settings = settings
@@ -64,7 +67,10 @@ func _initialize_run_state() -> void:
run_state = RunStateType.new(settings.seed, settings)
run_state.player_name = pending_player_name
run_state.base_archetype = pending_base_archetype
game_state.bind_run_state(run_state)
floor_transition = FloorTransitionType.new(ContentCatalogType.load_from_directory("res://data/ascii"))
var initial_floor := floor_transition.initial_floor(run_state)
game_state.set_floor_state(initial_floor)
game_state.player.cell = initial_floor.entrance
if pending_run_settings != null:
var game_started := GameEventType.new(
run_state.next_turn(),
@@ -170,13 +176,24 @@ func get_actor_cell_rect(actor_id: StringName) -> Rect2:
return Rect2()
func _process_interact() -> Array[TurnResult]:
if game_state.get_context_target() == &"exit_reached":
return _refresh_hud_for_context()
return _resolve_player_action(game_state.try_interact(), false)
var results := game_state.try_interact()
_resolve_player_action(results, false)
if not results.is_empty() and results[0].kind == &"stairs_used":
_transition_floor(results[0].from_cell)
return results
func _refresh_hud_for_context() -> Array[TurnResult]:
func _transition_floor(stairs_cell: Vector2i) -> void:
var transition := floor_transition.transition(run_state, game_state.floor_state, stairs_cell)
var destination: FloorState = transition["floor"]
game_state.set_floor_state(destination)
game_state.player.cell = transition["arrival"]
game_state.update_exploration()
map_renderer.set_map(game_state.map)
_sync_map_renderer_visibility()
player_view.set_actor(game_state.player)
_sync_camera_to_player()
_sync_enemy_views()
hud.refresh(game_state, run_state.recent_events(run_state.log.size()))
return []
func _process_action(direction: Vector2i) -> Array[TurnResult]:
return _resolve_player_action(game_state.try_player_action(direction), true)
@@ -53,7 +53,7 @@ static func _load_file(path: String, relative_path: String, result: Dictionary)
if file == null:
result.diagnostics.append("Unable to read content file: %s" % relative_path)
return
var parsed := JSON.parse_string(file.get_as_text())
var parsed: Variant = JSON.parse_string(file.get_as_text())
if parsed == null:
result.diagnostics.append("Invalid JSON content file: %s" % relative_path)
return
+2 -1
View File
@@ -4,6 +4,7 @@ extends CanvasLayer
const DOOR_PROMPT := "Espace — Ouvrir la porte"
const CHEST_PROMPT := "Espace — Ouvrir le coffre"
const STAIRS_PROMPT := "Espace — Descendre l’escalier"
const STAIRS_UP_PROMPT := "Espace — Monter l’escalier"
const EXIT_REACHED_PROMPT := "Sortie atteinte — prochain étage bientôt disponible"
@onready var player_health_label: Label = $PlayerHealthLabel
@@ -40,7 +41,7 @@ func get_context_prompt() -> String:
&"closed_door":
return DOOR_PROMPT
&"stairs":
return STAIRS_PROMPT
return STAIRS_UP_PROMPT if _state.floor_state.floor_number > 0 and _state.player.cell == _state.floor_state.entrance else STAIRS_PROMPT
&"exit_reached":
return EXIT_REACHED_PROMPT
_:
+2 -1
View File
@@ -43,7 +43,8 @@ func test_real_input_opens_door_then_chest_and_preserves_stairs_route() -> void:
assert_eq(state.player.cell, Vector2i(18, 4))
assert_eq(prompt_label.text, "Espace — Descendre l’escalier")
game._unhandled_input(_key_event(KEY_SPACE))
assert_true(state.exit_reached)
assert_eq(game.get_run_state().current_floor, 1)
assert_eq(game.get_game_state().player.cell, game.get_game_state().floor_state.entrance)
assert_string_contains(event_log.get_parsed_text(), "You descend the stairs.")
func _key_event(keycode: Key) -> InputEventKey:
+8 -8
View File
@@ -8,7 +8,7 @@ func before_each() -> void:
add_child_autofree(game)
await get_tree().process_frame
func test_real_input_event_completes_door_to_stairs_exit_without_replacing_the_current_map() -> void:
func test_real_input_event_completes_door_to_stairs_descent_and_arrives_on_floor_one() -> void:
var state := game.get_game_state()
var hud := game.get_node("HUD") as GameHud
var prompt_label := game.get_node("HUD/ContextPromptLabel") as Label
@@ -30,15 +30,15 @@ func test_real_input_event_completes_door_to_stairs_exit_without_replacing_the_c
assert_eq(hud.get_context_prompt(), "Espace — Descendre l’escalier")
game._unhandled_input(_key_event(KEY_E))
assert_true(state.exit_reached)
assert_eq(hud.get_context_prompt(), "Sortie atteinte — prochain étage bientôt disponible")
assert_eq(prompt_label.text, "Sortie atteinte — prochain étage bientôt disponible")
assert_eq(game.get_run_state().current_floor, 1)
assert_eq(hud.get_context_prompt(), "Espace — Monter l’escalier")
assert_eq(prompt_label.text, "Espace — Monter l’escalier")
assert_string_contains(event_log.get_parsed_text(), "You open the door.")
assert_string_contains(event_log.get_parsed_text(), "You descend the stairs.")
assert_same(state.map, initial_map)
assert_eq(state.map.size, initial_map_size)
assert_eq(state.map.get_cell(Vector2i(18, 4)), GridMapState.CellKind.STAIRS)
assert_eq(state.player.cell, Vector2i(18, 4))
assert_ne(state.map, initial_map)
assert_ne(state.map.size, initial_map_size)
assert_eq(initial_map.get_cell(Vector2i(18, 4)), GridMapState.CellKind.STAIRS)
assert_eq(state.player.cell, state.floor_state.entrance)
func _key_event(keycode: Key) -> InputEventKey:
var event := InputEventKey.new()
+3 -2
View File
@@ -36,8 +36,9 @@ func test_real_game_exposes_door_and_stairs_prompts_with_help_and_event_log() ->
assert_eq(prompt_label.text, "Espace — Descendre l’escalier")
var stairs_results := game.submit_test_interact()
assert_eq(stairs_results[0].kind, &"stairs_used")
assert_eq(hud.get_context_prompt(), "Sortie atteinte — prochain étage bientôt disponible")
assert_eq(prompt_label.text, "Sortie atteinte — prochain étage bientôt disponible")
assert_eq(game.get_run_state().current_floor, 1)
assert_eq(hud.get_context_prompt(), "Espace — Monter l’escalier")
assert_eq(prompt_label.text, "Espace — Monter l’escalier")
var results := game.submit_test_action(Vector2i.LEFT)
assert_eq(results[0].kind, &"moved")
+67
View File
@@ -0,0 +1,67 @@
extends GutTest
const MainMenuScene = preload("res://scenes/menu/MainMenu.tscn")
var menu: MainMenu
func before_each() -> void:
menu = MainMenuScene.instantiate()
add_child_autofree(menu)
await get_tree().process_frame
await get_tree().process_frame
func test_actual_menu_new_run_descends_to_generated_floor_one_and_returns_to_surface() -> void:
await _click_button(menu.get_node("Menu/Layout/NewRunButton") as Button)
var creation := menu.get_node("CharacterCreation") as CharacterCreation
(creation.get_node("Panel/Layout/Fields/SeedEdit") as LineEdit).text = "B5-MENU-E2E"
await _click_button(creation.get_node("Panel/Layout/Actions/StartRunButton") as Button)
await get_tree().process_frame
var game := menu.get_node("Game") as Game
assert_not_null(game)
assert_eq(game.get_run_state().current_floor, 0)
var surface := game.get_game_state().floor_state
assert_true(surface.metadata.get("hand_authored", false))
game.get_game_state().player.cell = surface.exit
game.submit_test_interact()
await get_tree().process_frame
var first_floor := game.get_game_state().floor_state
assert_eq(game.get_run_state().current_floor, 1)
assert_false(first_floor.metadata.get("hand_authored", false))
assert_eq(game.get_game_state().player.cell, first_floor.entrance)
assert_eq(first_floor.map.get_cell(first_floor.entrance), GridMapState.CellKind.STAIRS)
assert_eq((game.get_node("HUD") as GameHud).get_context_prompt(), "Espace — Monter l’escalier")
game.get_game_state().player.cell = first_floor.entrance
game.submit_test_interact()
await get_tree().process_frame
assert_string_contains((game.get_node("HUD/EventLog") as RichTextLabel).get_parsed_text(), "You ascend the stairs.")
assert_eq(game.get_run_state().current_floor, 0)
assert_same(game.get_game_state().floor_state, surface)
assert_eq(game.get_game_state().player.cell, surface.exit)
func _click_button(button: Button) -> void:
assert_not_null(button)
assert_true(button.visible)
var click_position := button.get_global_rect().get_center()
get_tree().root.push_input(_mouse_motion_event(click_position), true)
await get_tree().process_frame
get_tree().root.push_input(_mouse_button_event(click_position, true), true)
await get_tree().process_frame
get_tree().root.push_input(_mouse_button_event(click_position, false), true)
await get_tree().process_frame
func _mouse_motion_event(position: Vector2) -> InputEventMouseMotion:
var event := InputEventMouseMotion.new()
event.position = position
event.global_position = position
return event
func _mouse_button_event(position: Vector2, pressed: bool) -> InputEventMouseButton:
var event := InputEventMouseButton.new()
event.position = position
event.global_position = position
event.button_index = MOUSE_BUTTON_LEFT
event.button_mask = MOUSE_BUTTON_MASK_LEFT if pressed else 0
event.pressed = pressed
return event
@@ -0,0 +1 @@
uid://ctrkpp7a6m58j
+5 -4
View File
@@ -48,7 +48,7 @@ func test_stairs_usage_triggers_exactly_one_enemy_response_without_transitioning
assert_eq(state.player.cell, stairs)
assert_same(state.map, map)
func test_stairs_usage_refreshes_exact_exit_prompt_and_writes_the_journal() -> void:
func test_stairs_usage_transitions_to_floor_one_and_writes_the_journal() -> void:
var game := GameScene.instantiate() as Game
add_child_autofree(game)
await get_tree().process_frame
@@ -62,7 +62,8 @@ func test_stairs_usage_refreshes_exact_exit_prompt_and_writes_the_journal() -> v
var results := game.submit_test_interact()
assert_eq(results[0].kind, &"stairs_used")
assert_true(state.exit_reached)
assert_eq(hud.get_context_prompt(), "Sortie atteinte — prochain étage bientôt disponible")
assert_eq(prompt_label.text, "Sortie atteinte — prochain étage bientôt disponible")
assert_eq(game.get_run_state().current_floor, 1)
assert_eq(game.get_game_state().player.cell, game.get_game_state().floor_state.entrance)
assert_eq(hud.get_context_prompt(), "Espace — Monter l’escalier")
assert_eq(prompt_label.text, "Espace — Monter l’escalier")
assert_string_contains(event_log.get_parsed_text(), "You descend the stairs.")
@@ -0,0 +1,66 @@
extends GutTest
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
const SurfaceGeneratorType = preload("res://scripts/domain/world/surface_generator.gd")
const FloorTransitionType = preload("res://scripts/domain/run/floor_transition.gd")
const RunStateType = preload("res://scripts/domain/run/run_state.gd")
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
const ASCII_CATALOG_PATH := "res://data/ascii"
func test_floor_zero_is_hand_authored_and_floor_one_is_seed_floor_one_procedural() -> void:
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
var run := RunStateType.new("B5-surface-contract")
var transitions := FloorTransitionType.new(catalog)
var surface := transitions.initial_floor(run)
var descend := transitions.transition(run, surface, surface.exit)
var first_floor: FloorState = descend["floor"]
assert_eq(surface.floor_number, 0)
assert_true(surface.metadata.get("hand_authored", false))
assert_eq(surface.map.size, Vector2i(30, 20))
assert_eq(surface.entrance, Vector2i(4, 12))
assert_eq(surface.exit, Vector2i(18, 4))
assert_eq(surface.map.get_cell(surface.exit), GridMapStateType.CellKind.STAIRS)
assert_eq(first_floor.floor_number, 1)
assert_false(first_floor.metadata.get("hand_authored", false))
assert_eq(first_floor.seed, _seed_value("B5-surface-contract", 1))
assert_eq(first_floor.map.get_cell(first_floor.entrance), GridMapStateType.CellKind.STAIRS)
assert_eq(first_floor.map.get_cell(first_floor.exit), GridMapStateType.CellKind.STAIRS)
assert_eq(descend["arrival"], first_floor.entrance)
var independently_authored := SurfaceGeneratorType.generate("another-seed")
assert_eq(independently_authored.to_dict(), surface.to_dict(), "floor zero is authored rather than seed-generated")
func test_transition_cache_keeps_mutated_floor_state_and_arrival_cells_across_return_trip() -> void:
var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)
var run := RunStateType.new("B5-cache-contract")
var transitions := FloorTransitionType.new(catalog)
var surface := transitions.initial_floor(run)
var first_descent := transitions.transition(run, surface, surface.exit)
var first_floor: FloorState = first_descent["floor"]
first_floor.chests[Vector2i(3, 3)] = {"opened": true}
first_floor.enemies.clear()
first_floor.events.append({"id": "first-floor-event", "resolved": true})
var ascent := transitions.transition(run, first_floor, first_floor.entrance)
var returned_surface: FloorState = ascent["floor"]
returned_surface.map.set_cell(Vector2i(4, 7), GridMapStateType.CellKind.FLOOR)
var second_descent := transitions.transition(run, returned_surface, returned_surface.exit)
var cached_first_floor: FloorState = second_descent["floor"]
assert_same(returned_surface, surface)
assert_eq(ascent["arrival"], surface.exit)
assert_same(cached_first_floor, first_floor)
assert_eq(second_descent["arrival"], first_floor.entrance)
assert_eq(cached_first_floor.chests[Vector2i(3, 3)], {"opened": true})
assert_eq(cached_first_floor.enemies.size(), 0)
assert_true(cached_first_floor.events.has({"id": "first-floor-event", "resolved": true}))
assert_eq(surface.map.get_cell(Vector2i(4, 7)), GridMapStateType.CellKind.FLOOR)
func _seed_value(seed: String, floor_number: int) -> int:
var value := 0
for index in range(seed.length()):
value = (value * 31 + seed.unicode_at(index)) % 2147483647
return value + floor_number
@@ -0,0 +1 @@
uid://20vpsc7klakm