Refactor frontend
This commit is contained in:
parent
a4bab29d84
commit
561a66c196
8 changed files with 9 additions and 17 deletions
|
|
@ -0,0 +1,38 @@
|
|||
import { clamp } from 'shared';
|
||||
|
||||
export class LinearExtrapolator {
|
||||
private velocity = 0;
|
||||
private compensationVelocity = 0;
|
||||
private timeSinceSet = 0;
|
||||
|
||||
constructor(private currentValue: number) {}
|
||||
|
||||
public addFrame(value: number, rateOfChange: number) {
|
||||
this.timeSinceSet = 0;
|
||||
const differenceFromCurrent = value - this.currentValue;
|
||||
|
||||
if (Math.abs(differenceFromCurrent) > 200) {
|
||||
this.currentValue = value;
|
||||
this.compensationVelocity = 0;
|
||||
} else {
|
||||
this.compensationVelocity = differenceFromCurrent / (1 / 30);
|
||||
}
|
||||
|
||||
this.velocity = rateOfChange;
|
||||
}
|
||||
|
||||
public getValue(deltaTime: number): number {
|
||||
this.currentValue += deltaTime * this.velocity;
|
||||
|
||||
const compensationTimeLeft = 1 / 30 - this.timeSinceSet;
|
||||
|
||||
if (compensationTimeLeft > 0) {
|
||||
this.currentValue +=
|
||||
Math.min(compensationTimeLeft, deltaTime) * this.compensationVelocity;
|
||||
}
|
||||
|
||||
this.timeSinceSet += deltaTime;
|
||||
|
||||
return this.currentValue;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue