files-game/table/bandwidth_strip.gd
2026-10-01 23:01:56 -04:00

120 lines
3.4 KiB
GDScript

class_name BandwidthStrip
extends Node3D
## A player's Bandwidth as a neon loading bar along their edge of the mat (ui-plan.md §A5),
## mirrored small in the HUD (BandwidthBar). Bandwidth has no cap, so the scale is fixed: 20 cells
## of 1 Gb, with ticks at the Firewall costs, and the whole bar glows past 20 instead of clipping.
## For you, dimmer cells run on to what salvaging your hand could reach.
##
## Local axes: x runs along the bar (0 Gb at -x, as its owner sees it), z toward its owner.
const CELLS := 20
const LENGTH := 11.0
const CELL_DEPTH := 0.34
const LED_SHADER := preload("res://table/led.gdshader")
static var _material: ShaderMaterial
var color := Color("#56b4e9")
var gb := 0
var reach := 0
var _cells: Array[MeshInstance3D] = []
var _ticks: Array[Label3D] = []
var _value: Label3D
var _shown := 0.0
var _tween: Tween
var _pulse := 0.0
func _ready() -> void:
if _material == null:
_material = ShaderMaterial.new()
_material.shader = LED_SHADER
var step := LENGTH / CELLS
var mesh := BoxMesh.new()
mesh.size = Vector3(step * 0.8, 0.03, CELL_DEPTH)
for i in CELLS:
var c := MeshInstance3D.new()
c.mesh = mesh
c.material_override = _material
c.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
c.position = Vector3(-LENGTH / 2.0 + step * (i + 0.5), 0.015, 0)
add_child(c)
_cells.append(c)
var title := _label("BANDWIDTH", 40, Style.MUTED)
title.position = Vector3(-LENGTH / 2.0 - 0.15, 0.01, 0)
title.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
_value = _label("0 Gb", 64, color)
_value.position = Vector3(LENGTH / 2.0 + 0.2, 0.01, 0)
_value.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
_draw()
func _label(text: String, size: int, c: Color) -> Label3D:
var l := Label3D.new()
l.text = text
l.font = Style.body_font(700)
l.font_size = size
l.pixel_size = 0.004
l.rotation.x = -PI / 2
l.modulate = c
l.outline_size = 0
add_child(l)
return l
## The Firewall costs, marked under the bar.
func set_ticks(costs: Array) -> void:
for t in _ticks:
t.queue_free()
_ticks = []
var step := LENGTH / CELLS
for cost in costs:
if cost <= 0 or cost >= CELLS:
continue
var t := _label(str(cost), 30, Color(Style.SCREEN, 0.6))
t.position = Vector3(-LENGTH / 2.0 + step * cost, 0.01, CELL_DEPTH / 2.0 + 0.14)
_ticks.append(t)
## Shows `value` Gb (and the ghost to `reach_to`), filling cell by cell on the motion curve after
## `delay` seconds.
func set_value(value: int, reach_to: int, c: Color, delay := 0.0) -> void:
color = c
reach = maxi(reach_to, value)
_value.modulate = c
_value.text = "%d Gb" % value
if value == gb:
_draw()
return
gb = value
if _tween != null:
_tween.kill()
if Motion.instant() or not is_inside_tree():
_shown = value
_draw()
return
_tween = create_tween()
if delay > 0.0:
_tween.tween_interval(Motion.secs(delay))
_tween.tween_method(func(x):
_shown = x
_draw(), _shown, float(value), Motion.secs(0.6)).set_trans(Tween.TRANS_EXPO).set_ease(Tween.EASE_OUT)
func _process(delta: float) -> void:
if gb > CELLS:
_pulse = fmod(_pulse + delta, 2.0)
_draw()
func _draw() -> void:
var lit := roundi(_shown)
var over := gb > CELLS
var k := 0.5 + 0.5 * sin(_pulse * PI)
for i in _cells.size():
var v := Color(0, 0, 0, 0)
if i < lit:
v = Color(color.lerp(Color.WHITE, 0.2 * float(i) / CELLS), 2.6 + k * 1.4 if over else 1.7)
elif i < reach:
v = Color(color, 0.09)
_cells[i].set_instance_shader_parameter("led", v)