Snapshot of in-progress work before the alien overhaul so worker worktrees branch from current code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
4.5 KiB
GDScript
125 lines
4.5 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 record (Sender.record, RECORD_SIZE bytes)
|
|
## whenever it changes, and again at least every few frames: 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,
|
|
## whether it is building or has the upgrade menu open, and where its scarecrow is. 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"]
|
|
## Held bits, a 16-bit count per action, the aim (2 x s16), the aim and ground points and the
|
|
## scarecrow's position (3 x s16 cm each), device, weapon, flags, facing (s16), speed.
|
|
const RECORD_SIZE := 1 + 2 * 5 + 4 + 6 + 6 + 3 + 6 + 2 + 1
|
|
|
|
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
|
|
## The partner's scarecrow, as its machine moved it (CoopSession puts this on the host's copy).
|
|
var position := Vector3.ZERO
|
|
var yaw: float = 0.0
|
|
var speed: float = 0.0
|
|
|
|
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 (Sender.record). False if it isn't one.
|
|
func apply(rec: PackedByteArray) -> bool:
|
|
if rec.size() != RECORD_SIZE:
|
|
return false
|
|
var b := CoopLink.reader(rec)
|
|
_held = b.get_u8()
|
|
for i in ACTIONS.size():
|
|
var step := (b.get_u16() - _presses[i]) & 0xFFFF # the counts go round at 65536
|
|
if step < 0x8000:
|
|
_presses[i] += step
|
|
var ax := b.get_16() / 10000.0
|
|
aim = Vector2(ax, b.get_16() / 10000.0)
|
|
aim_point = CoopLink.get_v3(b)
|
|
ground_point = CoopLink.get_v3(b)
|
|
device = b.get_u8()
|
|
weapon = b.get_8()
|
|
var flags := b.get_u8()
|
|
build_active = (flags & 1) != 0
|
|
ui_open = (flags & 2) != 0
|
|
position = CoopLink.get_v3(b)
|
|
yaw = CoopLink.get_angle(b)
|
|
speed = b.get_u8() / 100.0
|
|
return true
|
|
|
|
|
|
## 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
|
|
|
|
## The input record (RemoteControls.apply): the actions held now (none while frozen: a menu
|
|
## is up), every press so far, and the scarecrow.
|
|
func record(combat: ScarecrowCombat, body: PlayerCharacter, build: bool, ui: bool, frozen: bool) -> PackedByteArray:
|
|
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
|
|
var b := StreamPeerBuffer.new()
|
|
b.put_u8(0 if frozen else bits)
|
|
for i in RemoteControls.ACTIONS.size():
|
|
b.put_u16(presses[i] & 0xFFFF)
|
|
b.put_16(clampi(roundi(combat.aim.x * 10000.0), -32767, 32767))
|
|
b.put_16(clampi(roundi(combat.aim.y * 10000.0), -32767, 32767))
|
|
CoopLink.put_v3(b, combat.aim_point)
|
|
CoopLink.put_v3(b, combat.ground_point)
|
|
b.put_u8(int(combat.device))
|
|
b.put_8(int(combat.weapon))
|
|
b.put_u8((1 if build else 0) | (2 if ui else 0))
|
|
CoopLink.put_v3(b, body.global_position)
|
|
CoopLink.put_angle(b, body.model.rotation.y)
|
|
b.put_u8(clampi(roundi(body.move_strength() * 100.0), 0, 255))
|
|
return b.data_array
|