Phaser – 如何正确组织游戏数据

huangapple go评论44阅读模式
英文:

phaser - how to keep game data organized right way

问题

我的游戏将处理这样的数据模型:

  • 角色
    • 名称
    • 力量基础
  • 联盟
    • 职位
    • 老板姓名
  • 比赛
    • 玩家
    • 角色
    • 联盟
    • 进度
    • 总力量
    • 金钱
      • 贷款1,贷款2
      • 资金1,资金2

正如您所见,这是一个嵌套的数据。

那么,在注册表更新事件本地存储游戏场景之间应该如何处理这些数据呢?

选项1)将所有内容保留在一个具有嵌套属性的单个类中。这是个问题,因为注册表事件不会针对特定属性触发。

选项2)为每个数据保留一个单独的变量,这很难处理,因为它们不会嵌套在类中。

选项3)还有其他想法吗?

英文:

My game will handle a data model like this:

- Characters
  - Name
  - PowerBase
- League
  - Title
  - BossName
- Match
  - Player
  - Character
  - League
  - Progress
  - PowerTotal
  - Money
    - Loan1, Loan2
    - Funding1, Funding2

As you can see, it's a nested data.

So, how should I handle this data between registry update events, local storage and game scenes?

Option 1) Keep everything in a single class with nested properties. It's a problem because registry events will not fire for specific properties.

Option 2) Keep a single variable for each data, with is hard to handle since they'll not be nested in classes.

Option 3) Any other idea?

答案1

得分: 1

这取决于您的偏好和用途。我个人喜欢使用一个全局变量,它保存整个游戏状态。使用这种方法,您可以在每个场景中访问它,而无需传递它。

而且,我不使用类,我只会使用一个 JSON 对象,因为它们容易存储(如果需要的话)(我尽量保持数据存储尽可能简单)。但如果您需要更多的逻辑和操作,这可能不是最佳解决方案。

// 在全局范围内
const GAME_STATE_KEY = 'MY_GAME_STATE';
var GameState = {
    // ...
};

// 保存和加载状态数据的函数(如果您想要持久保存它)

function loadStateData(){
    GameState = JSON.parse(localStorage.getItem(GAME_STATE_KEY));
}

function saveStateData(){
    localStorage.setItem(GAME_STATE_KEY, JSON.stringify(GameState));
}
英文:

It depends on your preference and usage. I personally like to use a global variable, that holds the entire gamestate. Using this method you have access from every scene without having to pass it.

And more over I don't use a class, I would simply use a json object, since they are easy to store if needed (I try to keep the datastore as dumb as possible). But if you need more logic and actions, this might not be the best solution.

<!-- language: lang-js -->

// in the global scope 
const GAME_STATE_KEY = &#39;MY_GAME_STATE&#39;;
var GameState = {
    // ...
};

// functions to save and load the State (if you want to persist it)

function loadStateData(){
    GameState = JSON.parse(localStorage.getItem(GAME_STATE_KEY));
}

function saveStateData(){
    localStorage.setItem(GAME_STATE_KEY, JSON.stringify(GameState));
}

huangapple
  • 本文由 发表于 2023年7月7日 01:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定