Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduce a little bit ECS to improve the composition.
The previous implementation used the
EntType
trait to define entity callbacks, but it encountered several limitations:EntType
from the World, as the callback holds a reference to it.It turns out, in Rust, our only choice is ECS. The traditional OOP approch always holds the reference, which prevent us from accessing entities in a unified method.
Of cause, this
ECS
is different with thatECS
, we have so many ECS. The bevy ECS is the most popular and powerful implementation in the rust world, and it is very inspiring. But I found it's too complicated for me, the query and system schedule is powerful but it feel like overkilled to me. I have tried bevy ECS, and my experience tell me I want something simpler, no complex queries and just callback style code.I came across a blog post, Archetypal ECS Considered Harmful, which inspired me. It reassured me that there are others who also seek a simpler ECS.
What I truly need is exactly what the post discusses - the compositional capabilities of ECS. Components are the key. By introducing components, we gain the ability to dynamically access data and provide a unified way to work with entities, solving the problems I faced.
Turning into ECS, I refactor the old EntType into EntHooks, a component, Any entity providing this component will be called by the engine. I also remove the Entity struct, moving its fields into seperate components:
Transform
,Phisics
,Sprite
.Some system, such as collision detection, need state, so I borrowed the idea of Resource from bevy. I added a CollisionSet resource, entities can be added into CollisionSet to enable the collision detection.
Here’s an example of accessing an entity's component:
See Demo for a full example.