State preserving template building
Summary
This change introduces a new approach to template building where components of entities that already exist (based on their anchor) are preserved, unless they are not part of the template in which case they will be despawned.
This functionality can be accessed by calling build_preserving or build_children_preserving on EntityCommands.
I also added a few tests to verify this behavior.
Why?
Currently, when building templates, the entire component set of an entity is overwritten by the template, regardless of whether the entity already exists. This behavior is problematic for UI elements that rely on persistent state, as it resets the state of child components.
Example
Consider a scenario with expansion tiles (foldable UI elements) in the scene tree. If some tiles are folded and an entity is deleted, the scene tree will need to be rebuilt. With the current approach, rebuilding the scene results in the template overwriting the state of the expansion tiles, causing them to unfold and effectively discarding the user's prior state.
I think the implementation is fine. I’ve read through the code several times and I’m already using it in my scene tree prototype on my fork. I’ll add a few small tests later.