class_name Main extends Control ## The app window: top bar, felt board and result tray. Owns the board data, ## the Play/Build mode, roll animation, and editing actions with undo. enum Mode { PLAY, BUILD } const HISTORY_SIZE := 6 const SAVE_DELAY := 0.6 const HINTS := { Mode.PLAY: "Click a die to roll. Results flow down the pipes.", Mode.BUILD: "Type to edit. Drag from a ● to another table to lay a pipe. Click a pipe to remove it.", } var data: TowerBoard var mode: Mode = Mode.PLAY var cards: Dictionary = {} # table id -> TableCard var lit_entries: Dictionary = {} # "tid:eid" -> true var lit_pipes: Dictionary = {} # "tid:eid>to" -> true var current: Dictionary = {} var history: Array = [] var rng := RandomNumberGenerator.new() var board: BoardView var tray: ResultTray var _token := 0 var _pulse_tween: Tween var _link_from: Dictionary = {} var _drop_card: TableCard var _hint: Label var _play_button: Button var _build_button: Button var _build_tools: Control var _collapse_all: Button var _toast: PanelContainer var _toast_label: Label var _toast_undo: Button var _toast_timer: Timer var _save_timer: Timer var _undo_snapshot := "" var _saving_enabled := true func _ready() -> void: rng.randomize() theme = Style.app_theme() var args := OS.get_cmdline_user_args() if args.has("--sample") or _arg_value(args, "--screenshot") != "": _saving_enabled = false if _saving_enabled: data = TowerBoard.load_saved() if data == null: data = SampleBoard.create() _build_ui() _rebuild_cards() _seed_session() _update_collapse_all() # Let the cards lay out before fitting them to the window. await get_tree().process_frame await get_tree().process_frame board.fit() _run_cli(args) func _exit_tree() -> void: if _saving_enabled and data: data.save() # ---------- UI ---------- func _build_ui() -> void: set_anchors_and_offsets_preset(PRESET_FULL_RECT) var root := VBoxContainer.new() root.set_anchors_and_offsets_preset(PRESET_FULL_RECT) root.add_theme_constant_override("separation", 0) add_child(root) root.add_child(_build_top_bar()) board = BoardView.new() board.setup(self) root.add_child(board) tray = ResultTray.new() tray.setup(self) root.add_child(tray) _build_toast() _save_timer = Timer.new() _save_timer.one_shot = true _save_timer.wait_time = SAVE_DELAY _save_timer.timeout.connect(func() -> void: if _saving_enabled: data.save() ) add_child(_save_timer) resized.connect(_place_toast) func _build_top_bar() -> Control: var bar := PanelContainer.new() var sb := Style.box(Style.FELT_DEEP, 0, [16, 10, 16, 10]) sb.border_width_bottom = 1 sb.border_color = Color(1, 1, 1, 0.07) bar.add_theme_stylebox_override("panel", sb) var h := HBoxContainer.new() h.add_theme_constant_override("separation", 16) bar.add_child(h) var brand := HBoxContainer.new() brand.add_theme_constant_override("separation", 9) var mark := Control.new() mark.custom_minimum_size = Vector2(20, 24) mark.size_flags_vertical = SIZE_SHRINK_CENTER mark.draw.connect(func() -> void: Icons.draw_tower(mark, Vector2(0, 0), 20.0 / 22.0, Style.ON_FELT, Style.BRASS)) brand.add_child(mark) var title := Style.make_label("TableTower", Style.display(), 23, Style.ON_FELT) title.size_flags_vertical = SIZE_SHRINK_CENTER brand.add_child(title) h.add_child(brand) var seg := PanelContainer.new() seg.add_theme_stylebox_override("panel", Style.box(Color(0, 0, 0, 0.28), 9, [3, 3, 3, 3])) seg.size_flags_vertical = SIZE_SHRINK_CENTER var seg_row := HBoxContainer.new() seg_row.add_theme_constant_override("separation", 0) seg.add_child(seg_row) var group := ButtonGroup.new() _play_button = _seg_button("Play", group) _build_button = _seg_button("Build", group) _play_button.button_pressed = true _play_button.pressed.connect(func() -> void: set_mode(Mode.PLAY)) _build_button.pressed.connect(func() -> void: set_mode(Mode.BUILD)) seg_row.add_child(_play_button) seg_row.add_child(_build_button) h.add_child(seg) _hint = Style.make_label(HINTS[mode], Style.body(), 15, Style.ON_FELT_MUTED) _hint.size_flags_horizontal = SIZE_EXPAND_FILL _hint.size_flags_vertical = SIZE_SHRINK_CENTER _hint.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS _hint.custom_minimum_size = Vector2(60, 0) h.add_child(_hint) var tools := HBoxContainer.new() tools.add_theme_constant_override("separation", 6) _build_tools = tools var add := Style.bar_button("+ Table", true) add.pressed.connect(add_table) tools.add_child(add) var reset := Style.bar_button("Reset sample") reset.pressed.connect(reset_sample) tools.add_child(reset) tools.visible = false h.add_child(tools) _collapse_all = Style.bar_button("Collapse all") _collapse_all.tooltip_text = "Collapse or expand every table at once" _collapse_all.pressed.connect(func() -> void: set_all_collapsed(_any_expanded())) h.add_child(_collapse_all) var zoom := HBoxContainer.new() zoom.add_theme_constant_override("separation", 6) var zout := Style.bar_button("−") zout.tooltip_text = "Zoom out" zout.custom_minimum_size = Vector2(34, 0) zout.pressed.connect(func() -> void: board.set_zoom(board.zoom / 1.15)) var zfit := Style.bar_button("Fit") zfit.tooltip_text = "Show every table" zfit.pressed.connect(func() -> void: board.fit()) var zin := Style.bar_button("+") zin.tooltip_text = "Zoom in" zin.custom_minimum_size = Vector2(34, 0) zin.pressed.connect(func() -> void: board.set_zoom(board.zoom * 1.15)) for b in [zout, zfit, zin]: zoom.add_child(b) h.add_child(zoom) return bar func _seg_button(text: String, group: ButtonGroup) -> Button: var b := Button.new() b.text = text b.toggle_mode = true b.button_group = group b.add_theme_font_override("font", Style.body_bold()) b.add_theme_font_size_override("font_size", 16) var normal := Style.box(Color(0, 0, 0, 0), 7, [16, 6, 16, 6]) var hover := Style.box(Color(1, 1, 1, 0.06), 7, [16, 6, 16, 6]) var pressed := Style.box(Style.CARD, 7, [16, 6, 16, 6]) Style.skin_button(b, normal, hover, pressed, Style.ON_FELT_MUTED, Style.ON_FELT, Style.INK) return b func _build_toast() -> void: _toast = PanelContainer.new() var sb := Style.box(Style.CARD, 10, [16, 9, 10, 9]) sb.shadow_color = Color(0, 0, 0, 0.4) sb.shadow_size = 18 sb.shadow_offset = Vector2(0, 8) _toast.add_theme_stylebox_override("panel", sb) var h := HBoxContainer.new() h.add_theme_constant_override("separation", 12) _toast.add_child(h) _toast_label = Style.make_label("", Style.body(), 15, Style.INK) _toast_label.size_flags_vertical = SIZE_SHRINK_CENTER h.add_child(_toast_label) _toast_undo = Button.new() _toast_undo.text = "Undo" _toast_undo.add_theme_font_override("font", Style.body_bold()) _toast_undo.add_theme_font_size_override("font_size", 15) var un := Style.box(Style.FELT, 7, [12, 5, 12, 5]) var uh := Style.box(Style.FELT_HOVER, 7, [12, 5, 12, 5]) Style.skin_button(_toast_undo, un, uh, uh, Style.BRASS) _toast_undo.pressed.connect(_undo) h.add_child(_toast_undo) _toast.hide() add_child(_toast) _toast_timer = Timer.new() _toast_timer.one_shot = true _toast_timer.wait_time = 5.0 _toast_timer.timeout.connect(func() -> void: _toast.hide() _undo_snapshot = "" ) add_child(_toast_timer) func toast(message: String, snapshot: String = "") -> void: _undo_snapshot = snapshot _toast_label.text = message _toast_undo.visible = snapshot != "" _toast.show() _toast.reset_size() _place_toast() _toast_timer.start() func _place_toast() -> void: if _toast and _toast.visible: _toast.reset_size() _toast.position = Vector2((size.x - _toast.size.x) / 2.0, size.y - _toast.size.y - 20.0) func _undo() -> void: if _undo_snapshot == "": return data.load_json(_undo_snapshot) _undo_snapshot = "" _toast.hide() _rebuild_cards() mark_dirty() func _snapshot() -> String: return data.to_json() # ---------- Cards ---------- func card_for(id: String) -> TableCard: return cards.get(id) func is_entry_lit(tid: String, eid: String) -> bool: return lit_entries.has(Roller.entry_key(tid, eid)) func _rebuild_cards() -> void: for c in board.cards.get_children(): board.cards.remove_child(c) c.queue_free() cards.clear() for t in data.tables: var card := TableCard.new() board.cards.add_child(card) card.setup(self, t) cards[t.id] = card _update_collapse_all() func _refresh_lit() -> void: for card in cards.values(): (card as TableCard).refresh_lit() func set_mode(m: Mode) -> void: mode = m _play_button.set_pressed_no_signal(m == Mode.PLAY) _build_button.set_pressed_no_signal(m == Mode.BUILD) _hint.text = HINTS[m] _build_tools.visible = m == Mode.BUILD board.pipes.hover_key = "" for card in cards.values(): (card as TableCard).rebuild() func mark_dirty() -> void: if _saving_enabled: _save_timer.start() _update_collapse_all() func _any_expanded() -> bool: for t in data.tables: if not t.collapsed: return true return false func _update_collapse_all() -> void: if _collapse_all: _collapse_all.text = "Collapse all" if _any_expanded() else "Expand all" # ---------- Editing actions ---------- func toggle_collapse(tid: String) -> void: var t := data.get_table(tid) if t == null: return t.collapsed = not t.collapsed card_for(tid).rebuild() mark_dirty() func set_all_collapsed(collapsed: bool) -> void: for t in data.tables: if t.collapsed != collapsed: t.collapsed = collapsed card_for(t.id).rebuild() mark_dirty() func add_table() -> void: var center := board.screen_to_world(board.size / 2.0) var t := RollTable.new(TowerBoard.new_id("t"), "New table") t.position = Vector2(maxf(20.0, roundf(center.x - 128.0)), maxf(20.0, roundf(center.y - 120.0))) for n in range(1, 5): t.entries.append(TableEntry.new(TowerBoard.new_id("e"), "Result %d" % n)) data.tables.append(t) var card := TableCard.new() board.cards.add_child(card) card.setup(self, t) cards[t.id] = card mark_dirty() func reset_sample() -> void: var snap := _snapshot() data = SampleBoard.create() lit_entries.clear() lit_pipes.clear() _rebuild_cards() mark_dirty() await get_tree().process_frame await get_tree().process_frame board.fit() toast("Board reset to the sample tables", snap) func add_entry(tid: String) -> void: var t := data.get_table(tid) var e := TableEntry.new(TowerBoard.new_id("e"), "Result %d" % (t.entries.size() + 1)) if t.is_ranged(): var hi := 0 for x in t.entries: hi = maxi(hi, x.range_hi) e.has_range = true e.range_lo = mini(t.die_size(), hi + 1) e.range_hi = e.range_lo e.text = "New range" t.entries.append(e) t.collapsed = false card_for(tid).rebuild() mark_dirty() func delete_entry(tid: String, eid: String) -> void: var t := data.get_table(tid) var e := t.find_entry(eid) if e == null: return var snap := _snapshot() t.entries.erase(e) card_for(tid).rebuild() mark_dirty() toast("Deleted “%s”" % e.text, snap) func delete_table(tid: String) -> void: var t := data.get_table(tid) if t == null: return var snap := _snapshot() data.remove_table(tid) var card := card_for(tid) cards.erase(tid) card.queue_free() for c in cards.values(): (c as TableCard).rebuild() mark_dirty() toast("Deleted %s" % t.name, snap) func remove_pipe(key: String) -> void: var from := key.get_slice(">", 0) var to := key.get_slice(">", 1) var t := data.get_table(from.get_slice(":", 0)) var target := data.get_table(to) if t == null: return var e := t.find_entry(from.get_slice(":", 1)) if e == null: return var snap := _snapshot() e.pipes.erase(to) lit_pipes.erase(key) board.pipes.hover_key = "" card_for(t.id).rebuild() mark_dirty() toast("Removed pipe from “%s” to %s" % [e.text, target.name if target else to], snap) func start_link(tid: String, eid: String) -> void: _link_from = {"tid": tid, "eid": eid} func update_link() -> void: var over := _card_under_mouse() if over != _drop_card: if is_instance_valid(_drop_card): _drop_card.set_drop_target(false) _drop_card = over if _drop_card: _drop_card.set_drop_target(true) func finish_link() -> void: var over := _card_under_mouse() if is_instance_valid(_drop_card): _drop_card.set_drop_target(false) _drop_card = null if _link_from.is_empty() or over == null: _link_from = {} return var t := data.get_table(_link_from["tid"]) var e := t.find_entry(_link_from["eid"]) if t else null _link_from = {} if e == null or e.pipes.has(over.table.id): return # Each range on a ranged table takes a single pipe, so a new one replaces the old. var replaced: RollTable = null if t.is_ranged(): if not e.pipes.is_empty(): replaced = data.get_table(e.pipes[0]) e.pipes.clear() e.pipes.append(over.table.id) card_for(t.id).rebuild() mark_dirty() var msg := "“%s” now rolls on %s" % [e.text, over.table.name] if replaced: msg += " instead of %s" % replaced.name toast(msg) func _card_under_mouse() -> TableCard: var gp := get_global_mouse_position() var kids := board.cards.get_children() for i in range(kids.size() - 1, -1, -1): var card := kids[i] as TableCard if card and card.get_global_rect().has_point(gp): return card return null # ---------- Rolling ---------- func start_roll(tid: String) -> void: var t := data.get_table(tid) if t == null: return _archive_current() _token += 1 var my := _token if _pulse_tween and _pulse_tween.is_valid(): _pulse_tween.kill() board.pipes.pulse_key = "" for c in cards.values(): (c as TableCard).set_rolling(false) (c as TableCard).clear_flick() var tree := Roller.roll(data, tid, rng) lit_entries.clear() lit_pipes.clear() _refresh_lit() current = {"root": tid, "root_name": t.name, "time": Time.get_unix_time_from_system(), "steps": [], "all": Roller.flatten(tree, []), "done": false} tray.render() await _play_node(tree, my) if my == _token: current["steps"] = current["all"] current["done"] = true tray.render() func _play_node(n: Dictionary, my: int) -> void: if my != _token or n.is_empty(): return var t: RollTable = n["table"] var stopped := n.has("stop") var card := card_for(t.id) if not stopped and card: card.set_rolling(true) for k in 8: card = card_for(t.id) if card == null: break if t.collapsed: card.set_more_text("Rolling… %s" % t.fmt(rng.randi_range(1, t.die_size()))) else: card.flick_random() await get_tree().create_timer((40.0 + k * 16.0) / 1000.0).timeout if my != _token: return card = card_for(t.id) if card: card.clear_flick() card.set_rolling(false) if not stopped: var e: TableEntry = n["entry"] lit_entries[Roller.entry_key(t.id, e.id)] = true _refresh_lit() if card: card.reveal(e.id) (current["steps"] as Array).append(Roller.step_of(n)) tray.render() for c in n["children"]: var key := Roller.pipe_key(t.id, (n["entry"] as TableEntry).id, (c["table"] as RollTable).id) lit_pipes[key] = true await _pulse(key, my) await _play_node(c, my) func _pulse(key: String, my: int) -> void: var length := board.pipes.path_length(key) if length <= 0.0 or my != _token: return board.pipes.pulse_key = key board.pipes.pulse_t = 0.0 _pulse_tween = create_tween().set_trans(Tween.TRANS_QUAD).set_ease(Tween.EASE_IN_OUT) _pulse_tween.tween_property(board.pipes, "pulse_t", 1.0, clampf(length * 0.8, 260.0, 650.0) / 1000.0) await _pulse_tween.finished if board.pipes.pulse_key == key: board.pipes.pulse_key = "" func _archive_current() -> void: if current.is_empty() or current.get("archived", false): return current["archived"] = true var rec := current.duplicate() rec["steps"] = current["all"] history.push_front(rec) if history.size() > HISTORY_SIZE: history.resize(HISTORY_SIZE) ## A game already under way: a couple of earlier rolls, and a current roll that ## runs through the d100 travel table into the collapsed trinket table. func _seed_session() -> void: var now := Time.get_unix_time_from_system() for seed_roll in [["monsters", 9], ["loot", 4]]: if data.get_table(seed_roll[0]): var tree := Roller.roll(data, seed_roll[0], rng) var steps := Roller.flatten(tree, []) history.push_front({"root": seed_roll[0], "root_name": data.get_table(seed_roll[0]).name, "time": now - seed_roll[1] * 60, "steps": steps, "all": steps, "archived": true}) var start := "wilderness" if data.get_table("wilderness") else (data.tables[0].id if not data.tables.is_empty() else "") if start == "": tray.render() return var best := Roller.roll(data, start, rng) for i in 400: if Roller.reaches(best, "trinkets"): break best = Roller.roll(data, start, rng) var all := Roller.flatten(best, []) current = {"root": start, "root_name": data.get_table(start).name, "time": now - 60, "steps": all, "all": all, "done": true} Roller.collect_lit(best, lit_entries, lit_pipes) _refresh_lit() tray.render() # ---------- Command line (used for screenshots and checks) ---------- # godot --path . -- --screenshot=/tmp/shot.png [--build] [--expand-all] [--collapse-all] # [--roll=] [--shot-delay=] func _arg_value(args: PackedStringArray, name: String) -> String: for a in args: if a.begins_with(name + "="): return a.substr(name.length() + 1) return "" func _run_cli(args: PackedStringArray) -> void: if args.has("--build"): set_mode(Mode.BUILD) if args.has("--expand-all"): set_all_collapsed(false) if args.has("--collapse-all"): set_all_collapsed(true) var roll := _arg_value(args, "--roll") if roll != "": start_roll(roll) var shot := _arg_value(args, "--screenshot") if shot == "": return var delay := _arg_value(args, "--shot-delay") await get_tree().create_timer(delay.to_float() if delay != "" else (2.5 if roll != "" else 0.8)).timeout var img := get_viewport().get_texture().get_image() img.save_png(shot) print("Saved screenshot to ", shot) get_tree().quit()