@hastearcade/snowglobe
Version:
A TypeScript port of CrystalOrb, a high-level Rust game networking library
75 lines • 2.15 kB
JavaScript
import { remEuclid } from './math.js';
export const MIN = -32768;
export const MAX = 32767;
const i16 = new Int16Array(1);
export function make(value = 0) {
i16[0] = value;
return i16[0];
}
// timestamp
export function fromSeconds(seconds, timestampSeconds) {
return make(makeFromSecondsFloat(seconds, timestampSeconds));
}
export function comparableRangeWithMidpoint(timestamp) {
const maxDistanceFromMidpoint = MAX / 2;
return {
min: make(timestamp - maxDistanceFromMidpoint),
max: make(timestamp + maxDistanceFromMidpoint)
};
}
export function acceptableTimestampRange(baseline, timestamp) {
const { min, max } = comparableRangeWithMidpoint(baseline);
return cmp(timestamp, min) >= 0 && cmp(timestamp, max) < 0;
}
export function increment(timestamp) {
return make(timestamp + 1);
}
export function asSeconds(timestamp, timestampSeconds) {
return timestamp * timestampSeconds;
}
export function add(timestamp, rhs) {
return make(timestamp + rhs);
}
export function sub(timestamp, rhs) {
return make(timestamp - rhs);
}
export function cmp(timestamp1, timestamp2) {
const difference = sub(timestamp1, timestamp2);
if (difference < 0) {
return -1;
}
if (difference === 0) {
return 0;
}
return 1;
}
// float timestamp
export function toFloat(timestamp) {
return +timestamp;
}
export function makeFromUnwrappedFloat(frames) {
return (remEuclid(frames + Math.pow(2, 15), Math.pow(2, 16)) -
Math.pow(2, 15));
}
export function makeFromSecondsFloat(seconds, timestampSeconds) {
return makeFromUnwrappedFloat(seconds / timestampSeconds);
}
export function ceil(timestamp) {
return make(Math.ceil(timestamp));
}
export function floor(timestamp) {
return make(Math.floor(timestamp));
}
export function subFloat(timestamp, rhs) {
return makeFromUnwrappedFloat(timestamp - rhs);
}
// timestamped
export function set(timestamped, timestamp) {
;
timestamped.timestamp = timestamp;
return timestamped;
}
export function get(timestamped) {
return timestamped.timestamp;
}
//# sourceMappingURL=timestamp.js.map