class_name GameState extends RefCounted const TurnResolverType = preload("res://scripts/domain/turn_resolver.gd") const InteractionResolverType = preload("res://scripts/domain/interaction_resolver.gd") const TrapEngineType = preload("res://scripts/domain/world/trap_engine.gd") const SpecialInteractionsType = preload("res://scripts/domain/world/special_interactions.gd") const CompanionStateType = preload("res://scripts/domain/entities/companion_state.gd") var player: ActorState # E4 keeps the selected pet run-scoped rather than making it a floor entity. var companion: CompanionState # Legacy renderer/game seams alias the active floor aggregate until their consumers # move to FloorState directly in later world slices. var floor_state: FloorState var enemies: Dictionary[StringName, ActorState] var map: GridMapState var visible_cells: Dictionary[Vector2i, bool] = {} var explored_cells: Dictionary[Vector2i, bool] = {} var content_catalog: ContentCatalog var exit_reached := false func _init(new_map: GridMapState, new_player: ActorState, new_enemies: Dictionary[StringName, ActorState]) -> void: player = new_player floor_state = FloorState.new(new_map) floor_state.enemies = new_enemies map = floor_state.map enemies = floor_state.enemies explored_cells = floor_state.explored_cells update_exploration() func bind_run_state(run_state: RunState) -> void: run_state.set_floor_state(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 content_catalog != null: var special_target := SpecialInteractionsType.prompt_target(self) if not special_target.is_empty(): return special_target if exit_reached: return &"exit_reached" if map.get_cell(player.cell) == GridMapState.CellKind.STAIRS: return &"stairs" if map.get_cell(player.cell) == GridMapState.CellKind.BED: return &"bed" for direction in InteractionResolverType.TARGET_OFFSETS: if map.get_cell(player.cell + direction) == GridMapState.CellKind.CHEST: return &"chest" for direction in InteractionResolverType.TARGET_OFFSETS: if map.get_cell(player.cell + direction) == GridMapState.CellKind.CLOSED_DOOR: return &"closed_door" return &"" func try_player_action(direction: Vector2i) -> Array[TurnResult]: # A visible secret can be found before crossing its wall; discovery consumes # the attempted exploration action so the revelation is journaled distinctly. if content_catalog != null: var secret_results := SpecialInteractionsType.reveal_visible(self, visible_cells) if not secret_results.is_empty(): update_exploration() return secret_results var results := TurnResolverType.resolve_player_action(self, direction) if content_catalog != null and results.size() == 1 and results[0].kind == &"moved": var discoveries := TrapEngineType.discover_visible(floor_state, player, visible_cells, content_catalog) if not discoveries.is_empty(): results = discoveries + results update_exploration() return results update_exploration() return results func try_interact() -> Array[TurnResult]: if content_catalog != null: var special_target := SpecialInteractionsType.prompt_target(self) if special_target == &"trap": var trap_cell := SpecialInteractionsType.trap_cell_at_or_adjacent(self) var outcome := TrapEngineType.disarm(floor_state, player, trap_cell, content_catalog) var kind: StringName = &"trap_disarmed" if bool(outcome.get("success", false)) else &"trap_disarm_failed" var message := "You disarm the trap." if bool(outcome.get("success", false)) else "You fail to disarm the trap." return [TurnResult.new(player.id, kind, player.cell, trap_cell, &"", 0, message, {"trap_id": outcome.get("trap_id", &"")})] if special_target in [&"shrine", &"quest_board"]: return [SpecialInteractionsType.resolve(self)] 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): for x in range(origin.x - radius, origin.x + radius + 1): var cell := Vector2i(x, y) if game_map.is_inside(cell) and origin.distance_to(cell) <= radius and _has_line_of_sight(game_map, origin, cell): visible[cell] = true return visible func update_exploration() -> void: visible_cells = compute_visible_cells(map, player.cell, 6) for cell in visible_cells: explored_cells[cell] = true func _has_line_of_sight(game_map: GridMapState, origin: Vector2i, target: Vector2i) -> bool: var current := origin var delta_x: int = abs(target.x - origin.x) var step_x: int = 1 if origin.x < target.x else -1 var delta_y: int = -abs(target.y - origin.y) var step_y: int = 1 if origin.y < target.y else -1 var error: int = delta_x + delta_y while current != target: var doubled_error: int = 2 * error if doubled_error >= delta_y: error += delta_y current.x += step_x if doubled_error <= delta_x: error += delta_x current.y += step_y if current != target and game_map.is_opaque(current): return false return true