299 lines
11 KiB
GDScript
299 lines
11 KiB
GDScript
class_name Game
|
|
extends Node2D
|
|
|
|
signal game_started(settings: RunSettings, run_state: RunState)
|
|
|
|
const REFERENCE_VIEWPORT := Vector2i(1280, 720)
|
|
const TILE_SIZE := Vector2i(32, 32)
|
|
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
|
const ActorStateType = preload("res://scripts/domain/actor_state.gd")
|
|
const GameStateType = preload("res://scripts/domain/game_state.gd")
|
|
const RunStateType = preload("res://scripts/domain/run/run_state.gd")
|
|
const RunSettingsType = preload("res://scripts/domain/run/run_settings.gd")
|
|
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")
|
|
@onready var event_presenter: EventPresenter = $EventPresenter
|
|
@onready var map_renderer: MapRenderer = $World/MapRenderer
|
|
@onready var player_view: ActorView = $World/Player
|
|
@onready var enemies: Node2D = $World/Enemies
|
|
@onready var camera: Camera2D = $Camera2D
|
|
@onready var hud: GameHud = $HUD
|
|
@onready var audio_library = $AudioLibrary
|
|
var game_state: GameState
|
|
var run_state: RunState
|
|
var enemy_views: Dictionary[StringName, ActorView] = {}
|
|
var last_turn_results: Array[TurnResult] = []
|
|
var action_router: ActionRouter
|
|
var pending_run_settings: RunSettings
|
|
var pending_player_name := "Sans-Nom"
|
|
var pending_base_archetype: StringName = &"vanguard"
|
|
|
|
func configure_new_run(settings: RunSettings, player_name: String, base_archetype: StringName) -> void:
|
|
pending_run_settings = settings
|
|
pending_player_name = player_name
|
|
pending_base_archetype = base_archetype
|
|
|
|
func _ready() -> void:
|
|
game_state = _create_initial_state()
|
|
_initialize_run_state()
|
|
action_router = ActionRouterType.new(self)
|
|
event_presenter.set_actor_lookup(_get_actor_view)
|
|
event_presenter.event_presented.connect(_on_event_presented)
|
|
map_renderer.set_map(game_state.map)
|
|
_sync_map_renderer_visibility()
|
|
player_view.set_actor(game_state.player)
|
|
_configure_camera()
|
|
_sync_enemy_views()
|
|
hud.refresh(game_state)
|
|
audio_library.set_ambience_context("exploration")
|
|
var capture_mode := _get_capture_mode()
|
|
if not capture_mode.is_empty():
|
|
_capture_visual_check(capture_mode)
|
|
if pending_run_settings != null:
|
|
call_deferred("_emit_game_started")
|
|
|
|
func _emit_game_started() -> void:
|
|
game_started.emit(run_state.settings, run_state)
|
|
|
|
func _initialize_run_state() -> void:
|
|
var settings := pending_run_settings
|
|
if settings == null:
|
|
settings = RunSettingsType.new("demo-run")
|
|
run_state = RunStateType.new(settings.seed, settings)
|
|
run_state.player_name = pending_player_name
|
|
run_state.base_archetype = pending_base_archetype
|
|
if pending_run_settings != null:
|
|
var game_started := GameEventType.new(
|
|
run_state.next_turn(),
|
|
run_state.current_floor,
|
|
&"game_started",
|
|
{
|
|
"seed": settings.seed,
|
|
"difficulty_id": settings.difficulty_id,
|
|
"permadeath": settings.permadeath,
|
|
"pet_enabled": settings.pet_enabled,
|
|
"player_name": run_state.player_name,
|
|
"base_archetype": run_state.base_archetype,
|
|
},
|
|
"%s begins a new run." % run_state.player_name,
|
|
&"player",
|
|
&"",
|
|
game_state.player.cell
|
|
)
|
|
run_state.append_events([game_started])
|
|
|
|
func _get_capture_mode() -> String:
|
|
var arguments := OS.get_cmdline_user_args()
|
|
if "--capture-exit" in arguments:
|
|
return "exit"
|
|
if "--capture" in arguments:
|
|
return "initial"
|
|
return ""
|
|
|
|
func _capture_visual_check(capture_mode: String) -> void:
|
|
if capture_mode == "exit":
|
|
_prepare_exit_capture_state()
|
|
await get_tree().process_frame
|
|
await get_tree().process_frame
|
|
await RenderingServer.frame_post_draw
|
|
var filename := "visual-check-exit.png" if capture_mode == "exit" else "visual-check.png"
|
|
var capture_path := ProjectSettings.globalize_path("res://artifacts/" + filename)
|
|
DirAccess.make_dir_recursive_absolute(capture_path.get_base_dir())
|
|
var image := get_viewport().get_texture().get_image()
|
|
var result := image.save_png(capture_path)
|
|
get_tree().quit(0 if result == OK else 1)
|
|
|
|
func _prepare_exit_capture_state() -> void:
|
|
game_state.map.set_cell(Vector2i(4, 10), GridMapStateType.CellKind.FLOOR)
|
|
game_state.map.set_cell(Vector2i(4, 7), GridMapStateType.CellKind.FLOOR)
|
|
game_state.player.cell = Vector2i(18, 4)
|
|
_process_interact()
|
|
_sync_camera_to_player()
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
var action := action_router.action_from_input(event)
|
|
if action != null:
|
|
action_router.submit(action)
|
|
|
|
func submit_test_action(direction: Vector2i) -> Array[TurnResult]:
|
|
action_router.submit(PlayerActionType.new(&"move", direction))
|
|
return last_turn_results
|
|
|
|
func submit_test_interact() -> Array[TurnResult]:
|
|
action_router.submit(PlayerActionType.new(&"interact"))
|
|
return last_turn_results
|
|
|
|
func submit_player_action(action: PlayerAction) -> Array[GameEvent]:
|
|
match action.kind:
|
|
&"move":
|
|
last_turn_results = _process_action(action.direction)
|
|
return run_state.recent_events(last_turn_results.size())
|
|
&"interact":
|
|
last_turn_results = _process_interact()
|
|
return run_state.recent_events(last_turn_results.size())
|
|
_:
|
|
last_turn_results = []
|
|
return _record_unavailable_action(action)
|
|
|
|
func _record_unavailable_action(action: PlayerAction) -> Array[GameEvent]:
|
|
var event := GameEventType.new(
|
|
run_state.next_turn(),
|
|
run_state.current_floor,
|
|
&"action_unavailable",
|
|
{"action": action.kind},
|
|
"Action unavailable: %s." % action.kind,
|
|
&"player",
|
|
&"",
|
|
game_state.player.cell
|
|
)
|
|
var events: Array[GameEvent] = [event]
|
|
run_state.append_events(events)
|
|
event_presenter.enqueue(events)
|
|
event_presenter.present_next()
|
|
hud.refresh(game_state, run_state.recent_events(run_state.log.size()))
|
|
return events
|
|
|
|
func get_game_state() -> GameState:
|
|
return game_state
|
|
|
|
func get_run_state() -> RunState:
|
|
return run_state
|
|
|
|
func get_actor_cell_rect(actor_id: StringName) -> Rect2:
|
|
if actor_id == &"player":
|
|
return player_view.get_cell_rect()
|
|
if enemy_views.has(actor_id):
|
|
return enemy_views[actor_id].get_cell_rect()
|
|
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)
|
|
|
|
func _refresh_hud_for_context() -> Array[TurnResult]:
|
|
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)
|
|
|
|
# TurnResult remains the public prototype seam. RunState is the authoritative
|
|
# journal: results from one resolved player action are converted and appended once,
|
|
# and every resulting GameEvent shares that action's turn number.
|
|
func _resolve_player_action(results: Array[TurnResult], sync_camera: bool) -> Array[TurnResult]:
|
|
_sync_map_renderer_visibility()
|
|
player_view.refresh()
|
|
if sync_camera:
|
|
_sync_camera_to_player()
|
|
_dispatch_audio(results)
|
|
var events := _record_events(results)
|
|
event_presenter.enqueue(events)
|
|
event_presenter.present_next()
|
|
hud.refresh(game_state, run_state.recent_events(run_state.log.size()))
|
|
return results
|
|
|
|
func _record_events(results: Array[TurnResult]) -> Array[GameEvent]:
|
|
var events: Array[GameEvent] = []
|
|
if results.is_empty():
|
|
return events
|
|
var turn := run_state.next_turn()
|
|
for result in results:
|
|
events.append(GameEventType.new(
|
|
turn,
|
|
run_state.current_floor,
|
|
result.kind,
|
|
{
|
|
"from_cell": result.from_cell,
|
|
"to_cell": result.to_cell,
|
|
"damage": result.damage,
|
|
},
|
|
result.message,
|
|
result.actor_id,
|
|
result.target_id,
|
|
result.to_cell
|
|
))
|
|
run_state.append_events(events)
|
|
return events
|
|
|
|
func _dispatch_audio(results: Array[TurnResult]) -> void:
|
|
for result in results:
|
|
match result.kind:
|
|
&"melee":
|
|
audio_library.set_ambience_context("combat")
|
|
audio_library.play_sfx("attack_given")
|
|
&"enemy_melee":
|
|
audio_library.set_ambience_context("combat")
|
|
audio_library.play_sfx("attack_received")
|
|
&"enemy_defeated":
|
|
audio_library.play_sfx("death")
|
|
|
|
func _on_event_presented(event: GameEvent) -> void:
|
|
if event.kind == &"enemy_defeated":
|
|
_sync_enemy_views()
|
|
|
|
func _get_actor_view(actor_id: StringName) -> ActorView:
|
|
if actor_id == &"player":
|
|
return player_view
|
|
return enemy_views.get(actor_id) as ActorView
|
|
|
|
func _create_initial_state() -> GameState:
|
|
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 player := ActorStateType.new(&"player", Vector2i(4, 12), 12, 3)
|
|
var goblin := ActorStateType.new(&"goblin", Vector2i(24, 8), 6, 2)
|
|
return GameStateType.new(map, player, {&"goblin": goblin})
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
func _configure_camera() -> void:
|
|
var map_size := game_state.map.size * TILE_SIZE
|
|
camera.limit_left = $World.position.x
|
|
camera.limit_top = $World.position.y
|
|
camera.limit_right = $World.position.x + map_size.x
|
|
camera.limit_bottom = $World.position.y + map_size.y
|
|
_sync_camera_to_player()
|
|
|
|
func _sync_camera_to_player() -> void:
|
|
camera.position = $World.position + Vector2(game_state.player.cell * TILE_SIZE)
|
|
|
|
func _sync_map_renderer_visibility() -> void:
|
|
map_renderer.set_visibility(game_state.visible_cells, game_state.explored_cells)
|
|
|
|
func _sync_enemy_views() -> void:
|
|
for enemy_id in enemy_views.keys():
|
|
if not game_state.enemies.has(enemy_id):
|
|
enemy_views[enemy_id].queue_free()
|
|
enemy_views.erase(enemy_id)
|
|
for enemy_id in game_state.enemies:
|
|
if not enemy_views.has(enemy_id):
|
|
var view := ActorViewType.new()
|
|
view.name = String(enemy_id).capitalize()
|
|
view.texture = preload("res://assets/dcss/goblin.png")
|
|
enemies.add_child(view)
|
|
enemy_views[enemy_id] = view
|
|
enemy_views[enemy_id].set_actor(game_state.enemies[enemy_id])
|