Only iterate over each entry once

This commit is contained in:
2023-11-15 19:25:37 +01:00
parent 1ceacd2e62
commit 0a44f7b2dc
2 changed files with 10 additions and 2 deletions

View File

@@ -139,6 +139,7 @@ EntityMapIter entityMapQueryIter(EntityMap *entityMap, Position pos, Size size)
EntityMapIndex mapIndex = calculateMapIndex(entityMap, pos, size); EntityMapIndex mapIndex = calculateMapIndex(entityMap, pos, size);
EntityMapIter it = { EntityMapIter it = {
.entityMap=entityMap, .entityMap=entityMap,
.queryIdx=entityMap->queryCount++,
.index=mapIndex, .index=mapIndex,
.x=mapIndex.minX, .x=mapIndex.minX,
.y=mapIndex.minY, .y=mapIndex.minY,
@@ -159,10 +160,15 @@ bool entityMapQueryNext(EntityMapIter *it) {
return false; return false;
} }
} }
it->cellIdx++;
EntityMapCell cell = it->entityMap->cells[cellOffset]; EntityMapCell cell = it->entityMap->cells[cellOffset];
it->entry = bzArrayGet(it->entityMap->entities, cell); EntityMapEntry *entry = &bzArrayGet(it->entityMap->entities, cell);
if (entry->queryIdx == it->queryIdx) {
return entityMapQueryNext(it);
}
entry->queryIdx = it->queryIdx;
it->entry = *entry;
it->cellIdx++;
return true; return true;
} }

View File

@@ -27,6 +27,7 @@ typedef struct EntityMap {
i32 height; i32 height;
i32 cellResolution; i32 cellResolution;
i32 cellDepth; i32 cellDepth;
i32 queryCount;
} EntityMap; } EntityMap;
typedef struct EntityMapDesc { typedef struct EntityMapDesc {
@@ -38,6 +39,7 @@ typedef struct EntityMapDesc {
typedef struct EntityMapIter { typedef struct EntityMapIter {
EntityMap *entityMap; EntityMap *entityMap;
i32 queryIdx;
EntityMapEntry entry; EntityMapEntry entry;
EntityMapIndex index; EntityMapIndex index;
i32 x; i32 x;