extends TestCase func test_round_trip_preserves_every_field() -> void: var command: PlayerCommand = PlayerCommand.new() command.tick = 4_000_000_000 command.move_x = -127 command.move_y = 90 command.yaw = 65535 command.pitch = -32767 command.buttons = PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD | 0x8000 command.selection = 200 command.material = 3 var decoded: PlayerCommand = PlayerCommand.decode(command.encode()) assert_not_null(decoded) assert_eq(decoded.tick, 4_000_000_000) assert_eq(decoded.move_x, -127) assert_eq(decoded.move_y, 90) assert_eq(decoded.yaw, 65535) assert_eq(decoded.pitch, -32767) assert_eq(decoded.buttons, command.buttons) assert_eq(decoded.selection, 200) assert_eq(decoded.material, 3) func test_encoding_is_compact_and_fixed_size() -> void: assert_eq(PlayerCommand.new().encode().size(), PlayerCommand.ENCODED_SIZE) assert_eq(PlayerCommand.ENCODED_SIZE, 14) func test_decode_at_an_offset() -> void: var first: PlayerCommand = PlayerCommand.new() first.tick = 7 var second: PlayerCommand = PlayerCommand.new() second.tick = 8 var stream: PackedByteArray = first.encode() + second.encode() assert_eq(PlayerCommand.decode(stream, PlayerCommand.ENCODED_SIZE).tick, 8) func test_truncated_buffer_returns_null_without_error() -> void: var bytes: PackedByteArray = PlayerCommand.new().encode() bytes.resize(PlayerCommand.ENCODED_SIZE - 1) assert_null(PlayerCommand.decode(bytes)) assert_null(PlayerCommand.decode(PackedByteArray())) assert_null(PlayerCommand.decode(PlayerCommand.new().encode(), 1)) assert_null(PlayerCommand.decode(PlayerCommand.new().encode(), -1)) func test_hostile_wire_values_are_clamped() -> void: var bytes: PackedByteArray = PlayerCommand.new().encode() bytes.encode_s8(4, -128) bytes.encode_s16(8, -32768) var decoded: PlayerCommand = PlayerCommand.decode(bytes) assert_eq(decoded.move_x, -PlayerCommand.AXIS_MAX) assert_eq(decoded.pitch, -PlayerCommand.PITCH_MAX) func test_from_input_clamps_and_wraps() -> void: var command: PlayerCommand = PlayerCommand.from_input(1, Vector2(5.0, -5.0), -PI * 0.5, 9.0) assert_eq(command.move_x, PlayerCommand.AXIS_MAX) assert_eq(command.move_y, -PlayerCommand.AXIS_MAX) assert_eq(command.yaw, PlayerCommand.YAW_STEPS * 3 / 4) assert_eq(command.pitch, PlayerCommand.PITCH_MAX) func test_input_helpers_read_back() -> void: var command: PlayerCommand = PlayerCommand.from_input( 1, Vector2(0.5, 0.0), PI, 0.0, PlayerCommand.BUTTON_JUMP ) assert_near(command.yaw_radians(), PI, 0.001) assert_near(command.move_vector().x, 0.5, 0.01) assert_true(command.is_pressed(PlayerCommand.BUTTON_JUMP)) assert_false(command.is_pressed(PlayerCommand.BUTTON_FIRE))