C7-Game / Prototype

An early-stage, open-source 4X strategy game
https://c7-game.github.io/
MIT License
34 stars 9 forks source link

Create a RandomComponent for random number generation #289

Open QuintillusCFC opened 2 years ago

QuintillusCFC commented 2 years ago

In keeping with the migration towards events + components, create a RandomComponent for random number generation, and switch all uses of the RNG to use it.

This should ensure that there is one source of RNGs for the game.

Also, if feasible, keep the seed itself stored in the JSON, and if present use that to initialize the RandomComponent's RNG.

Make sure it's still fairly easy to manually set the seed (in code, for now), so debugging with a fixed seed remains possible.

Split from #282 as a future enhancement. Could be done for Carthage, but we already have 25+ architecture items for Carthage so I'm not inclined to add it right now.

WildWeazel commented 2 years ago

So this is going to raise a question that I haven't yet addressed with components: how to reference a component that you need to use repeatedly. So far the 2 examples are purely reactive - nobody does anything to them, they just listen. But we're going to have lots of other classes calling into RandomComponent, so it probably doesn't make sense to keep looking it up from ComponentManager each time. Should other classes, even non-Components, be allowed to store a reference to a Component? Maybe a WeakReference?

WildWeazel commented 2 years ago

Alternatively, and this is a larger architectural decision, should the ComponentManager or some other object responsible for instantiating all of the components use dependency injection to pass component references to other components that require them? This allows components to be even "dumber" and maybe easier to test, but it would require the creator to know all of the dependencies and in which order to create them, and would make circular dependencies a problem (which is okay if they're just listening to each others' events). In our current model only each component knows what else it needs, but also has to look up its dependencies itself.