Repository navigation
v0.3.0
v0.3.0
- Properties!
From the new section in README.md:
Properties
Properties are a new addition to tinyecs. Until now, the only way to mark an
entity to be of a specific type, or have a special feature, was to add a flag
component, e.g.
ecs.add_component(eid, 'is-drawable', True)while not pretty, this does the job, but has the problem that you now need
different systems for times when you do want to filter, and if you don't want
to, since that flag component is added to the arguments of the system.
To solve this, properties (as inspired by Lisp) have been added. These are
"flags" that live outside the component registry. They will not be handed
over to the system when used in run_system.
You can add, remove, and check for them like this:
ecs.set_property(eid, 'is-drawable')
ecs.has_property(eid, 'is-drawable') # --> True
ecs.remove_property(eid, 'is-drawable')The tinyecs functions that return multiple entities and components now support
filtering by these properties.
ecs.run_system(dt, 'position', 'sprite', has_properties{'is-drawable'})
ecs.has(eid, has_properties={'is-sprite'})
result = ecs.eids_by_cids('position', 'sprite',
has_properties={'is-drawable'})
result = ecs.comps_of_archetype('position', 'sprite',
has_properties={'is-drawable', ...})This way, e.g. sprites can now be layered by filtering for the different
types:
ecs.run_system(0, render_sprites, 'position', 'sprite', has_properties={'is-enemy'))
ecs.run_system(0, render_sprites, 'position', 'sprite', has_properties={'bullet'))
ecs.run_system(0, render_sprites, 'position', 'sprite', has_properties={'fx'))
ecs.run_system(0, render_sprites, 'position', 'sprite', has_properties={'hud'))Finally, properties can be searched directly, e.g. to prune dead entities:
for eid in ecs.eid_by_property('is-dead'):
ecs.remove_entity(eid)