249 lines
7.1 KiB
GDScript
249 lines
7.1 KiB
GDScript
extends Control
|
||
|
||
signal back_pressed
|
||
|
||
const BoardScene = preload("res://scenes/Board.tscn")
|
||
const DEFAULT_STATUS := "One guest per room, floor and corridor.\nTap = X · Hold or right-click = check in."
|
||
|
||
var levels: Array = []
|
||
var level_index := 0
|
||
var started_at := 0
|
||
var elapsed_ms := 0
|
||
var win_shown := false
|
||
var current_level_id := ""
|
||
|
||
var board: Board
|
||
var level_label: Label
|
||
var timer_label: Label
|
||
var status_label: Label
|
||
var win_detail: Label
|
||
var overlay: ColorRect
|
||
|
||
|
||
func _ready() -> void:
|
||
levels = Levels.load_all()
|
||
_build_ui()
|
||
set_process(true)
|
||
|
||
|
||
func _build_ui() -> void:
|
||
var margin := MarginContainer.new()
|
||
margin.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
||
margin.add_theme_constant_override("margin_left", 20)
|
||
margin.add_theme_constant_override("margin_right", 20)
|
||
margin.add_theme_constant_override("margin_top", 18)
|
||
margin.add_theme_constant_override("margin_bottom", 18)
|
||
add_child(margin)
|
||
|
||
var column := VBoxContainer.new()
|
||
column.add_theme_constant_override("separation", 10)
|
||
margin.add_child(column)
|
||
|
||
# Top bar
|
||
var top := HBoxContainer.new()
|
||
top.add_theme_constant_override("separation", 10)
|
||
column.add_child(top)
|
||
|
||
var back := UI.button("‹", false, 44)
|
||
back.custom_minimum_size = Vector2(48, 44)
|
||
back.add_theme_font_size_override("font_size", 30)
|
||
back.pressed.connect(_on_back)
|
||
top.add_child(back)
|
||
|
||
level_label = UI.label("Room 1", 20, Palette.MUTED)
|
||
level_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
|
||
level_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
top.add_child(level_label)
|
||
|
||
timer_label = UI.label("00:00", 18, Palette.MUTED)
|
||
timer_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
|
||
timer_label.custom_minimum_size = Vector2(72, 0)
|
||
top.add_child(timer_label)
|
||
|
||
# Board
|
||
board = BoardScene.instantiate() as Board
|
||
board.custom_minimum_size = Vector2(320, 320)
|
||
board.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
board.size_flags_vertical = Control.SIZE_EXPAND_FILL
|
||
board.changed.connect(_on_board_changed)
|
||
board.solved.connect(_on_solved)
|
||
board.mistake.connect(_on_mistake)
|
||
column.add_child(board)
|
||
|
||
# Status
|
||
status_label = UI.label(DEFAULT_STATUS, 17, Palette.MUTED)
|
||
status_label.custom_minimum_size = Vector2(0, 56)
|
||
status_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
column.add_child(status_label)
|
||
|
||
# Bottom actions
|
||
var actions := HBoxContainer.new()
|
||
actions.add_theme_constant_override("separation", 8)
|
||
column.add_child(actions)
|
||
|
||
var undo := UI.button("Undo", false, 52)
|
||
undo.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
undo.pressed.connect(_on_undo)
|
||
actions.add_child(undo)
|
||
|
||
var restart := UI.button("Restart", false, 52)
|
||
restart.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
restart.pressed.connect(_on_restart)
|
||
actions.add_child(restart)
|
||
|
||
var hint := UI.button("Hint", true, 52)
|
||
hint.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
hint.pressed.connect(_on_hint)
|
||
actions.add_child(hint)
|
||
|
||
_build_win_overlay()
|
||
|
||
|
||
func _build_win_overlay() -> void:
|
||
overlay = ColorRect.new()
|
||
overlay.color = Color(0.18, 0.12, 0.08, 0.48)
|
||
overlay.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
||
overlay.mouse_filter = Control.MOUSE_FILTER_STOP
|
||
overlay.visible = false
|
||
add_child(overlay)
|
||
|
||
var center := CenterContainer.new()
|
||
center.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
||
overlay.add_child(center)
|
||
|
||
var panel := UI.panel(Palette.CREAM, 34, 28)
|
||
panel.custom_minimum_size = Vector2(420, 0)
|
||
center.add_child(panel)
|
||
|
||
var box := VBoxContainer.new()
|
||
box.add_theme_constant_override("separation", 10)
|
||
panel.add_child(box)
|
||
|
||
var portrait_center := CenterContainer.new()
|
||
box.add_child(portrait_center)
|
||
var portrait := _make_win_portrait()
|
||
portrait.custom_minimum_size = Vector2(170, 170)
|
||
portrait_center.add_child(portrait)
|
||
|
||
box.add_child(UI.label("Everyone checked in!", 34))
|
||
win_detail = UI.label("Room 1 · 00:00", 18, Palette.MUTED)
|
||
box.add_child(win_detail)
|
||
box.add_child(UI.label("Sweet dreams, little guests.", 18, Palette.MUTED))
|
||
|
||
var next := UI.button("Next room", true, 56)
|
||
box.add_child(next)
|
||
next.pressed.connect(_on_next)
|
||
|
||
|
||
func _make_win_portrait() -> Control:
|
||
if ResourceLoader.exists("res://art/crumb_head.png"):
|
||
var texture_rect := TextureRect.new()
|
||
texture_rect.texture = load("res://art/crumb_head.png")
|
||
texture_rect.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
||
texture_rect.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
||
return texture_rect
|
||
return RatPortrait.new()
|
||
|
||
|
||
func start_level(index: int) -> void:
|
||
if levels.is_empty():
|
||
status_label.text = "No rooms found — check data/levels.json."
|
||
status_label.add_theme_color_override("font_color", Palette.RED)
|
||
return
|
||
|
||
level_index = clampi(index, 0, levels.size() - 1)
|
||
var level: Dictionary = levels[level_index]
|
||
current_level_id = str(level.get("id", ""))
|
||
board.set_level(level)
|
||
|
||
level_label.text = "Room %d of %d" % [level_index + 1, levels.size()]
|
||
started_at = Time.get_ticks_msec()
|
||
elapsed_ms = 0
|
||
win_shown = false
|
||
overlay.visible = false
|
||
if level.get("clues", []).size() > 0:
|
||
status_label.text = "Gold-ringed guests are already checked in.\nTap = X · Hold or right-click = check in."
|
||
else:
|
||
status_label.text = DEFAULT_STATUS
|
||
status_label.add_theme_color_override("font_color", Palette.MUTED)
|
||
|
||
|
||
func _process(_delta: float) -> void:
|
||
timer_label.visible = bool(SaveGame.get_setting("show_timer"))
|
||
if not visible or win_shown or started_at == 0:
|
||
return
|
||
elapsed_ms = Time.get_ticks_msec() - started_at
|
||
timer_label.text = _format_time(elapsed_ms)
|
||
|
||
|
||
func _format_time(ms: int) -> String:
|
||
var total: int = int(ms / 1000.0)
|
||
var minutes: int = total / 60
|
||
var seconds: int = total % 60
|
||
return "%02d:%02d" % [minutes, seconds]
|
||
|
||
|
||
func _on_board_changed() -> void:
|
||
if win_shown:
|
||
return
|
||
status_label.text = DEFAULT_STATUS
|
||
status_label.add_theme_color_override("font_color", Palette.MUTED)
|
||
|
||
|
||
func _on_mistake() -> void:
|
||
status_label.text = "That room's already taken — undo or try another."
|
||
status_label.add_theme_color_override("font_color", Palette.RED)
|
||
|
||
|
||
func _on_solved() -> void:
|
||
if win_shown:
|
||
return
|
||
win_shown = true
|
||
status_label.text = "Everyone checked in."
|
||
status_label.add_theme_color_override("font_color", Palette.INK)
|
||
win_detail.text = "Room %d · %s" % [level_index + 1, _format_time(elapsed_ms)]
|
||
|
||
SaveGame.complete_room(current_level_id, level_index + 1)
|
||
|
||
await get_tree().create_timer(0.45).timeout
|
||
if win_shown:
|
||
overlay.visible = true
|
||
|
||
|
||
func _on_undo() -> void:
|
||
win_shown = false
|
||
overlay.visible = false
|
||
board.undo()
|
||
|
||
|
||
func _on_restart() -> void:
|
||
win_shown = false
|
||
overlay.visible = false
|
||
started_at = Time.get_ticks_msec()
|
||
elapsed_ms = 0
|
||
board.restart()
|
||
|
||
|
||
func _on_hint() -> void:
|
||
if board.hint():
|
||
if not win_shown:
|
||
status_label.text = "Crumb found a guest who was waiting."
|
||
status_label.add_theme_color_override("font_color", Palette.MUTED)
|
||
else:
|
||
if not win_shown:
|
||
status_label.text = "Every guest is already checked in."
|
||
status_label.add_theme_color_override("font_color", Palette.MUTED)
|
||
|
||
|
||
func _on_next() -> void:
|
||
if level_index + 1 < levels.size():
|
||
start_level(level_index + 1)
|
||
else:
|
||
_on_back()
|
||
|
||
|
||
func _on_back() -> void:
|
||
win_shown = false
|
||
overlay.visible = false
|
||
back_pressed.emit()
|