harvest/game/scripts/remote_controls.gd
2026-10-04 17:30:48 -04:00

94 lines
3.3 KiB
GDScript

class_name RemoteControls
extends RefCounted
## What a scarecrow played on another machine wants this tick, as the host sees it (co-op,
## CoopSession). The partner's game sends its input every frame: which actions are held, a
## running press count per action (so a press is never lost with a dropped packet: the host
## counts the new ones), its aim (and the ground point a pumpkin bomb would land on), the weapon
## in hand, and whether it is building or has the upgrade menu open. The
## kit classes (Scarecrow, Interactions, ScarecrowCombat) read this instead of the local Input
## when their remote is set.
const ACTIONS: Array[StringName] = [&"fire", &"fear", &"patch", &"interact", &"offhand"]
var aim := Vector2(0.0, -1.0)
var aim_point := Vector3.ZERO
var ground_point := Vector3.ZERO
## ScarecrowCombat.Device of the partner's aim, and its main ScarecrowCombat.Weapon (-1: none
## said).
var device: int = 0
var weapon: int = -1
var build_active := false
var ui_open := false
var _held := 0
var _presses := RemoteControls.counts()
var _seen := RemoteControls.counts()
## A count per action, all 0.
static func counts() -> PackedInt32Array:
var c := PackedInt32Array()
c.resize(ACTIONS.size())
return c
func held(action: StringName) -> bool:
var i := ACTIONS.find(action)
return i >= 0 and (_held & (1 << i)) != 0
## Whether the action was pressed since the last time this was asked (one press each time).
func take(action: StringName) -> bool:
var i := ACTIONS.find(action)
if i < 0 or _seen[i] >= _presses[i]:
return false
_seen[i] += 1
return true
## The partner's input record (CoopSession): {"held": bits, "presses": counts, "aim": Vector2,
## "aim_point": Vector3, "ground": Vector3, "device", "weapon", "build", "ui"}.
func apply(rec: Dictionary) -> void:
_held = int(rec.get("held", 0))
var p: Variant = rec.get("presses", [])
var counts: Array = Array(p) if p is PackedInt32Array else (p if p is Array else [])
for i in mini(ACTIONS.size(), counts.size()):
_presses[i] = maxi(_presses[i], int(counts[i]))
aim = rec.get("aim", aim)
aim_point = rec.get("aim_point", aim_point)
ground_point = rec.get("ground", ground_point)
device = int(rec.get("device", device))
weapon = int(rec.get("weapon", weapon))
build_active = bool(rec.get("build", false))
ui_open = bool(rec.get("ui", false))
## Forget held actions (the partner left, or a new morning).
func release() -> void:
_held = 0
_seen = _presses.duplicate()
## The local side: an input record for the actions held now and every press so far.
class Sender:
var presses := RemoteControls.counts()
var _was := RemoteControls.counts()
## Counts presses from input events (taps between two frames still count).
func note(event: InputEvent) -> void:
for i in RemoteControls.ACTIONS.size():
if event.is_action_pressed(RemoteControls.ACTIONS[i], false):
presses[i] += 1
_was[i] = 1
func record(aim: Vector2, aim_point: Vector3, device: int, build: bool, ui: bool) -> Dictionary:
var bits := 0
for i in RemoteControls.ACTIONS.size():
var down := Input.is_action_pressed(RemoteControls.ACTIONS[i])
if down:
bits |= 1 << i
if _was[i] == 0: # pressed without an event (simulated input)
presses[i] += 1
_was[i] = 1 if down else 0
return {"held": bits, "presses": presses.duplicate(), "aim": aim, "aim_point": aim_point, "device": device,
"build": build, "ui": ui}