Add update method to entity map
This commit is contained in:
@@ -81,7 +81,7 @@ EntityMapEntry entityMapInsert(EntityMap *entityMap, ecs_entity_t entity, Positi
|
|||||||
entry.index = mapIndex;
|
entry.index = mapIndex;
|
||||||
|
|
||||||
i32 entryIdx = bzArraySize(entityMap->entities);
|
i32 entryIdx = bzArraySize(entityMap->entities);
|
||||||
entry.entryID = entryIdx;
|
entry.entryIdx = entryIdx;
|
||||||
bzArrayPush(entityMap->entities, entry);
|
bzArrayPush(entityMap->entities, entry);
|
||||||
|
|
||||||
bool wasInserted = true;
|
bool wasInserted = true;
|
||||||
@@ -90,6 +90,7 @@ EntityMapEntry entityMapInsert(EntityMap *entityMap, ecs_entity_t entity, Positi
|
|||||||
for (i32 x = mapIndex.minX; x <= mapIndex.maxX; x++) {
|
for (i32 x = mapIndex.minX; x <= mapIndex.maxX; x++) {
|
||||||
wasInserted &= insertMapEntry(entityMap, entryIdx, x, y);
|
wasInserted &= insertMapEntry(entityMap, entryIdx, x, y);
|
||||||
if (!wasInserted) {
|
if (!wasInserted) {
|
||||||
|
entityMapRemoveIdx(entityMap, entryIdx);
|
||||||
return (EntityMapEntry) {
|
return (EntityMapEntry) {
|
||||||
.entity = 0,
|
.entity = 0,
|
||||||
};
|
};
|
||||||
@@ -99,10 +100,27 @@ EntityMapEntry entityMapInsert(EntityMap *entityMap, ecs_entity_t entity, Positi
|
|||||||
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
void entityMapRemove(EntityMap *entityMap, EntityMapEntry entry) {
|
EntityMapEntry entityMapUpdate(EntityMap *entityMap, EntityMapEntry entry, Position pos, Size size) {
|
||||||
entityMapRemoveID(entityMap, entry.entryID);
|
EntityMapEntry check = bzArrayGet(entityMap->entities, entry.entity);
|
||||||
|
BZ_ASSERT(check.entity == entry.entity);
|
||||||
|
|
||||||
|
EntityMapIndex mapIndex = calculateMapIndex(entityMap, pos, size);
|
||||||
|
if (entry.index.minX == mapIndex.minX &&
|
||||||
|
entry.index.minY == mapIndex.minY &&
|
||||||
|
entry.index.maxX == mapIndex.maxX &&
|
||||||
|
entry.index.maxY == mapIndex.maxY) {
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Has room for optimization
|
||||||
|
entityMapRemoveIdx(entityMap, entry.entryIdx);
|
||||||
|
entityMapInsert(entityMap, entry.entity, pos, size);
|
||||||
|
|
||||||
}
|
}
|
||||||
void entityMapRemoveID(EntityMap *entityMap, i32 entryID) {
|
void entityMapRemove(EntityMap *entityMap, EntityMapEntry entry) {
|
||||||
|
entityMapRemoveIdx(entityMap, entry.entryIdx);
|
||||||
|
}
|
||||||
|
void entityMapRemoveIdx(EntityMap *entityMap, i32 entryID) {
|
||||||
EntityMapEntry entry = bzArrayGet(entityMap->entities, entryID);
|
EntityMapEntry entry = bzArrayGet(entityMap->entities, entryID);
|
||||||
EntityMapIndex index = entry.index;
|
EntityMapIndex index = entry.index;
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ typedef struct EntityMapIndex {
|
|||||||
typedef struct EntityMapEntry {
|
typedef struct EntityMapEntry {
|
||||||
ecs_entity_t entity;
|
ecs_entity_t entity;
|
||||||
i32 queryIdx;
|
i32 queryIdx;
|
||||||
i32 entryID;
|
i32 entryIdx;
|
||||||
EntityMapIndex index;
|
EntityMapIndex index;
|
||||||
} EntityMapEntry;
|
} EntityMapEntry;
|
||||||
|
|
||||||
@@ -49,8 +49,9 @@ EntityMap entityMapCreate(const EntityMapDesc *desc);
|
|||||||
void entityMapDestroy(EntityMap *entityMap);
|
void entityMapDestroy(EntityMap *entityMap);
|
||||||
|
|
||||||
EntityMapEntry entityMapInsert(EntityMap *entityMap, ecs_entity_t entity, Position pos, Size size);
|
EntityMapEntry entityMapInsert(EntityMap *entityMap, ecs_entity_t entity, Position pos, Size size);
|
||||||
|
EntityMapEntry entityMapUpdate(EntityMap *entityMap, EntityMapEntry entry, Position pos, Size size);
|
||||||
void entityMapRemove(EntityMap *entityMap, EntityMapEntry entry);
|
void entityMapRemove(EntityMap *entityMap, EntityMapEntry entry);
|
||||||
void entityMapRemoveID(EntityMap *entityMap, i32 entryID);
|
void entityMapRemoveIdx(EntityMap *entityMap, i32 entryID);
|
||||||
|
|
||||||
EntityMapIter entityMapQueryIter(EntityMap *entityMap, Position pos, Size size);
|
EntityMapIter entityMapQueryIter(EntityMap *entityMap, Position pos, Size size);
|
||||||
bool entityMapQueryNext(EntityMapIter *it);
|
bool entityMapQueryNext(EntityMapIter *it);
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ bool init(void *userData) {
|
|||||||
.maxWidth=game->map.width * game->map.tileWidth,
|
.maxWidth=game->map.width * game->map.tileWidth,
|
||||||
.maxHeight=game->map.height * game->map.tileHeight,
|
.maxHeight=game->map.height * game->map.tileHeight,
|
||||||
.cellResolution=game->map.tileWidth,
|
.cellResolution=game->map.tileWidth,
|
||||||
.cellDepth=10
|
.cellDepth=16
|
||||||
});
|
});
|
||||||
|
|
||||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
|
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
|
||||||
|
|||||||
Reference in New Issue
Block a user