Remove entity map in favor of spatial grid
This commit is contained in:
@@ -1,174 +0,0 @@
|
||||
#include "entity_map.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
EntityMap entityMapCreate(const EntityMapDesc *desc) {
|
||||
i32 width = (desc->maxWidth + (desc->cellResolution - 1)) / desc->cellResolution;
|
||||
i32 height = (desc->maxHeight + (desc->cellResolution - 1)) / desc->cellResolution;
|
||||
EntityMap map = {
|
||||
.width=width,
|
||||
.height=height,
|
||||
.cellResolution=desc->cellResolution,
|
||||
.cellDepth=desc->cellDepth,
|
||||
};
|
||||
|
||||
size_t numBytes = sizeof(EntityMapCell) * map.width * map.height * map.cellDepth;
|
||||
map.cells = bzAlloc(numBytes);
|
||||
bzMemSet(map.cells, 0, numBytes);
|
||||
map.entities = bzArrayCreate(EntityMapEntry, 512);
|
||||
|
||||
return map;
|
||||
}
|
||||
void entityMapDestroy(EntityMap *entityMap) {
|
||||
bzFree(entityMap->cells);
|
||||
entityMap->cells = NULL;
|
||||
bzArrayDestroy(entityMap->entities);
|
||||
entityMap->entities = NULL;
|
||||
}
|
||||
|
||||
static EntityMapIndex calculateMapIndex(EntityMap *entityMap, Position pos, Size size) {
|
||||
i32 minX = (i32) floorf(pos.x);
|
||||
i32 minY = (i32) floorf(pos.y);
|
||||
i32 maxX = (i32) floorf(pos.x + size.x);
|
||||
i32 maxY = (i32) floorf(pos.y + size.y);
|
||||
return (EntityMapIndex) {
|
||||
(i16) (minX / entityMap->width),
|
||||
(i16) (minY / entityMap->height),
|
||||
(i16) (maxX / entityMap->width),
|
||||
(i16) (maxY / entityMap->height),
|
||||
};
|
||||
}
|
||||
|
||||
static bool insertMapEntry(EntityMap *entityMap, EntityMapCell idx, i32 x, i32 y) {
|
||||
EntityMapCell *cell = entityMap->cells + (y * entityMap->width + x) * entityMap->cellDepth;
|
||||
for (i32 i = 0; i < entityMap->cellResolution; i++) {
|
||||
if (cell[i] == 0) {
|
||||
cell[i] = idx;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static void removeMapEntry(EntityMap *entityMap, EntityMapCell idx, i32 x, i32 y) {
|
||||
EntityMapCell *cell = entityMap->cells + (y * entityMap->width + x) * entityMap->cellDepth;
|
||||
i32 i;
|
||||
for (i = 0; i < entityMap->cellResolution; i++) {
|
||||
if (cell[i] == idx) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
i32 j;
|
||||
for (j = i; j < entityMap->cellResolution; j++) {
|
||||
if (cell[j] == 0) {
|
||||
j--;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (i != j) {
|
||||
// swap with last
|
||||
cell[i] = cell[j];
|
||||
cell[j] = 0;
|
||||
} else {
|
||||
// 'i' is last
|
||||
cell[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
EntityMapEntry entityMapInsert(EntityMap *entityMap, ecs_entity_t entity, Position pos, Size size) {
|
||||
EntityMapEntry entry = {entity};
|
||||
EntityMapIndex mapIndex = calculateMapIndex(entityMap, pos, size);
|
||||
entry.index = mapIndex;
|
||||
|
||||
i32 entryIdx = bzArraySize(entityMap->entities);
|
||||
entry.entryIdx = entryIdx;
|
||||
bzArrayPush(entityMap->entities, entry);
|
||||
|
||||
bool wasInserted = true;
|
||||
|
||||
for (i32 y = mapIndex.minY; y <= mapIndex.maxY; y++) {
|
||||
for (i32 x = mapIndex.minX; x <= mapIndex.maxX; x++) {
|
||||
wasInserted &= insertMapEntry(entityMap, entryIdx, x, y);
|
||||
if (!wasInserted) {
|
||||
entityMapRemoveIdx(entityMap, entryIdx);
|
||||
return (EntityMapEntry) {
|
||||
.entity = 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
EntityMapEntry entityMapUpdate(EntityMap *entityMap, EntityMapEntry entry, Position pos, Size size) {
|
||||
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 entityMapRemove(EntityMap *entityMap, EntityMapEntry entry) {
|
||||
entityMapRemoveIdx(entityMap, entry.entryIdx);
|
||||
}
|
||||
void entityMapRemoveIdx(EntityMap *entityMap, i32 entryID) {
|
||||
EntityMapEntry entry = bzArrayGet(entityMap->entities, entryID);
|
||||
EntityMapIndex index = entry.index;
|
||||
|
||||
for (i32 y = index.minY; y <= index.maxY; y++) {
|
||||
for (i32 x = index.minX; x <= index.maxX; x++) {
|
||||
removeMapEntry(entityMap, entryID, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
EntityMapEntry last = bzArrayGet(entityMap->entities, bzArraySize(entityMap->entities));
|
||||
bzArraySet(entityMap->entities, entryID, last);
|
||||
bzArrayPop(entityMap->entities);
|
||||
}
|
||||
|
||||
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,
|
||||
.cellIdx=0,
|
||||
};
|
||||
return it;
|
||||
}
|
||||
bool entityMapQueryNext(EntityMapIter *it) {
|
||||
i32 cellOffset = (it->y * it->entityMap->width + it->x) *
|
||||
it->entityMap->cellDepth + it->cellIdx;
|
||||
if (it->cellIdx >= it->entityMap->cellDepth ||
|
||||
it->entityMap->cells[cellOffset] == 0) {
|
||||
it->cellIdx = 0;
|
||||
it->x++;
|
||||
if (it->x > it->index.maxX) {
|
||||
it->y++;
|
||||
if (it->y > it->index.maxY)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
it->cellIdx++;
|
||||
|
||||
EntityMapCell cell = it->entityMap->cells[cellOffset];
|
||||
EntityMapEntry *entry = &bzArrayGet(it->entityMap->entities, cell);
|
||||
if (entry->queryIdx == it->queryIdx) {
|
||||
return entityMapQueryNext(it);
|
||||
}
|
||||
entry->queryIdx = it->queryIdx;
|
||||
it->entry = *entry;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
#ifndef PIXELDEFENSE_ENTITY_MAP_H
|
||||
#define PIXELDEFENSE_ENTITY_MAP_H
|
||||
|
||||
#include <breeze.h>
|
||||
#include <flecs.h>
|
||||
|
||||
#include "components.h"
|
||||
|
||||
typedef struct EntityMapIndex {
|
||||
i32 minX, minY;
|
||||
i32 maxX, maxY;
|
||||
} EntityMapIndex;
|
||||
|
||||
typedef struct EntityMapEntry {
|
||||
ecs_entity_t entity;
|
||||
i32 queryIdx;
|
||||
i32 entryIdx;
|
||||
EntityMapIndex index;
|
||||
} EntityMapEntry;
|
||||
|
||||
typedef i32 EntityMapCell;
|
||||
|
||||
typedef struct EntityMap {
|
||||
EntityMapEntry *entities;
|
||||
EntityMapCell *cells;
|
||||
i32 width;
|
||||
i32 height;
|
||||
i32 cellResolution;
|
||||
i32 cellDepth;
|
||||
i32 queryCount;
|
||||
} EntityMap;
|
||||
|
||||
typedef struct EntityMapDesc {
|
||||
i32 maxWidth;
|
||||
i32 maxHeight;
|
||||
i32 cellResolution;
|
||||
i32 cellDepth;
|
||||
} EntityMapDesc;
|
||||
|
||||
typedef struct EntityMapIter {
|
||||
EntityMap *entityMap;
|
||||
i32 queryIdx;
|
||||
EntityMapEntry entry;
|
||||
EntityMapIndex index;
|
||||
i32 x;
|
||||
i32 y;
|
||||
i32 cellIdx;
|
||||
} EntityMapIter;
|
||||
|
||||
EntityMap entityMapCreate(const EntityMapDesc *desc);
|
||||
void entityMapDestroy(EntityMap *entityMap);
|
||||
|
||||
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 entityMapRemoveIdx(EntityMap *entityMap, i32 entryID);
|
||||
|
||||
EntityMapIter entityMapQueryIter(EntityMap *entityMap, Position pos, Size size);
|
||||
bool entityMapQueryNext(EntityMapIter *it);
|
||||
|
||||
|
||||
|
||||
#endif //PIXELDEFENSE_ENTITY_MAP_H
|
||||
@@ -4,8 +4,6 @@
|
||||
#include <breeze.h>
|
||||
#include <flecs.h>
|
||||
|
||||
#include "entity_map.h"
|
||||
|
||||
typedef enum InputState {
|
||||
INPUT_NONE,
|
||||
INPUT_PLACING,
|
||||
@@ -20,7 +18,7 @@ typedef struct Game {
|
||||
BzTileset buildingsTileset;
|
||||
BzTileset entitiesTileset;
|
||||
BzTileMap map;
|
||||
EntityMap entityMap;
|
||||
BzSpatialGrid *entityGrid;
|
||||
f32 frameDuration;
|
||||
ecs_entity_t entity;
|
||||
struct {
|
||||
|
||||
15
game/main.c
15
game/main.c
@@ -32,8 +32,6 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
|
||||
appDesc->render = (BzAppRenderFunc) render;
|
||||
appDesc->imguiRender = (BzAppRenderFunc) imguiRender;
|
||||
|
||||
Game *game = bzAlloc(sizeof(*game));
|
||||
bzMemSet(game, 0, sizeof(*game));
|
||||
appDesc->userData = NULL;
|
||||
appDesc->useFlecs = true;
|
||||
|
||||
@@ -99,11 +97,12 @@ bool init(void *userData) {
|
||||
.objectGroups[OBJECTS_GAME]=(BzTileObjectsDesc) {"Game"},
|
||||
.objectGroups[OBJECTS_ENTITIES]=(BzTileObjectsDesc ) {"Entities"}
|
||||
});
|
||||
game->entityMap = entityMapCreate(&(EntityMapDesc) {
|
||||
game->entityGrid = bzSpatialGridCreate(&(BzSpatialGridDesc) {
|
||||
.maxWidth=game->map.width * game->map.tileWidth,
|
||||
.maxHeight=game->map.height * game->map.tileHeight,
|
||||
.cellResolution=game->map.tileWidth,
|
||||
.cellDepth=16
|
||||
.cellWidth=game->map.tileWidth * 2,
|
||||
.cellHeight=game->map.tileHeight * 2,
|
||||
.userDataSize=sizeof(ecs_entity_t)
|
||||
});
|
||||
|
||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
|
||||
@@ -129,6 +128,10 @@ void deinit(void *userData) {
|
||||
bzTilesetDestroy(&game->buildingsTileset);
|
||||
bzTilesetDestroy(&game->entitiesTileset);
|
||||
|
||||
|
||||
bzObjectPoolDestroy(game->pools.pathData);
|
||||
bzSpatialGridDestroy(game->entityGrid);
|
||||
|
||||
ecs_singleton_remove(ECS, Game);
|
||||
}
|
||||
|
||||
@@ -226,6 +229,8 @@ void render(float dt, void *userData) {
|
||||
game->input.buildingSize.sizeY * height, placeColor);
|
||||
}
|
||||
|
||||
bzSpatialGridDrawDebugGrid(game->entityGrid);
|
||||
|
||||
|
||||
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
|
||||
int tileX = (int) worldPos.x / 16;
|
||||
|
||||
Reference in New Issue
Block a user