Port the project to a 360x640 canvas with the redesigned lobby, settings sheet, and refreshed UI.
397 lines
12 KiB
GDScript
397 lines
12 KiB
GDScript
extends Control
|
|
|
|
signal back_pressed
|
|
signal guest_book_pressed
|
|
signal settings_pressed
|
|
|
|
const BoardScene = preload("res://scenes/Board.tscn")
|
|
const DEFAULT_STATUS := "One guest per room, row and column."
|
|
|
|
var levels: Array = []
|
|
var level_index := 0
|
|
var started_at := 0
|
|
var elapsed_ms := 0
|
|
var win_shown := false
|
|
var current_level_id := ""
|
|
var ui_paused := false
|
|
|
|
var board: Board
|
|
var level_label: Label
|
|
var guest_label: Label
|
|
var timer_label: Label
|
|
var status_label: Label
|
|
var win_detail: Label
|
|
var overlay: ColorRect
|
|
var rule_strip: HBoxContainer
|
|
var legend_row: HBoxContainer
|
|
var legend_chips: Array = []
|
|
var new_guest_box: HBoxContainer
|
|
var new_guest_label: Label
|
|
var new_guest_portrait: TextureRect
|
|
|
|
|
|
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", 12)
|
|
margin.add_theme_constant_override("margin_right", 12)
|
|
margin.add_theme_constant_override("margin_top", 10)
|
|
margin.add_theme_constant_override("margin_bottom", 10)
|
|
add_child(margin)
|
|
|
|
var column := VBoxContainer.new()
|
|
column.add_theme_constant_override("separation", 6)
|
|
margin.add_child(column)
|
|
|
|
# Top bar
|
|
var top := HBoxContainer.new()
|
|
top.add_theme_constant_override("separation", 8)
|
|
column.add_child(top)
|
|
|
|
var back := UI.round_button("res://assets/icons/arrow-left.svg", 44)
|
|
back.pressed.connect(_on_back)
|
|
top.add_child(back)
|
|
|
|
var center := VBoxContainer.new()
|
|
center.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
center.add_theme_constant_override("separation", 0)
|
|
top.add_child(center)
|
|
|
|
level_label = UI.label("Room 1", 18)
|
|
center.add_child(level_label)
|
|
|
|
var sub := HBoxContainer.new()
|
|
sub.alignment = BoxContainer.ALIGNMENT_CENTER
|
|
sub.add_theme_constant_override("separation", 8)
|
|
center.add_child(sub)
|
|
|
|
guest_label = UI.label("0 / 8", 11, Palette.MUTED)
|
|
sub.add_child(guest_label)
|
|
|
|
timer_label = UI.label("00:00", 11, Palette.MUTED)
|
|
sub.add_child(timer_label)
|
|
|
|
var gear := UI.round_button("res://assets/icons/settings.svg", 44)
|
|
gear.pressed.connect(func() -> void: settings_pressed.emit())
|
|
top.add_child(gear)
|
|
|
|
# Rule chips, first three rooms
|
|
rule_strip = HBoxContainer.new()
|
|
rule_strip.alignment = BoxContainer.ALIGNMENT_CENTER
|
|
rule_strip.add_theme_constant_override("separation", 4)
|
|
column.add_child(rule_strip)
|
|
rule_strip.add_child(_rule_chip("1 per room", "region"))
|
|
rule_strip.add_child(_rule_chip("1 per row/col", "row"))
|
|
rule_strip.add_child(_rule_chip("No touching", "touch"))
|
|
|
|
# Room legend: one guest per colour
|
|
legend_row = HBoxContainer.new()
|
|
legend_row.alignment = BoxContainer.ALIGNMENT_CENTER
|
|
legend_row.add_theme_constant_override("separation", 3)
|
|
column.add_child(legend_row)
|
|
|
|
# Board
|
|
board = BoardScene.instantiate() as Board
|
|
board.custom_minimum_size = Vector2(300, 300)
|
|
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, 12, Palette.MUTED)
|
|
status_label.custom_minimum_size = Vector2(0, 26)
|
|
column.add_child(status_label)
|
|
|
|
# Actions
|
|
var actions := HBoxContainer.new()
|
|
actions.add_theme_constant_override("separation", 8)
|
|
column.add_child(actions)
|
|
|
|
var undo := UI.button("Undo", false, 48)
|
|
undo.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
undo.add_theme_font_size_override("font_size", 17)
|
|
undo.pressed.connect(_on_undo)
|
|
actions.add_child(undo)
|
|
|
|
var hint := UI.button("Hint", true, 48)
|
|
hint.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
hint.add_theme_font_size_override("font_size", 17)
|
|
hint.pressed.connect(_on_hint)
|
|
actions.add_child(hint)
|
|
|
|
_build_win_overlay()
|
|
|
|
|
|
func _rule_chip(text: String, mode: String) -> PanelContainer:
|
|
var panel := UI.panel(Palette.PAPER, 12, 5)
|
|
panel.custom_minimum_size = Vector2(102, 42)
|
|
var row := HBoxContainer.new()
|
|
row.add_theme_constant_override("separation", 3)
|
|
panel.add_child(row)
|
|
|
|
var diagram := MiniDiagram.new()
|
|
diagram.mode = mode
|
|
diagram.custom_minimum_size = Vector2(30, 30)
|
|
row.add_child(diagram)
|
|
|
|
var label := UI.label(text, 9, Palette.MUTED)
|
|
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
|
|
label.custom_minimum_size = Vector2(58, 0)
|
|
row.add_child(label)
|
|
|
|
return panel
|
|
|
|
|
|
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, 26, 18)
|
|
panel.custom_minimum_size = Vector2(320, 0)
|
|
center.add_child(panel)
|
|
|
|
var box := VBoxContainer.new()
|
|
box.add_theme_constant_override("separation", 6)
|
|
panel.add_child(box)
|
|
|
|
var portrait_center := CenterContainer.new()
|
|
box.add_child(portrait_center)
|
|
var portrait := _make_win_portrait()
|
|
portrait.custom_minimum_size = Vector2(110, 110)
|
|
portrait_center.add_child(portrait)
|
|
|
|
box.add_child(UI.heading("Everyone checked in!", 26))
|
|
win_detail = UI.label("Room 1 · 00:00", 13, Palette.MUTED)
|
|
box.add_child(win_detail)
|
|
box.add_child(UI.label("Sweet dreams, little guests.", 12, Palette.MUTED))
|
|
|
|
new_guest_box = HBoxContainer.new()
|
|
new_guest_box.alignment = BoxContainer.ALIGNMENT_CENTER
|
|
new_guest_box.add_theme_constant_override("separation", 6)
|
|
new_guest_box.visible = false
|
|
box.add_child(new_guest_box)
|
|
|
|
new_guest_portrait = TextureRect.new()
|
|
new_guest_portrait.custom_minimum_size = Vector2(42, 42)
|
|
new_guest_portrait.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
|
new_guest_portrait.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
|
new_guest_box.add_child(new_guest_portrait)
|
|
|
|
new_guest_label = UI.label("", 11, Palette.INK)
|
|
new_guest_label.custom_minimum_size = Vector2(190, 0)
|
|
new_guest_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
new_guest_box.add_child(new_guest_label)
|
|
|
|
var next := UI.button("Next room", true, 48)
|
|
next.add_theme_font_size_override("font_size", 17)
|
|
box.add_child(next)
|
|
next.pressed.connect(_on_next)
|
|
|
|
var book := UI.button("Guest Book", false, 42)
|
|
book.add_theme_font_size_override("font_size", 15)
|
|
box.add_child(book)
|
|
book.pressed.connect(func() -> void: guest_book_pressed.emit())
|
|
|
|
|
|
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 yet."
|
|
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" % (level_index + 1)
|
|
started_at = Time.get_ticks_msec()
|
|
elapsed_ms = 0
|
|
win_shown = false
|
|
overlay.visible = false
|
|
new_guest_box.visible = false
|
|
rule_strip.visible = level_index < 3
|
|
status_label.text = "Gold rings are already in." if level.get("clues", []).size() > 0 else DEFAULT_STATUS
|
|
status_label.add_theme_color_override("font_color", Palette.MUTED)
|
|
_refresh_legend()
|
|
_update_guest_count()
|
|
|
|
|
|
func _refresh_legend() -> void:
|
|
for child in legend_row.get_children():
|
|
child.free()
|
|
legend_chips.clear()
|
|
if board.region_rats.is_empty():
|
|
return
|
|
|
|
for region in range(board.n):
|
|
var id := str(board.region_rats[region])
|
|
var color: Color = Palette.REGION_COLORS[region % Palette.REGION_COLORS.size()]
|
|
var chip := PanelContainer.new()
|
|
chip.custom_minimum_size = Vector2(32, 32)
|
|
var style := StyleBoxFlat.new()
|
|
style.bg_color = color
|
|
style.corner_radius_top_left = 9
|
|
style.corner_radius_top_right = 9
|
|
style.corner_radius_bottom_left = 9
|
|
style.corner_radius_bottom_right = 9
|
|
style.border_width_left = 1
|
|
style.border_width_right = 1
|
|
style.border_width_top = 1
|
|
style.border_width_bottom = 1
|
|
style.border_color = Color(1, 1, 1, 0.55)
|
|
chip.add_theme_stylebox_override("panel", style)
|
|
chip.tooltip_text = str(Guests.by_id(id).get("name", id))
|
|
legend_row.add_child(chip)
|
|
|
|
var texture := TextureRect.new()
|
|
texture.texture = board.region_textures.get(region)
|
|
texture.custom_minimum_size = Vector2(28, 28)
|
|
texture.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
|
texture.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
|
texture.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
chip.add_child(texture)
|
|
|
|
legend_chips.append(chip)
|
|
|
|
|
|
func _update_legend() -> void:
|
|
for region in range(legend_chips.size()):
|
|
var chip := legend_chips[region]
|
|
var solved := false
|
|
for r in board.n:
|
|
for c in board.n:
|
|
if int(board.cells[r][c]) == Board.RAT and int(board.regions[r][c]) == region:
|
|
solved = true
|
|
break
|
|
if solved:
|
|
break
|
|
var style: StyleBoxFlat = chip.get_theme_stylebox("panel")
|
|
if style != null:
|
|
style.border_color = Palette.GOLD if solved else Color(1, 1, 1, 0.55)
|
|
style.border_width_left = 3 if solved else 1
|
|
style.border_width_right = 3 if solved else 1
|
|
style.border_width_top = 3 if solved else 1
|
|
style.border_width_bottom = 3 if solved else 1
|
|
|
|
|
|
func _update_guest_count() -> void:
|
|
guest_label.text = "%d / %d guests" % [Rules.count_rats(board.cells), board.n]
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
timer_label.visible = bool(SaveGame.get_setting("show_timer"))
|
|
if ui_paused or 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 set_ui_paused(value: bool) -> void:
|
|
ui_paused = value
|
|
|
|
|
|
func restart_room() -> void:
|
|
start_level(level_index)
|
|
|
|
|
|
func _on_board_changed() -> void:
|
|
if win_shown:
|
|
return
|
|
status_label.text = DEFAULT_STATUS
|
|
status_label.add_theme_color_override("font_color", Palette.MUTED)
|
|
_update_guest_count()
|
|
_update_legend()
|
|
|
|
|
|
func _on_mistake() -> void:
|
|
status_label.text = "That room's taken — undo or try again."
|
|
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)]
|
|
_update_legend()
|
|
|
|
var unlocked_id := SaveGame.complete_room(current_level_id, level_index + 1)
|
|
if unlocked_id != "":
|
|
var guest := Guests.by_id(unlocked_id)
|
|
new_guest_label.text = "New guest: %s the %s" % [str(guest.get("name", "")), str(guest.get("role", ""))]
|
|
new_guest_portrait.texture = load(Guests.portrait(unlocked_id, "thumbs"))
|
|
new_guest_box.visible = true
|
|
else:
|
|
new_guest_box.visible = false
|
|
|
|
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_hint() -> void:
|
|
if board.hint():
|
|
if not win_shown:
|
|
status_label.text = "Crumb found a guest."
|
|
status_label.add_theme_color_override("font_color", Palette.MUTED)
|
|
else:
|
|
if not win_shown:
|
|
status_label.text = "Everyone 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()
|