emgr, or Entity Manager, is a simple Lua library which implements a paradigm similar to Entity Component System (ECS).
The main difference with ECS is that the Component and System concepts are merged into the Manager.
See src.
An entity is a table which is a set of managers, defining a composition. It also has some methods and will free itself from managers on garbage collection.
Operations and data are not owned by an entity; its role is to bind managers to an object with automatic memory management.
A manager can be anything implementing the manager interface (methods). It defines and manages a component for the entities.
Operations can be implemented by managers, maybe depending on other managers, or functions processing entities and multiple managers, etc.
|
To ensure proper garbage collection, init() / free() should be called from the entity and the manager must not have a strong reference to entities just to manage the internal data.
|
-- Create an entity.
emgr.entity()
-- Entity.
-- Initialize the entity into a manager.
-- Can be called even if already initialized.
entity:init(manager, ...)
-- Free the entity from a manager.
-- Can be called even if already freed.
entity:free(manager)
-- Check if the entity has a manager as composition (i.e. is managed by a manager).
-- Low-level alternative: check `entity[manager]`
entity:has(manager)
-- Manager interface, called by corresponding entity methods.
manager:__init(entity, ...)
manager:__free(entity)
for manager in pairs(entity) do ... end