pub struct SpatialGrid { /* private fields */ }Expand description
In-memory spatial grid mapping cell coordinates to entity ID lists.
Intended lifetime: one tick. Build with SpatialGrid::new(), populate
with insert, query with query_cell/query_neighbors, then drop.
Implementations§
Source§impl SpatialGrid
impl SpatialGrid
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a grid pre-allocated for at least capacity entities.
Sourcepub fn insert(&mut self, entity_id: &str, x: i32, y: i32)
pub fn insert(&mut self, entity_id: &str, x: i32, y: i32)
Insert an entity at world position (x, y).
Computes the cell coordinate and appends entity_id to that cell’s list.
An entity may appear in multiple calls (e.g. once per owned component),
but callers are responsible for deduplication if required.
Sourcepub fn remove(&mut self, entity_id: &str, x: i32, y: i32)
pub fn remove(&mut self, entity_id: &str, x: i32, y: i32)
Remove an entity from its current cell at world position (x, y).
Removes the first occurrence of entity_id in the cell.
No-ops if the entity is not present.
Sourcepub fn query_cell(&self, x: i32, y: i32) -> &[String]
pub fn query_cell(&self, x: i32, y: i32) -> &[String]
Return all entity IDs in the cell containing world position (x, y).
Returns an empty slice if the cell is unpopulated.
Sourcepub fn query_neighbors(&self, cx: i32, cy: i32) -> Vec<&str>
pub fn query_neighbors(&self, cx: i32, cy: i32) -> Vec<&str>
Return all entity IDs in the 3x3 neighborhood of cell (cx, cy).
Results are unsorted and may contain duplicates if an entity spans cells. Allocates a Vec bounded by 9 * max_entities_per_cell.
Sourcepub fn query_radius(&self, x: i32, y: i32, range: i32) -> Vec<(&str, i32, i32)>
pub fn query_radius(&self, x: i32, y: i32, range: i32) -> Vec<(&str, i32, i32)>
Return all entity IDs within range of world position (x, y).
Queries the 3x3 cell neighborhood and excludes cells whose closest
point to (x, y) exceeds range. Because the grid stores cell
assignment — not exact entity positions — this is cell-granularity
filtering. Callers with exact entity coordinates should apply a
final distance_squared check for precise results.
Each returned tuple is (entity_id, cell_origin_x, cell_origin_y).
Sourcepub fn cell_count(&self) -> usize
pub fn cell_count(&self) -> usize
Return the number of occupied cells.
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Return the total number of entity references across all cells.