diff --git a/game/entity_map.c b/game/entity_map.c index ac23314..91deb8d 100644 --- a/game/entity_map.c +++ b/game/entity_map.c @@ -139,6 +139,7 @@ EntityMapIter entityMapQueryIter(EntityMap *entityMap, Position pos, Size size) EntityMapIndex mapIndex = calculateMapIndex(entityMap, pos, size); EntityMapIter it = { .entityMap=entityMap, + .queryIdx=entityMap->queryCount++, .index=mapIndex, .x=mapIndex.minX, .y=mapIndex.minY, @@ -159,10 +160,15 @@ bool entityMapQueryNext(EntityMapIter *it) { return false; } } + it->cellIdx++; 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; } diff --git a/game/entity_map.h b/game/entity_map.h index 48fc651..fc9e39b 100644 --- a/game/entity_map.h +++ b/game/entity_map.h @@ -27,6 +27,7 @@ typedef struct EntityMap { i32 height; i32 cellResolution; i32 cellDepth; + i32 queryCount; } EntityMap; typedef struct EntityMapDesc { @@ -38,6 +39,7 @@ typedef struct EntityMapDesc { typedef struct EntityMapIter { EntityMap *entityMap; + i32 queryIdx; EntityMapEntry entry; EntityMapIndex index; i32 x;