1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-04 09:04:13 +02:00

add encoder for sendprops

This commit is contained in:
Robin Appelman 2017-09-01 19:49:40 +02:00
commit ca2083c64a
7 changed files with 336 additions and 58 deletions

View file

@ -1,43 +1,8 @@
import {BitStream} from 'bit-buffer';
import {BSPDecalPacket} from '../../Data/Packet';
import {Vector} from '../../Data/Vector';
export function getCoord(stream: BitStream): number {
const hasInt = stream.readBoolean();
const hasFract = stream.readBoolean();
let value = 0;
if (hasInt || hasFract) {
const sign = stream.readBoolean();
if (hasInt) {
value += stream.readBits(14) + 1;
}
if (hasFract) {
value += stream.readBits(5) * (1 / 32);
}
if (sign) {
value = -value;
}
}
return value;
}
export function encodeCoord(value: number, stream: BitStream) {
const abs = Math.abs(value);
const intPart = Math.floor(abs);
const fractPart = abs % 1;
stream.writeBoolean(intPart !== 0);
stream.writeBoolean(fractPart !== 0);
if (intPart || fractPart) {
stream.writeBoolean(value < 0);
if (intPart) {
stream.writeBits(intPart - 1, 14);
}
if (fractPart) {
stream.writeBits(fractPart * 32, 5);
}
}
}
import {SendPropParser} from '../SendPropParser';
import {SendPropEncoder} from '../SendPropEncoder';
export function getVecCoord(stream: BitStream): Vector {
const hasX = stream.readBoolean();
@ -45,9 +10,9 @@ export function getVecCoord(stream: BitStream): Vector {
const hasZ = stream.readBoolean();
return {
x: hasX ? getCoord(stream) : 0,
y: hasY ? getCoord(stream) : 0,
z: hasZ ? getCoord(stream) : 0,
x: hasX ? SendPropParser.readBitCoord(stream) : 0,
y: hasY ? SendPropParser.readBitCoord(stream) : 0,
z: hasZ ? SendPropParser.readBitCoord(stream) : 0,
};
}
@ -57,13 +22,13 @@ export function encodeVecCoord(vector: Vector, stream: BitStream) {
stream.writeBoolean(vector.z !== 0);
if (vector.x !== 0) {
encodeCoord(vector.x, stream);
SendPropEncoder.writeBitCoord(vector.x, stream);
}
if (vector.y !== 0) {
encodeCoord(vector.y, stream);
SendPropEncoder.writeBitCoord(vector.y, stream);
}
if (vector.z !== 0) {
encodeCoord(vector.z, stream);
SendPropEncoder.writeBitCoord(vector.z, stream);
}
}