sunder/scripts/multiplayer/motion.gd
2026-02-18 18:33:17 -05:00

33 lines
882 B
GDScript

class_name MotionPack extends RefCounted
static var tick:int = 0
var direction := Vector2.ZERO
var mask:Array[bool]
func _init() -> void:
mask.resize(256)
mask.fill(false)
func pack() -> PackedByteArray:
var buffer := StreamPeerBuffer.new()
buffer.put_32(tick)
buffer.put_float(direction.x)
buffer.put_float(direction.y)
# bitpack mask array into single byte
var packed:int = 0
for i in mask.size():
if mask[i]: packed |= (1 << i)
buffer.put_u8(packed)
return buffer.data_array
static func unpack(data:PackedByteArray) -> MotionPack:
var buffer := StreamPeerBuffer.new()
buffer.data_array = data
var move := MotionPack.new()
move.tick = buffer.get_32()
move.direction = Vector2(buffer.get_float(), buffer.get_float())
# unpack mask array
var packed:int = buffer.get_u8()
for i in range(256): # 0 to 255
move.mask[i] = bool(packed & (1 << i))
return move