如何使用VUE VUEX(或任何其他国家管理框架)处理游戏环



我目前使用Vuex。

游戏是基于文本的,这意味着没有图形,并且使所有游戏功能都易于与VUE集成。我的游戏功能都与vuex相关,我没有将我注入VUE的外部代码:Vuex处理所有内容。

我遇到了一个问题,如果有任何框架损失,我的游戏环会计算出Delta时间的时间以及 Update 的次数。由于我的游戏逻辑与Vuex相关,因此我需要使用VUEX派遣大约20至60个动作/秒(取决于FPS(来更新游戏的某些部分(以秒为单位,直到达到一定值(。

我很担心看到我的Vuex Dev-Tools由scriptLoop垃圾邮件(这使其毫无用处(,并且可能会影响游戏性能。

我不知道这是否是正确的方法,如果我缺少某些东西,或者只是为此而没有完成州管理框架(我认为(。

在这里,我正在谈论的代码的某些部分:

// App.vue: core component
public loop(): void {
  this.now = new Date().getTime();
  const elapsed = this.now - this.before;
  const times = Math.floor(elapsed / this.interval);
  (elapsed > this.interval) ? this.update(times) : this.update(1);
  this.before = new Date().getTime();
}
// Update some parts of the game-logic by dispatching the SCRIPT_LOOP action
public update(times: number): void {
  this.$store.dispatch('SCRIPT_LOOP', times);
}
// scripts.mutations.ts: SCRIPT_LOOP action commit this mutation
...
scriptLoop(state: ScriptsState, payload: { scripts: string[], times: number }) {
  payload.scripts.forEach((script) => {
    (state.scripts.find((scr) => scr.name === script) as Models.Script).progression += times / 30;
  });
},
...

我的修复程序是创建一个外部(与VUEX相关的(游戏逻辑,在班级初始化器中,我通过我的Vuex商店,因此我的外部游戏逻辑可以在存储状态更改,甚至在需要时派遣/提交。

最新更新