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 run.ensure_floor_deadline(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 run.ensure_floor_deadline(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)