danprince / midas

🫅 Traditional roguelike where everything you touch turns to gold.
https://danprince.itch.io/midas
2 stars 0 forks source link

JSON Inheritance Collisions #42

Open danprince opened 4 years ago

danprince commented 4 years ago

There are some problems with the JSON format inheritance causing property overwrites.

"player": {
  "extends": ["human"],
  "effects": {
    "rest": ["restore-hp", 1]
  }
}

In this example all inherited effects would be clobbered. There could be a few ways to get around this, one would be running the effects for every inherited type (e.g. player.effects.rest would also trigger human.effects.rest).

Another would be allowing keys to be unflattened at build time:

"player": {
  "extends": ["human"],
  "effects.rest": ["restore-hp", 1]
}

This would unflatten to the structure above, but could also support merging with keys in ancestors.

danprince commented 4 years ago

This could also be useful for partial overrides if sprite became an object:

"human": {
  "sprite.texture": "sprites.png",
  "sprite.x": 50,
  "sprite.y": 50,
  "sprite.w": 16,
  "sprite.h": 16,
},
"player": {
  "extends": ["human"],
  "sprite.x": 20,
  "sprite.y": 0
}
danprince commented 4 years ago

The main problem with this technique is type safety. It makes it much more difficult to validate a data file because the flattened form of the types is time consuming to create.

interface GameObject {
  sprite: Sprite
}

interface GameObjectTemplate {
  "sprite.x": Sprite["x"],
  "sprite.y": Sprite["y"],
  // and so on
}
danprince commented 4 years ago

What if the flattened interfaces could be generated as part of the validation step instead?