I'm trying to create an entity/component system that automatically matches suitable entities suitable systems. I'm using std::bitset
and RTTI
to automatically assign a bit value to every component type.
A system is defined like this: MovementSystem : System<Position, Velocity>
.
MovementSystem
, in this example, accepts any entity that has both the Position
and the Velocity
components (and any other component).
To check if an entity is suitable, I compare the system's bitset to the entity's bitset.
// Let's assume there are max 4 components
1 1 0 1 // Entity bitset
^ ^ ^
Position Velocity OtherB
1 1 0 0 // Suitable example system bitset
^ ^
Position Velocity
1 1 1 0 // Unsuitable example system bitset
^ ^ ^ // Entity does not have OtherA!
Position Velocity OtherA
So far, my solution is this one:
if(entityBitset & systemBitset) == systemBitset)) { /* entity is suitable! */ }
It seems to work, but I found it after doodling bitsets on a whiteboard. Is it correct? Can it be improved any further? (Entities will be created and destroyed an immense amount of times in my games, so performance is very important!)
Code is here if needed (shouldn't be), but it's almost impossible to read.
^
).all()
or.any()
the XOR result?