TableTower/ui/pipe_layer.gd
RobinFiredrake 60c2d03147 Build the pipe-board app in Godot
Tables are cards on a felt board, joined by copper pipes that carry a
roll from one table to the next. Play mode rolls with a flicker and a
bead down each pipe; Build mode edits names, results, ranges and pipes.

- core/: data model (RollTable, TableEntry, TowerBoard), sample board
  with a d100 table of 100 trinkets and a weighted d100 range table,
  and a Roller with a depth limit
- ui/: board with pan and zoom, pipe and port layers, collapsible table
  cards, result tray with earlier rolls, undo toasts, and a Collapse all
  / Expand all button
- assets/fonts/: Rokkitt and Atkinson Hyperlegible (SIL OFL)
- tests/test_roller.gd: headless checks for the model and roller

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:05:41 -06:00

115 lines
3.4 KiB
GDScript

class_name PipeLayer
extends Control
## Draws the copper pipes between cards, lit pipes in brass, and the glowing bead
## that travels down a pipe during a roll. Sits behind the cards.
const SAMPLES := 40
var main: Main
var hover_key: String = ""
var pulse_key: String = ""
var pulse_t: float = 0.0
## pipe key -> polyline in world coordinates, from the last frame.
var _paths: Dictionary = {}
func _init() -> void:
mouse_filter = MOUSE_FILTER_IGNORE
# Controls only draw inside their own rect, so cover the whole world.
size = Vector2(BoardView.WORLD_SIZE, BoardView.WORLD_SIZE)
func _process(_delta: float) -> void:
queue_redraw()
static func curve(a: Vector2, b: Vector2) -> PackedVector2Array:
var dx := maxf(70.0, absf(b.x - a.x) / 2.0)
var c1 := a + Vector2(dx, 0)
var c2 := b - Vector2(dx, 0)
var pts := PackedVector2Array()
for i in SAMPLES + 1:
pts.append(a.bezier_interpolate(c1, c2, b, float(i) / SAMPLES))
return pts
func _draw() -> void:
_paths.clear()
var lit_list: Array = []
var build := main.mode == Main.Mode.BUILD
for t in main.data.tables:
var card := main.card_for(t.id)
if card == null:
continue
for e in t.entries:
for to in e.pipes:
var target := main.card_for(to)
if target == null:
continue
var a := card.anchor_for(e.id)
if a.is_empty():
continue
var key := Roller.pipe_key(t.id, e.id, to)
var pts := curve(a["pos"], target.inlet_point())
_paths[key] = pts
if main.lit_pipes.has(key):
lit_list.append(pts)
else:
_draw_pipe(pts, false, build and key == hover_key)
# Lit pipes go on top so the path of the last roll reads clearly.
for pts in lit_list:
_draw_pipe(pts, true, false)
if pulse_key != "" and _paths.has(pulse_key):
var p := point_along(_paths[pulse_key], pulse_t)
draw_circle(p, 13.0, Color(Style.BRASS, 0.3))
draw_circle(p, 9.0, Color(Style.BRASS, 0.5))
draw_circle(p, 7.0, Style.PULSE)
func _draw_pipe(pts: PackedVector2Array, lit: bool, hovered: bool) -> void:
if lit:
draw_polyline(pts, Color(Style.BRASS, 0.18), 20.0, true)
draw_polyline(pts, Style.BRASS_DARK if lit else Style.COPPER_DARK, 11.0, true)
var inner := Style.BRASS if lit else Style.COPPER
if hovered:
inner = Style.DANGER
draw_polyline(pts, inner, 6.0, true)
draw_set_transform(Vector2(0, -1.5))
draw_polyline(pts, Color(1.0, 0.886, 0.745, 0.4), 1.5, true)
draw_set_transform(Vector2.ZERO)
func path_length(key: String) -> float:
if not _paths.has(key):
return 0.0
var pts: PackedVector2Array = _paths[key]
var total := 0.0
for i in range(1, pts.size()):
total += pts[i - 1].distance_to(pts[i])
return total
static func point_along(pts: PackedVector2Array, t: float) -> Vector2:
if pts.size() < 2:
return pts[0] if pts.size() == 1 else Vector2.ZERO
var total := 0.0
for i in range(1, pts.size()):
total += pts[i - 1].distance_to(pts[i])
var want := clampf(t, 0.0, 1.0) * total
for i in range(1, pts.size()):
var seg := pts[i - 1].distance_to(pts[i])
if want <= seg and seg > 0.0:
return pts[i - 1].lerp(pts[i], want / seg)
want -= seg
return pts[pts.size() - 1]
## The pipe under a world-space point, or "" if none is within `tolerance`.
func pipe_at(p: Vector2, tolerance: float) -> String:
for key in _paths:
var pts: PackedVector2Array = _paths[key]
for i in range(1, pts.size()):
if Geometry2D.get_closest_point_to_segment(p, pts[i - 1], pts[i]).distance_to(p) <= tolerance:
return key
return ""