I am not sure if you were able to see the Gist I added. Seems like Medium does not render it all the time. If not please follow this link:
https://gist.github.com/mzaks/fbdf810109cbf4061b26b2b3d2c65d69
Anyways, the system-state component in my design, is there for double book keeping. Imagine we have following systems:
- Move System (based on position and velocity of the “movable object”)
- Wind System (based on position and wind velocity)
- Gravity System (based on position and gravity)
- Collision System
Move, Wind and Gravity could all change the position component. Collision system should only iterate on entities, which have their position component added or updated. Move, Wind and Gravity systems evaluate entities and change the Position
component, but not the PositionHistory
. Collision system calls the Diff
method to identify “moved” components something like this.
entity.Diff<Move, MoveHistory>().IsAddedOrUpdated()
Collision System does not care about how (or how many times) the Position was changed. It just needs to know that since last time it was executed, the value of Position
component has changed.
If we have only one system which reacts on position change. Than we can call Balance
method directly in this system:
entity.Balance<Move, MoveHistory>().IsAddedOrUpdated()
Because Balance
updates the IDiffComponent
in this case HistoryComponent
and returns the DiffResult
. However if we have multiple systems which are reactive (for example lets add TargetAcquisition system), than we need to think about dedicated place / system, where we can call the Balance
method so that all the reactive system will identify the change. Because after the Balance
method is called, IsChanged()
on a new DiffResult
will return false
.
In some cases there might exist the need to have multiple system-state components. For example if you are building up your own index over the entities, throughout multiple systems. Than for every system you need your own system-state component. Otherwise one IDiffComponent
and a Balance
system in the right place, should be enough.