Proper building detection

This commit is contained in:
2023-11-10 05:54:40 +01:00
parent 3fdd24d951
commit 60e66bbd67
6 changed files with 123 additions and 51 deletions

View File

@@ -53,27 +53,6 @@ bool prepareBuildingLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
return false;
}
static void detectSize(BzTile *const data, BzTile * const ownData,
const BzTile tile, const BzTile ownerTile,
const i32 width, const i32 height,
BzTile x, BzTile y, TileSize * const max) {
if (x >= width || y >= height) return;
BzTile *curTile = data + y * width + x;
BzTile *curOwn = ownData + y * width + x;
if (*curTile != tile || *curOwn != ownerTile) return;
max->w = max->w > x ? max->w : x;
max->h = max->h > y ? max->h : y;
*curTile = 0;
*curOwn = 0;
detectSize(data, ownData, tile, ownerTile, width, height, x + 1, y, max);
detectSize(data, ownData, tile, ownerTile, width, height, x, y + 1, max);
}
bool handleBuildLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
ECS_COMPONENT(ECS, TilePosition);
ECS_COMPONENT(ECS, TileSize);
@@ -86,16 +65,10 @@ bool handleBuildLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
for (i32 x = 0; x < layer->width; x++) {
BzTile *tile = data + y * layer->width + x;
BzTile *ownTile = ownData + y * layer->width + x;
if (*tile == 0) continue;
if (*tile == BUILDINGS_NONE) continue;
// We have a building
TileSize size = {.w=(BzTile)x, .h=(BzTile)y};
if (*tile != BUILDINGS_ROAD) {
detectSize(data, ownData, *tile, *ownTile,
layer->width, layer->height, (BzTile) x, (BzTile) y, &size);
}
size.w -= x - 1;
size.h -= y - 1;
TileSize size = {};
getBuildingSize(*tile, &size.w, &size.h);
bzLogInfo("Got size: %2d %2d", size.w, size.h);
ecs_entity_t e = ecs_new_id(ECS);
@@ -105,7 +78,7 @@ bool handleBuildLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
bzTileMapUpdateCollider(&GAME.map, x, y);
}
}
return true;
return false;
}
bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
@@ -123,9 +96,17 @@ bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
sizeX += 2;
sizeY += 2;
BzTileLayer *buildLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
for (i32 y = tileY; y < tileY + sizeY; y++) {
for (i32 x = tileX; x < tileX + sizeX; x++) {
if (x != tileX && x != tileX + sizeX - 1 &&
y != tileY && y != tileY + sizeY - 1) {
// Without padding
BzTile tile = bzTileLayerGetTile(buildLayer, x, y);
if (tile == BUILDINGS_ROAD)
return false;
}
BzTileCollider collider = bzTileMapGetCollider(map, x, y);
f32 posX = x * map->tileWidth;
f32 posY = y * map->tileHeight;