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

272 lines
8.9 KiB
GDScript

class_name FirewallPort
extends Node3D
## A gateway as an ethernet port (gameplay-plan.md §B4): a socket printed on the mat, and a
## Firewall as a plug seated in it, with gold contacts (the cards' edge-connector motif: hardware
## plugs in here) and a row of LEDs, one per Firewall level the rules allow, lit up to its level.
## Firewalls have no Health, so the animations are plug in, swap up and unplug.
##
## Local axes: z runs along the line through the gateway, x across it.
const TILE_SHADER := preload("res://table/tile.gdshader")
const LED_SHADER := preload("res://table/led.gdshader")
const SOCKET := Vector2(0.62, 0.46)
const PLUG := Vector3(0.52, 0.2, 0.36)
const LED_SIZE := 0.1
const LED_STEP := 0.17
## Where the plug sits seated, and where it comes in from.
const SEATED := Vector3(0, PLUG.y / 2.0 + 0.004, 0)
const ABOVE := Vector3(0, 1.5, 0.25)
static var _socket_material: ShaderMaterial
static var _led_material: ShaderMaterial
static var _housing_material: StandardMaterial3D
static var _clip_material: StandardMaterial3D
static var _gold_material: StandardMaterial3D
## The level shown (0: no Firewall) and how many LEDs there are.
var level := 0
var levels := 3
var _socket: MeshInstance3D
var _tab: MeshInstance3D
var _plug: Node3D
var _leds: Array[MeshInstance3D] = []
var _tween: Tween
var _glow_tween: Tween
var _mark := Color(0, 0, 0, 0)
var _glow := 0.0
func _ready() -> void:
# A bit larger than life, so the LEDs read from across the table and in the top view.
scale = Vector3.ONE * 1.3
_make_materials()
_socket = _decal(SOCKET, 0.06)
_socket.position.y = 0.005
# The latch slot, on the socket's far side.
_tab = _decal(Vector2(0.24, 0.1), 0.025)
_tab.position = Vector3(0, 0.005, SOCKET.y / 2.0 + 0.04)
_plug = Node3D.new()
_plug.name = "Plug"
_plug.visible = false
add_child(_plug)
_plug.add_child(_box(PLUG, Vector3.ZERO, _housing_material))
_plug.add_child(_box(Vector3(0.2, 0.05, 0.16), Vector3(0, PLUG.y / 2.0 + 0.02, PLUG.z / 2.0 - 0.07), _clip_material))
for i in 8:
var x := (i - 3.5) * 0.052
_plug.add_child(_box(Vector3(0.03, 0.012, 0.1), Vector3(x, PLUG.y / 2.0 + 0.006, -PLUG.z / 2.0 + 0.06), _gold_material))
_build_leds()
_apply_socket()
func _make_materials() -> void:
if _socket_material != null:
return
_socket_material = ShaderMaterial.new()
_socket_material.shader = TILE_SHADER
_socket_material.set_shader_parameter("size", SOCKET)
_socket_material.set_shader_parameter("chamfer", 0.06)
_socket_material.set_shader_parameter("edge_width", 0.03)
_socket_material.set_shader_parameter("inner_line", 0.6)
_socket_material.render_priority = 0
_led_material = ShaderMaterial.new()
_led_material.shader = LED_SHADER
_housing_material = StandardMaterial3D.new()
_housing_material.albedo_color = Color(0.05, 0.13, 0.15, 0.78)
_housing_material.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
_housing_material.roughness = 0.12
_housing_material.metallic_specular = 0.8
_housing_material.rim_enabled = true
_housing_material.rim = 0.6
_housing_material.emission_enabled = true
_housing_material.emission = Style.ACCENT_DK
_housing_material.emission_energy_multiplier = 0.6
_clip_material = StandardMaterial3D.new()
_clip_material.albedo_color = Color(0.03, 0.06, 0.07)
_clip_material.roughness = 0.3
_gold_material = StandardMaterial3D.new()
_gold_material.albedo_color = Color("#ffd24a")
_gold_material.metallic = 1.0
_gold_material.roughness = 0.25
_gold_material.emission_enabled = true
_gold_material.emission = Color("#ffb02e")
_gold_material.emission_energy_multiplier = 0.12
## A flat chamfered decal on the mat, drawn with the tile shader.
func _decal(size: Vector2, chamfer: float) -> MeshInstance3D:
var m := MeshInstance3D.new()
var plane := PlaneMesh.new()
plane.size = size
m.mesh = plane
var mat: ShaderMaterial = _socket_material
if size != SOCKET:
mat = _socket_material.duplicate()
mat.set_shader_parameter("size", size)
mat.set_shader_parameter("chamfer", chamfer)
mat.set_shader_parameter("inner_line", 0.0)
m.material_override = mat
m.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
add_child(m)
return m
func _box(size: Vector3, at: Vector3, mat: Material) -> MeshInstance3D:
var m := MeshInstance3D.new()
var b := BoxMesh.new()
b.size = size
m.mesh = b
m.material_override = mat
m.position = at
return m
func _build_leds() -> void:
for l in _leds:
l.queue_free()
_leds = []
var mesh := BoxMesh.new()
mesh.size = Vector3(LED_SIZE, 0.035, LED_SIZE)
for i in levels:
var led := MeshInstance3D.new()
led.mesh = mesh
led.material_override = _led_material
led.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
led.position = Vector3((i - (levels - 1) / 2.0) * LED_STEP, PLUG.y / 2.0 + 0.018, 0.02)
_plug.add_child(led)
_leds.append(led)
_light(level, false)
## Shows Firewall `to` (0 for none) out of `max_levels`, animating from the shown level after
## `delay` seconds unless `animate` is false.
func set_level(to: int, max_levels: int, animate := true, delay := 0.0) -> void:
if max_levels != levels:
levels = maxi(1, max_levels)
_build_leds()
if to == level:
return
var from := level
level = to
if _tween != null:
_tween.kill()
if not animate or Motion.instant() or not is_inside_tree():
_plug.visible = to > 0
_plug.position = SEATED
_plug.scale = Vector3.ONE
_light(to, false)
return
_tween = create_tween()
if delay > 0.0:
_tween.tween_interval(Motion.secs(delay))
if from == 0:
_plug_in()
elif to == 0:
_unplug()
else:
_swap(to > from)
## The plug rushes in from above, hangs over the socket while it lines up, and snaps in; then
## the LEDs come on one by one.
func _plug_in() -> void:
_tween.tween_callback(func():
_plug.visible = true
_plug.scale = Vector3.ONE
_plug.position = ABOVE
_plug.rotation = Vector3(0.5, 0.35, 0)
_light(0, false))
var tw := _tween
tw.set_parallel(false)
Motion.rush_in(tw, _plug, "position", Vector3(0, 0.42, 0.02), 0.22)
tw.parallel()
Motion.rush_in(tw, _plug, "rotation", Vector3(0.08, 0, 0), 0.22)
Motion.drift(tw, _plug, "position", Vector3(0, 0.34, 0), 0.28)
tw.parallel()
Motion.drift(tw, _plug, "rotation", Vector3.ZERO, 0.28)
Motion.rush_out(tw, _plug, "position", SEATED, 0.1)
tw.tween_callback(func(): pulse(Style.firewall_color(level, levels), 2.6))
for i in level:
tw.tween_callback(_light.bind(i + 1, true))
tw.tween_interval(Motion.secs(0.08))
## Up a level: the plug lifts a little and reseats, and the new LEDs flash on. Down: they go out.
func _swap(up: bool) -> void:
var tw := _tween
Motion.rush_in(tw, _plug, "position", SEATED + Vector3(0, 0.14, 0), 0.12)
Motion.rush_out(tw, _plug, "position", SEATED, 0.1)
tw.tween_callback(func():
_light(level, true)
if up:
pulse(Style.firewall_color(level, levels), 2.0))
## Cleared: the LEDs go out, the plug lifts out of the socket and away.
func _unplug() -> void:
var tw := _tween
tw.tween_callback(_light.bind(0, true))
Motion.rush_in(tw, _plug, "position", SEATED + Vector3(0, 0.3, 0), 0.16)
Motion.drift(tw, _plug, "position", SEATED + Vector3(0, 0.42, 0), 0.22)
Motion.rush_out(tw, _plug, "position", ABOVE, 0.18)
tw.parallel()
Motion.rush_out(tw, _plug, "scale", Vector3.ONE * 0.4, 0.18)
tw.tween_callback(func(): _plug.visible = false)
## Lights the first `n` LEDs in the level's color; the rest stay dark.
func _light(n: int, flash: bool) -> void:
var c := Style.firewall_color(maxi(1, level), levels)
for i in _leds.size():
var on := i < n
_leds[i].set_instance_shader_parameter("led", Color(c, 3.4 if flash and i == n - 1 else 1.7) if on else Color(0.12, 0.16, 0.17, 0.0))
## A glow around the socket that fades out: a Firewall going in, or the gateway being picked.
func pulse(color: Color, strength: float) -> void:
if _glow_tween != null:
_glow_tween.kill()
_glow = strength
_mark = color
_apply_socket()
if Motion.instant():
_glow = 0.0
_apply_socket()
return
_glow_tween = create_tween()
_glow_tween.tween_method(func(g):
_glow = g
_apply_socket(), strength, 0.0, Motion.secs(0.7)).set_trans(Tween.TRANS_EXPO).set_ease(Tween.EASE_OUT)
## Highlights the socket for a mark (a legal source or target), or clears it.
func set_mark(color: Color, strength: float) -> void:
_mark = color
_glow = strength
_apply_socket()
func _apply_socket() -> void:
if _socket == null:
return
var edge := Color(Style.ACCENT, 0.75) if level == 0 else Color(Style.firewall_color(level, levels), 0.9)
if _glow > 0.0:
edge = _mark
for d in [_socket, _tab]:
d.set_instance_shader_parameter("fill_color", Color(Style.BG, 0.92))
d.set_instance_shader_parameter("edge_color", edge)
d.set_instance_shader_parameter("glow", _glow)
## Whether an animation is playing.
func busy() -> bool:
return _tween != null and _tween.is_valid() and _tween.is_running()
func finish() -> void:
if busy():
_tween.kill()
_plug.visible = level > 0
_plug.position = SEATED
_plug.rotation = Vector3.ZERO
_plug.scale = Vector3.ONE
_light(level, false)