I'm restructuring my model of entities, components and systems, where entities are:
const createEntity = (name, components) => ({
id: genUniqueId(),
name: name,
components: components
});
And I came across a question, and would like to hear someone's experience about it. The question is what are the advantages and disadvantages of each of the following implementation approaches:
1 - Implement all entities in a single global object, and traverse this object by applying the systems in each entity, example:
const entities = {};
for(let key in entities)
for(let system of systems)
system(entities[key]);
2 - Create several global variables for each group that the systems serve, example:
const players = {};
const monsters = {};
const clouds = {};
for(let key in players)
systems['input'](players[key]);
for(let key in monsters)
systems['simpleAI'](monsters[key]);
for(let key in clouds)
systems['wind'](clouds[key]);
3 - Create local variables within the systems to store the entities, and functions for each system to add and remove entities from their internal objects, example:
const windSystem = () => {
const clouds = {};
for(let key in clouds)
...
};
const addEntityInWindSystem = () => {...};
const delEntityInWindSystem = () => {...};
My intention with the question is just to look for other solutions or even understandings about the cons and advantages that each implementation can bring me during development.
clouds
constant do inwindSystem
? I can't remember, but I think that will raise a duplicate declaration error, because you already have it defined in the global scope. \$\endgroup\$