I am developing a program where I have many classes that need to store both the current value, and the previous value. For example, a Player
class that would need to store its current x
(and previous x
), its current y
(and prev y
), etc. In addition, I also want to be able to get the interpolated versions of these values. So for example I could get the halfway point between the previous x
and the current x
.
At the moment I am doing it like this:
class Player {
private _x: number;
private _prevX: number;
constructor() {
this._x = 0;
this._prevX = 0;
}
get x() {
return this._x;
}
set x(val: number) {
this._prevX = this._x;
this._x = val;
}
interX(t: number) {
return (1 - t) * this._prevX + this._x * t;
}
}
And this is nice because I can just do player.x
to get the current x
value, and player.x = 5
to set the current x value (and also automatically set the previous value). Getting the current value is an extremely common operation.
However, it's not so nice because of all the boilerplate. When I have 5 different values I want to store both the current and previous values of, it gets very verbose. So, I thought to try this instead:
class Interpolate {
prev: number;
curr: number;
constructor(prev: number, curr: number) {
this.prev = prev;
this.curr = curr;
}
set(val: number) {
this.prev = this.curr;
this.curr = val;
}
interpolate(t: number) {
return (1 - t) * this.prev + this.curr * t;
}
}
class Player {
x: Interpolate;
y: Interpolate;
constructor() {
this.x = new Interpolate(0, 0);
this.y = new Interpolate(0, 0);
}
}
And while this is nice, now it has the problem that to get the current value, I have to do player.x.curr
and player.y.curr
. Not so nice!
Is there any way to have my cake and eat it too? Ideally, I would like to be able to do something like this:
let foo = player.x; // gets currentX value
player.x = 55; // also sets prevX
player.x.inter(0.5); // gets halfway point between previous and current
But the only way I know how to do this is with the first solution, which fills all of my classes with a ton of boilerplate and repetitious error-prone logic.
Also, if possible, the solution needs to be pretty performant (this is for a game, so I doubt I can use something like a complex proxy for a property like the player's position)