@geckos.io/snapshot-interpolation
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -17,3 +17,3 @@ "use strict"; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
@@ -20,0 +20,0 @@ return result; |
@@ -37,6 +37,6 @@ import { Snapshot, InterpolatedSnapshot, State } from './types'; | ||
/** Interpolate between two snapshots give the percentage or time. */ | ||
interpolate(snapshotA: Snapshot, snapshotB: Snapshot, timeOrPercentage: number, parameters: string): InterpolatedSnapshot; | ||
interpolate(snapshotA: Snapshot, snapshotB: Snapshot, timeOrPercentage: number, parameters: string, deep?: string): InterpolatedSnapshot; | ||
private _interpolate; | ||
/** Get the calculated interpolation on the client. */ | ||
calcInterpolation(parameters: string): InterpolatedSnapshot | undefined; | ||
calcInterpolation(parameters: string, deep?: string): InterpolatedSnapshot | undefined; | ||
} |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -71,10 +82,20 @@ exports.SnapshotInterpolation = void 0; | ||
SnapshotInterpolation.CreateSnapshot = function (state) { | ||
// check if state is an array | ||
if (!Array.isArray(state)) | ||
throw new Error('You have to pass an Array to createSnapshot()'); | ||
// check if each entity has an id | ||
var withoutID = state.filter(function (e) { return typeof e.id !== 'string' && typeof e.id !== 'number'; }); | ||
//console.log(withoutID) | ||
if (withoutID.length > 0) | ||
throw new Error('Each Entity needs to have a id'); | ||
var check = function (state) { | ||
// check if state is an array | ||
if (!Array.isArray(state)) | ||
throw new Error('You have to pass an Array to createSnapshot()'); | ||
// check if each entity has an id | ||
var withoutID = state.filter(function (e) { return typeof e.id !== 'string' && typeof e.id !== 'number'; }); | ||
//console.log(withoutID) | ||
if (withoutID.length > 0) | ||
throw new Error('Each Entity needs to have a id'); | ||
}; | ||
if (Array.isArray(state)) { | ||
check(state); | ||
} | ||
else { | ||
Object.keys(state).forEach(function (key) { | ||
check(state[key]); | ||
}); | ||
} | ||
return { | ||
@@ -96,6 +117,7 @@ id: SnapshotInterpolation.NewId(), | ||
/** Interpolate between two snapshots give the percentage or time. */ | ||
SnapshotInterpolation.prototype.interpolate = function (snapshotA, snapshotB, timeOrPercentage, parameters) { | ||
return this._interpolate(snapshotA, snapshotB, timeOrPercentage, parameters); | ||
SnapshotInterpolation.prototype.interpolate = function (snapshotA, snapshotB, timeOrPercentage, parameters, deep) { | ||
if (deep === void 0) { deep = ''; } | ||
return this._interpolate(snapshotA, snapshotB, timeOrPercentage, parameters, deep); | ||
}; | ||
SnapshotInterpolation.prototype._interpolate = function (snapshotA, snapshotB, timeOrPercentage, parameters) { | ||
SnapshotInterpolation.prototype._interpolate = function (snapshotA, snapshotB, timeOrPercentage, parameters, deep) { | ||
var sorted = [snapshotA, snapshotB].sort(function (a, b) { return b.time - a.time; }); | ||
@@ -125,3 +147,2 @@ var params = parameters | ||
this.serverTime = lerp_1.lerp(t1, t0, pPercent); | ||
var tmpSnapshot = JSON.parse(JSON.stringify(newer)); | ||
var lerpFnc = function (method, start, end, t) { | ||
@@ -146,5 +167,16 @@ if (typeof start === 'undefined' || typeof end === 'undefined') | ||
}; | ||
newer.state.forEach(function (e, i) { | ||
if (!Array.isArray(newer.state) && deep === '') | ||
throw new Error('You forgot to add the "deep" parameter.'); | ||
if (Array.isArray(newer.state) && deep !== '') | ||
throw new Error('No "deep" needed it state is an array.'); | ||
var newerState = Array.isArray(newer.state) | ||
? newer.state | ||
: newer.state[deep]; | ||
var olderState = Array.isArray(older.state) | ||
? older.state | ||
: older.state[deep]; | ||
var tmpSnapshot = JSON.parse(JSON.stringify(__assign(__assign({}, newer), { state: newerState }))); | ||
newerState.forEach(function (e, i) { | ||
var id = e.id; | ||
var other = older.state.find(function (e) { return e.id === id; }); | ||
var other = olderState.find(function (e) { return e.id === id; }); | ||
if (!other) | ||
@@ -161,3 +193,4 @@ return; | ||
var pn = lerpFnc(lerpMethod, p1, p0, pPercent); | ||
tmpSnapshot.state[i][p] = pn; | ||
if (Array.isArray(tmpSnapshot.state)) | ||
tmpSnapshot.state[i][p] = pn; | ||
}); | ||
@@ -174,3 +207,4 @@ }); | ||
/** Get the calculated interpolation on the client. */ | ||
SnapshotInterpolation.prototype.calcInterpolation = function (parameters) { | ||
SnapshotInterpolation.prototype.calcInterpolation = function (parameters, deep) { | ||
if (deep === void 0) { deep = ''; } | ||
// get the snapshots [this._interpolationBuffer] ago | ||
@@ -184,3 +218,3 @@ var serverTime = SnapshotInterpolation.Now() - this._timeOffset - this._interpolationBuffer; | ||
return; | ||
return this._interpolate(newer, older, serverTime, parameters); | ||
return this._interpolate(newer, older, serverTime, parameters, deep); | ||
}; | ||
@@ -187,0 +221,0 @@ return SnapshotInterpolation; |
@@ -12,5 +12,8 @@ export declare type Value = number | string | Quat | undefined; | ||
time: Time; | ||
state: State | { | ||
[key: string]: State; | ||
}; | ||
} | ||
export interface InterpolatedSnapshot { | ||
state: State; | ||
} | ||
export interface InterpolatedSnapshot extends Omit<Snapshot, 'id' | 'time'> { | ||
percentage: number; | ||
@@ -17,0 +20,0 @@ older: ID; |
{ | ||
"name": "@geckos.io/snapshot-interpolation", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A Snapshot Interpolation library for Real-Time Multiplayer Games", | ||
@@ -42,10 +42,10 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@geckos.io/client": "^1.4.3", | ||
"@geckos.io/server": "^1.4.3", | ||
"@geckos.io/client": "^1.5.0", | ||
"@geckos.io/server": "^1.5.0", | ||
"html-webpack-plugin": "^4.3.0", | ||
"jest": "^26.0.1", | ||
"nodemon": "^2.0.3", | ||
"nodemon": "^2.0.4", | ||
"npm-run-all": "^4.1.5", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^3.9.2", | ||
"typescript": "^3.9.5", | ||
"webpack": "^4.43.0", | ||
@@ -52,0 +52,0 @@ "webpack-cli": "^3.3.11", |
@@ -54,2 +54,8 @@ <div align="center"> | ||
## Install | ||
```console | ||
npm install @geckos.io/snapshot-interpolation | ||
``` | ||
## How to use | ||
@@ -85,3 +91,3 @@ | ||
// initialize the library (add your server's fps in milliseconds) | ||
// initialize the library (add your server's fps) | ||
const SI = new SnapshotInterpolation(serverFPS) | ||
@@ -98,3 +104,3 @@ | ||
// calculate the interpolation for the parameters x and y and return the snapshot | ||
const snapshot = SI.calcInterpolation('x y') | ||
const snapshot = SI.calcInterpolation('x y') // [deep: string] as optional second parameter | ||
@@ -138,3 +144,3 @@ // access your state | ||
id: 'myHero', | ||
rad: Math.PI / 4, | ||
rad: Math.PI / 2, | ||
rotationInDeg: 90, | ||
@@ -149,2 +155,29 @@ q: { x: 0, y: 0.707, z: 0, w: 0.707 }, | ||
## Multiple States | ||
```js | ||
// the players state | ||
const playersState = [ | ||
{ id: 0, x: 65, y: -17 }, | ||
{ id: 1, x: 32, y: 9 }, | ||
] | ||
// the towers state | ||
const towersState = [ | ||
{ id: 0, health: 100, type: 'blizzardCannon' }, | ||
{ id: 1, health: 89, type: 'flameThrower' }, | ||
] | ||
const worldState = { | ||
players: playersState, | ||
towers: towersState, | ||
} | ||
// create snapshot | ||
SI.snapshot.create(worldState) | ||
// we only want to interpolate x and y on the players object | ||
SI.calcInterpolation('x y', 'players') | ||
``` | ||
## Vault | ||
@@ -162,3 +195,3 @@ | ||
You can compress the snapshots manually before sending them to the client, and decompress them when the client receives them. No, compression library is included. You have the freedom to do it however it suits your game best. | ||
Will be available soon. | ||
@@ -225,6 +258,6 @@ ## API | ||
/** Interpolate between two snapshots give the percentage or time. */ | ||
SI.interpolate(snapshotA: Snapshot, snapshotB: Snapshot, timeOrPercentage: number, parameters: string): InterpolatedSnapshot | ||
SI.interpolate(snapshotA: Snapshot, snapshotB: Snapshot, timeOrPercentage: number, parameters: string, deep: string): InterpolatedSnapshot | ||
/** Get the calculated interpolation on the client. */ | ||
SI.calcInterpolation(parameters: string): InterpolatedSnapshot | undefined | ||
SI.calcInterpolation(parameters: string, deep: string): InterpolatedSnapshot | undefined | ||
@@ -231,0 +264,0 @@ // class Vault |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84947
503
285