files-game/present/presenter.gd
2026-10-01 23:01:56 -04:00

264 lines
10 KiB
GDScript

class_name Presenter
extends Node
## Acts the game out (gameplay-plan.md §B5). Each change to the match comes with the engine's
## events for it (s.events, filtered for you); the presenter lays them out on a timeline, one beat
## after another, and turns them into how cards travel (motion styles the table applies in its
## next sync) and cues that need the new positions (pop-ups, flashes, the turn banner).
##
## The table still snaps to the view in that sync: every card's goal is its real spot, and the
## events only choose the way there and when. So a missed or misdrawn animation can never leave
## the table showing the wrong state (godot-plan.md §3). This lives outside engine/ and match/,
## which use no nodes.
## Seconds each kind of event holds the timeline at normal speed before the next one starts.
## Kinds that come in runs (draws, discards) have short beats, so a run fans out.
const BEATS := {
"turn": 0.3, "drew": 0.11, "hand": 0.1, "discard": 0.16, "install": 0.42, "placed": 0.34,
"moved": 0.4, "recalled": 0.32, "flipped": 0.28, "combat": 0.32, "rolled": 0.62, "rerolled": 0.5,
"damaged": 0.3, "destroyed": 0.45, "captured": 0.5, "firewall": 0.55, "bandwidth": 0.0, "counter": 0.12,
"stack": 0.75, "pile": 0.12,
}
## An opponent's card held up for you to read takes longer than your own.
const REVEAL_BEAT := 0.85
## The longest one change may take; longer ones are squeezed to fit.
const MAX_BATCH := 3.4
## Extra time after the last beat for its animation to land.
const TAIL := 0.45
var table: GameTable
var hud: Hud
## When the show for the last change ends, in engine seconds.
var _until := 0.0
## Cues waiting for the sync: Callables taking no arguments.
var _cues: Array[Callable] = []
## Before the table syncs to `after`: plans the show for `events` (as `me` sees them). `before` is
## the view the table shows now ({} for a new game).
func stage(events: Array, before: Dictionary, after: Dictionary, me: int) -> void:
_cues = []
if events.is_empty() or Motion.instant():
return
var plan := []
var t := 0.0
for ev in events:
var beat: float = BEATS.get(ev.k, 0.0)
if (ev.k == "install" or ev.k == "discard") and ev.get("p", me) != me and _from_hidden(ev):
beat = REVEAL_BEAT
plan.append({"ev": ev, "at": t})
t += beat
var squeeze := 1.0 if t <= MAX_BATCH else MAX_BATCH / t
var bot_draws: Array[float] = []
# What earlier events in this change did, for the later ones: cards that went on the stack,
# Anomalies destroyed.
var seen := {"stacked": {}, "destroyed": {}}
for step in plan:
var at: float = step.at * squeeze
_stage_one(step.ev, at, before, after, me, bot_draws, seen)
_deal_bot_hand(before, after, me, bot_draws)
_until = Time.get_ticks_msec() / 1000.0 + Motion.secs(t * squeeze + TAIL)
## Whether `ev` brings out a card the viewer couldn't see: from a hand or the deck.
static func _from_hidden(ev: Dictionary) -> bool:
var from: String = ev.get("from", "hand")
return from in ["hand", "deck", ""]
func _stage_one(ev: Dictionary, at: float, before: Dictionary, after: Dictionary, me: int, bot_draws: Array[float], seen: Dictionary) -> void:
var motions := table.motions
match ev.k:
"drew":
if ev.p == me and ev.card >= 0:
motions["card:%d" % ev.card] = {"style": "draw", "delay": at}
else:
bot_draws.append(at)
"discard":
var key := "card:%d" % ev.card
if ev.card < 0:
return
var from: String = ev.get("from", "")
if ev.get("why", "") == "destroyed":
motions[key] = {"style": "destroy", "delay": at}
elif ev.p != me and (_from_hidden(ev) or (from == "stack" and seen.stacked.has(ev.card))):
# A card of theirs you hadn't seen: held up for you to read on its way.
motions[key] = {"style": "reveal", "delay": at, "p": ev.p, "from": "deck" if from == "deck" else "hand", "color": Style.OWNER[ev.p]}
else:
var spec := {"style": "discard", "delay": at}
if ev.get("from", "") == "deck":
spec.merge({"p": ev.p, "from": "deck"})
motions[key] = spec
"stack":
# An Execution played: up in front of you, then beside the table until it resolves.
if ev.get("kind", "") == "execution" and ev.card >= 0:
seen.stacked[ev.card] = true
var spec := {"style": "stack", "delay": at, "color": Style.OWNER[ev.p]}
if ev.p != me:
spec.merge({"p": ev.p, "from": "hand"})
motions["card:%d" % ev.card] = spec
"pile":
# Back into a hidden pile (a Server going home, a card shuffled in).
if ev.card >= 0:
motions["card:%d" % ev.card] = {"style": "leave", "delay": at, "p": ev.p, "to": ev.zone}
"install":
if ev.card < 0:
return
var key := "card:%d" % ev.card
var d: Dictionary = after.defs.get(State.name_of(after, ev.card), {}) if ev.card < after.cards.size() else {}
if d.get("kind", "") == "server":
motions[key] = {"style": "slam", "delay": at, "p": ev.p, "from": "serverPile"}
elif ev.p != me:
motions[key] = {"style": "reveal", "delay": at, "p": ev.p, "from": "hand", "color": Style.OWNER[ev.p]}
else:
motions[key] = {"style": "slam", "delay": at}
"placed", "moved", "recalled", "flipped":
var key := Moves.anomaly_key(ev.ref)
motions[key] = {"style": "move", "delay": at, "color": Style.OWNER[ev.ref.player]}
# Its Hardware rides along, just behind.
if after.players[ev.ref.player].anomalies.size() >= ev.ref.slot:
for id in State.hardware_of(after, ev.ref):
motions["card:%d" % id] = {"style": "move", "delay": at + 0.04, "color": Style.OWNER[ev.ref.player]}
if ev.k == "flipped":
_cue(func(): table.flash_card(key, Style.ACCENT, at + 0.2))
"destroyed":
var key := Moves.anomaly_key(ev.ref)
seen.destroyed[key] = true
motions[key] = {"style": "destroy", "delay": at}
var loc = ev.get("loc")
_cue(func():
var w = table.anchor_of(Moves.loc_key(loc)) if loc != null else table.anchor_of(key)
if w != null:
table.pop_text(w, "DESTROYED", Style.BAD, at, 24))
"combat":
_cue(func():
var w = table.anchor_of(Moves.loc_key(ev.loc))
if w != null:
table.pop_text(w, "// COMBAT", Style.ATTACK, at, 22))
"rolled":
for rk in ev.dice:
var dice: Array = ev.dice[rk]
if dice.is_empty():
continue
var parts: PackedStringArray = rk.split(":")
var key := "anomaly:%s:%s" % [parts[0], parts[1]] if parts.size() == 2 else ""
var owner := int(parts[0]) if parts.size() == 2 else me
var text := "⚄ %s = %d" % [" ".join(dice.map(func(x): return str(x))), Util.sum(dice)]
_cue(func():
var w = table.anchor_of(key)
if w == null:
w = table.anchor_of(Moves.loc_key(ev.loc))
if w != null:
table.pop_text(w + Vector3(0, 0.3, 0), text, Style.OWNER[owner].lightened(0.3), at, 20)
table.flash_card(key, Style.OWNER[owner], at))
"rerolled":
var key := Moves.anomaly_key(ev.ref)
var text := "REROLL ⚄ %s" % " ".join(ev.dice.map(func(x): return str(x)))
_cue(func():
var w = table.anchor_of(key)
if w != null:
table.pop_text(w + Vector3(0, 0.3, 0), text, Style.WARN, at, 20))
"damaged":
var key := Moves.anomaly_key(ev.ref)
var r: Dictionary = ev.ref
var amount: int = ev.amount
var loc = ev.get("loc")
_cue(func():
table.pop_damage(r, amount, false, loc, at)
table.flash_card(key, Style.BAD, at))
"captured":
var loc: String = ev.loc
var p: int = ev.p
_cue(func():
table.flash_capture(loc, p, at)
var w = table.anchor_of(Moves.loc_key(loc))
if w != null:
table.pop_text(w, "%s TAKES %s" % [State.player_name(p).to_upper(), loc], Style.OWNER[p].lightened(0.25), at + 0.1, 22))
"firewall":
table.gate_delays[ev.line] = at
var to: int = ev.to
var line: String = ev.line
_cue(func():
var w = table.anchor_of("gate:" + line)
if w != null:
var text := "FIREWALL CLEARED" if to == 0 else "FIREWALL LV %d" % to
table.pop_text(w + Vector3(0, 0.4, 0), text, Style.firewall_color(maxi(1, to), after.rules.firewallCost.size()), at + 0.45, 18))
"bandwidth":
var delta: int = ev.to - ev.from
if delta != 0:
var p: int = ev.p
if not table.bandwidth_delays.has(p):
table.bandwidth_delays[p] = at
_cue(func(): hud.pulse_bandwidth(p, delta, at))
"counter":
var key := Moves.anomaly_key(ev.ref)
var what: String = ev.get("what", "")
var delta: int = ev.to - ev.from
var text := ""
var color: Color = Style.CLASS_ACCENT.G
if what == "damage":
# Damage itself pops when dealt; a counter going down is a heal, unless the Anomaly
# was just destroyed and is being reset.
if delta < 0 and not seen.destroyed.has(key):
text = "+%d HEAL" % -delta
color = Style.TYPE_ACCENT.server
elif delta != 0:
text = "%+d %s" % [delta, {"infection": "☣", "bugs": "✱"}.get(what, "•")]
if text != "":
_cue(func():
var w = table.anchor_of(key)
if w != null:
table.pop_text(w + Vector3(0, 0.2, 0), text, color, at, 18))
"turn":
var p: int = ev.p
var turn: int = ev.turn
var title := "YOUR COMMAND" if p == me else "%s'S COMMAND" % State.player_name(p).to_upper()
var sub := "ROUND %d · COMMAND %d" % [State.round_of(after, turn), turn] if after.rules.commands.on else "TURN %d" % turn
_cue(func(): hud.banner(title, sub, Style.OWNER[p], at))
## The bot's hand is face-down placeholders by position: the new ones are dealt off its deck, at
## the times its draws came.
func _deal_bot_hand(before: Dictionary, after: Dictionary, me: int, bot_draws: Array[float]) -> void:
if before.is_empty():
return
var p := State.other(me)
var n0: int = mini(before.players[p].hand.size(), GameTable.BOT_HAND_SHOWN)
var n1: int = mini(after.players[p].hand.size(), GameTable.BOT_HAND_SHOWN)
for i in range(n0, n1):
var j := i - n0
var at: float = bot_draws[j] if j < bot_draws.size() else 0.0
table.motions["bothand:%d:%d" % [p, i]] = {"style": "deal", "delay": at, "p": p, "from": "deck"}
func _cue(fn: Callable) -> void:
_cues.append(fn)
## After the table's sync: the cues, which read the new positions.
func run() -> void:
var cues := _cues
_cues = []
for fn in cues:
fn.call()
## Whether a show is playing.
func busy() -> bool:
if Motion.instant():
return false
return Time.get_ticks_msec() / 1000.0 < _until or table.animating()
## Seconds until the planned show ends (0 when none is playing).
func time_left() -> float:
if Motion.instant():
return 0.0
return maxf(0.0, _until - Time.get_ticks_msec() / 1000.0)
## Skips to the end of the show.
func skip() -> void:
_until = 0.0
_cues = []
table.finish_animations()
hud.finish_banner()