Problem
BaseGridMap is allowed to "optimize" spatial/area searches by limiting entities to check and dividing them into cells. However, with the ECS architecture, systems frequently need to work with a specific component rather than the entity itself. Currently, BaseGridMap queries return entities only. As a result, every query that needs a component has to call FindComponent() for each returned entity or do something else to map entities to components. A typical workflow becomes:
Query BaseGridMap for entities;
Call FindComponent() on each entity;
Only now do actual work.
This partially defeats the purpose of using an optimized spatial lookup when the actual object required by the system is a component.
Proposal
Add component-aware lookup support to BaseGridMap, allowing a spatial query to directly retrieve or iterate over components associated with registered entities.
For example:
class BaseGridMap {
int FindComponentsInRange(out notnull array<ScriptComponent> components, typename componentTypename, vector origin, float range, int mask = 0);
};
So, now you can do:
class MySystem : GameSystem {
protected ref SomeBaseGridMap m_gridMap;
void Register(notnull MyComponent comp) {
m_gridMap.Insert(comp.GetOwner());
};
void Work() {
array<MyComponent> comps = {};
m_gridMap.FindComponentsInRange(comps, MyComponent, pos, r);
foreach (MyComponent comp : comps) {
Do(comp);
};
};
};