打字稿中的隐式属性?

发布于 2025-01-25 08:35:58 字数 1715 浏览 2 评论 0原文

我正在开发一个程序,其中我有许多需要存储当前值和先前值的课程。例如,player需要存储其当前x(以及以前的x)的类,其当前y(和Prev Y)等。此外,我还希望能够获得这些值的插值版本。因此,例如,我可以在上一个x和当前x之间获得中间点。

目前,我这样做了:

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;
  }
}

这很好,因为我只能做player.x以获取当前x value,player.x = 5设置当前x值(并且也会自动设置上一个值)。获取当前值是极其常见的操作。

但是,由于所有样板,它都不是很好。当我有5个不同的值时,我想同时存储当前和先前的值,它会变得非常详细。因此,我想尝试一下:

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);
  }
}

虽然这很好,但现在有一个问题要获得当前值,我必须做player.x.curr and player.y。 .curr。不太好!

有什么办法也可以吃蛋糕吗?理想情况下,我希望能够做类似的事情:

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

但是,我知道如何做到这一点的唯一方法是第一个解决方案,这使我所有的类都用大量的样板和重复性的错误逻辑填充。

另外,如果可能的话,解决方案需要表现出色(这是一个游戏,所以我怀疑我可以使用像玩家位置这样的属性的复杂代理人之类的东西)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文