@geckos.io/snapshot-interpolation
Version:
A Snapshot Interpolation library for Real-Time Multiplayer Games
14 lines • 6.64 kB
JavaScript
var Snap;(()=>{"use strict";var t={156:function(t,e,r){
/**
* @author Yannick Deubel (https://github.com/yandeu)
* @copyright Copyright (c) 2021 Yannick Deubel; Project Url: https://github.com/geckosio/snapshot-interpolation
* @license {@link https://github.com/geckosio/snapshot-interpolation/blob/master/LICENSE|GNU GPLv3}
*/
var i=this&&this.__createBinding||(Object.create?function(t,e,r,i){void 0===i&&(i=r);var a=Object.getOwnPropertyDescriptor(e,r);a&&!("get"in a?!e.__esModule:a.writable||a.configurable)||(a={enumerable:!0,get:function(){return e[r]}}),Object.defineProperty(t,i,a)}:function(t,e,r,i){void 0===i&&(i=r),t[i]=e[r]}),a=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e}),n=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)"default"!==r&&Object.prototype.hasOwnProperty.call(t,r)&&i(e,t,r);return a(e,t),e};Object.defineProperty(e,"__esModule",{value:!0}),e.Types=e.Vault=e.SnapshotInterpolation=void 0;var o=r(357);Object.defineProperty(e,"SnapshotInterpolation",{enumerable:!0,get:function(){return o.SnapshotInterpolation}});var s=r(552);Object.defineProperty(e,"Vault",{enumerable:!0,get:function(){return s.Vault}}),e.Types=n(r(613))},164:(t,e)=>{
/**
* @author three.js authors
* @copyright Copyright © 2010-2021 three.js authors
* @license {@link https://github.com/mrdoob/three.js/blob/dev/LICENSE|MIT}
* @description Copied and modified from: https://github.com/mrdoob/three.js/blob/464efc85ecfda5c03d786d15d8f8eff20d70f256/src/math/Quaternion.js
*/
Object.defineProperty(e,"__esModule",{value:!0}),e.quatSlerp=void 0;e.quatSlerp=(t,e,r)=>{if(0===r)return t;if(1===r)return e;let i=t.x,a=t.y,n=t.z,o=t.w;const s=e.x,l=e.y,u=e.z,f=e.w;if(o!==f||i!==s||a!==l||n!==u){let t=1-r;const e=i*s+a*l+n*u+o*f,p=e>=0?1:-1,d=1-e*e;if(d>.001){const i=Math.sqrt(d),a=Math.atan2(i,e*p);t=Math.sin(t*a)/i,r=Math.sin(r*a)/i}const h=r*p;if(i=i*t+s*h,a=a*t+l*h,n=n*t+u*h,o=o*t+f*h,t===1-r){const t=1/Math.sqrt(i*i+a*a+n*n+o*o);i*=t,a*=t,n*=t,o*=t}}return{x:i,y:a,z:n,w:o}}},357:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.SnapshotInterpolation=void 0;const i=r(552),a=r(671),n=r(164);class o{constructor(t,e={}){this.vault=new i.Vault,this._interpolationBuffer=100,this._timeOffset=-1,this.serverTime=0,t&&(this._interpolationBuffer=1e3/t*3),this.config={autoCorrectTimeOffset:!0,...e}}get interpolationBuffer(){return{get:()=>this._interpolationBuffer,set:t=>{this._interpolationBuffer=t}}}static Now(){return Date.now()}get timeOffset(){return this._timeOffset}static NewId(){return Math.random().toString(36).substr(2,6)}get snapshot(){return{create:t=>o.CreateSnapshot(t),add:t=>this.addSnapshot(t)}}static CreateSnapshot(t){const e=t=>{if(!Array.isArray(t))throw new Error("You have to pass an Array to createSnapshot()");if(t.filter((t=>"string"!=typeof t.id&&"number"!=typeof t.id)).length>0)throw new Error("Each Entity needs to have a id")};return Array.isArray(t)?e(t):Object.keys(t).forEach((r=>{e(t[r])})),{id:o.NewId(),time:o.Now(),state:t}}addSnapshot(t){var e;const r=o.Now(),i=t.time;if(-1===this._timeOffset&&(this._timeOffset=r-i),!0===(null===(e=this.config)||void 0===e?void 0:e.autoCorrectTimeOffset)){const t=r-i;Math.abs(this._timeOffset-t)>50&&(this._timeOffset=t)}this.vault.add(t)}interpolate(t,e,r,i,a=""){return this._interpolate(t,e,r,i,a)}_interpolate(t,e,r,i,o){const s=[t,e].sort(((t,e)=>e.time-t.time)),l=i.trim().replace(/\W+/," ").split(" "),u=s[0],f=s[1],p=u.time,d=f.time,h=r<=1?r:(r-d)/(p-d);this.serverTime=(0,a.lerp)(d,p,h);if(!Array.isArray(u.state)&&""===o)throw new Error('You forgot to add the "deep" parameter.');if(Array.isArray(u.state)&&""!==o)throw new Error('No "deep" needed it state is an array.');const c=Array.isArray(u.state)?u.state:u.state[o],v=Array.isArray(f.state)?f.state:f.state[o];let _=JSON.parse(JSON.stringify({...u,state:c}));c.forEach(((t,e)=>{const r=t.id,i=v.find((t=>t.id===r));i&&l.forEach((r=>{const o=r.match(/\w\(([\w]+)\)/),s=o?null==o?void 0:o[1]:"linear";o&&(r=null==o?void 0:o[0].replace(/\([\S]+$/gm,""));const l=null==t?void 0:t[r],u=((t,e,r,i)=>{if(void 0!==e&&void 0!==r){if("string"==typeof e||"string"==typeof r)throw new Error("Can't interpolate string!");if("number"==typeof e&&"number"==typeof r){if("linear"===t)return(0,a.lerp)(e,r,i);if("deg"===t)return(0,a.degreeLerp)(e,r,i);if("rad"===t)return(0,a.radianLerp)(e,r,i)}if("object"==typeof e&&"object"==typeof r&&"quat"===t)return(0,n.quatSlerp)(e,r,i);throw new Error(`No lerp method "${t}" found!`)}})(s,null==i?void 0:i[r],l,h);Array.isArray(_.state)&&(_.state[e][r]=u)}))}));return{state:_.state,percentage:h,newer:u.id,older:f.id}}calcInterpolation(t,e=""){const r=o.Now()-this._timeOffset-this._interpolationBuffer,i=this.vault.get(r);if(!i)return;const{older:a,newer:n}=i;return a&&n?this._interpolate(n,a,r,t,e):void 0}}e.SnapshotInterpolation=o},552:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Vault=void 0;e.Vault=class{constructor(){this._vault=[],this._vaultSize=120}getById(t){var e;return null===(e=this._vault.filter((e=>e.id===t)))||void 0===e?void 0:e[0]}clear(){this._vault=[]}get(t,e){var r;const i=this._vault.sort(((t,e)=>e.time-t.time));if(void 0===t)return i[0];for(let a=0;a<i.length;a++){if(i[a].time<=t){const n={older:i[a],newer:i[a-1]};if(e){const e=Math.abs(t-n.older.time),i=Math.abs(t-(null===(r=n.newer)||void 0===r?void 0:r.time));return isNaN(i)||i<=e?n.older:n.newer}return n}}}add(t){this._vault.length>this._vaultSize-1&&this._vault.sort(((t,e)=>t.time-e.time)).shift(),this._vault.push(t)}get size(){return this._vault.length}setMaxSize(t){this._vaultSize=t}getMaxSize(){return this._vaultSize}}},613:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0})},671:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.radianLerp=e.degreeLerp=e.lerp=void 0;const r=3.14159265359,i=6.28318530718;e.lerp=(t,e,r)=>t+(e-t)*r;e.degreeLerp=(t,r,i)=>{let a,n=r-t;return n<-180?(r+=360,a=(0,e.lerp)(t,r,i),a>=360&&(a-=360)):n>180?(r-=360,a=(0,e.lerp)(t,r,i),a<0&&(a+=360)):a=(0,e.lerp)(t,r,i),a};e.radianLerp=(t,a,n)=>{let o,s=a-t;return s<-r?(a+=i,o=(0,e.lerp)(t,a,n),o>=i&&(o-=i)):s>r?(a-=i,o=(0,e.lerp)(t,a,n),o<0&&(o+=i)):o=(0,e.lerp)(t,a,n),o}}},e={};function r(i){var a=e[i];if(void 0!==a)return a.exports;var n=e[i]={exports:{}};return t[i].call(n.exports,n,n.exports,r),n.exports}var i={};(()=>{var t=i;const e=r(156),a=r(156);t.default={SnapshotInterpolation:e.SnapshotInterpolation,Vault:a.Vault}})(),Snap=i.default})();