diff --git a/assets/buildings.tsj b/assets/buildings.tsj index cfb8733..50fc5b2 100644 --- a/assets/buildings.tsj +++ b/assets/buildings.tsj @@ -292,6 +292,14 @@ "id":36, "type":"fletcher" }, + { + "id":38, + "type":"mine" + }, + { + "id":39, + "type":"mine" + }, { "id":40, "type":"baracks" diff --git a/engine/breeze/map/tileset.c b/engine/breeze/map/tileset.c index 1f35c7e..374c746 100644 --- a/engine/breeze/map/tileset.c +++ b/engine/breeze/map/tileset.c @@ -79,8 +79,10 @@ BzTileset bzTilesetCreate(const BzTilesetDesc *desc) { } BzTile bzTilesetGetTile(BzTileset *tileset, BzTile tile) { + if (tile == 0) + return -1; tile = tile - tileset->startID; - BZ_ASSERT(tile >= tile && tile < tileset->tileCount); + BZ_ASSERT(tile >= 0 && tile < tileset->tileCount); return tile; } Rectangle bzTilesetGetTileRegion(BzTileset *tileset, BzTile tileID) { diff --git a/game/main.c b/game/main.c index 697916b..38a3f94 100644 --- a/game/main.c +++ b/game/main.c @@ -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; diff --git a/game/utils/buildings.h b/game/utils/buildings.h index 971375e..0355fc7 100644 --- a/game/utils/buildings.h +++ b/game/utils/buildings.h @@ -27,7 +27,7 @@ typedef enum BUILDINGS { } BUILDINGS; -static bool getTileBuilding(BzTile tile) { +static BUILDINGS getTileBuilding(BzTile tile) { switch (tile) { case 0: case 1: @@ -47,6 +47,8 @@ static bool getTileBuilding(BzTile tile) { return BUILDINGS_WAREHOUSE; case 6: case 7: + case 38: + case 39: return BUILDINGS_MINE; case 8: case 9: @@ -93,22 +95,99 @@ static bool getTileBuilding(BzTile tile) { } static BUILDINGS getBuildingFromStr(const char *str) { - if (strncmp("keep", str, 4)) return BUILDINGS_KEEP; - if (strncmp("granary", str, 7)) return BUILDINGS_GRANARY; - if (strncmp("armory", str, 6)) return BUILDINGS_ARMORY; - if (strncmp("warehouse", str, 9)) return BUILDINGS_WAREHOUSE; - if (strncmp("mine", str, 4)) return BUILDINGS_MINE; - if (strncmp("baracks", str, 7)) return BUILDINGS_BARACKS; - if (strncmp("orchard", str, 7)) return BUILDINGS_ORCHARD; - if (strncmp("animal_farm", str, 11)) return BUILDINGS_ANIMAL_FARM; - if (strncmp("fletcher", str, 8)) return BUILDINGS_FLETCHER; - if (strncmp("smithy", str, 6)) return BUILDINGS_SMITHY; - if (strncmp("workshop", str, 8)) return BUILDINGS_WORKSHOP; - if (strncmp("farm", str, 4)) return BUILDINGS_FARM; - if (strncmp("road", str, 4)) return BUILDINGS_ROAD; - if (strncmp("wall", str, 4)) return BUILDINGS_WALL; - if (strncmp("gatehouse", str, 9)) return BUILDINGS_GATEHOUSE; - if (strncmp("tower", str, 5)) return BUILDINGS_TOWER; - if (strncmp("small_tower", str, 11)) return BUILDINGS_SMALL_TOWER; + if (strncmp("keep", str, 4) == 0) return BUILDINGS_KEEP; + if (strncmp("granary", str, 7) == 0) return BUILDINGS_GRANARY; + if (strncmp("armory", str, 6) == 0) return BUILDINGS_ARMORY; + if (strncmp("warehouse", str, 9) == 0) return BUILDINGS_WAREHOUSE; + if (strncmp("mine", str, 4) == 0) return BUILDINGS_MINE; + if (strncmp("baracks", str, 7) == 0) return BUILDINGS_BARACKS; + if (strncmp("orchard", str, 7) == 0) return BUILDINGS_ORCHARD; + if (strncmp("animal_farm", str, 11) == 0) return BUILDINGS_ANIMAL_FARM; + if (strncmp("fletcher", str, 8) == 0) return BUILDINGS_FLETCHER; + if (strncmp("smithy", str, 6) == 0) return BUILDINGS_SMITHY; + if (strncmp("workshop", str, 8) == 0) return BUILDINGS_WORKSHOP; + if (strncmp("farm", str, 4) == 0) return BUILDINGS_FARM; + if (strncmp("road", str, 4) == 0) return BUILDINGS_ROAD; + if (strncmp("wall", str, 4) == 0) return BUILDINGS_WALL; + if (strncmp("gatehouse", str, 9) == 0) return BUILDINGS_GATEHOUSE; + if (strncmp("tower", str, 5) == 0) return BUILDINGS_TOWER; + if (strncmp("small_tower", str, 11) == 0) return BUILDINGS_SMALL_TOWER; else return BUILDINGS_NONE; } +static void getBuildingSize(BUILDINGS type, BzTile *outWidth, BzTile *outHeight) { + switch (type) { + case BUILDINGS_KEEP: + if (outWidth) *outWidth = 3; + if (outHeight) *outHeight = 3; + break; + case BUILDINGS_GRANARY: + if (outWidth) *outWidth = 1; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_ARMORY: + if (outWidth) *outWidth = 1; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_WAREHOUSE: + if (outWidth) *outWidth = 1; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_MINE: + if (outWidth) *outWidth = 2; + if (outHeight) *outHeight = 2; + break; + case BUILDINGS_BARACKS: + if (outWidth) *outWidth = 2; + if (outHeight) *outHeight = 2; + break; + case BUILDINGS_ORCHARD: + if (outWidth) *outWidth = 2; + if (outHeight) *outHeight = 2; + break; + case BUILDINGS_ANIMAL_FARM: + if (outWidth) *outWidth = 2; + if (outHeight) *outHeight = 2; + break; + case BUILDINGS_FLETCHER: + if (outWidth) *outWidth = 2; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_SMITHY: + if (outWidth) *outWidth = 2; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_WORKSHOP: + if (outWidth) *outWidth = 2; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_FARM: + if (outWidth) *outWidth = 1; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_ROAD: + if (outWidth) *outWidth = 1; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_WALL: + if (outWidth) *outWidth = 1; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_GATEHOUSE: + if (outWidth) *outWidth = 1; + if (outHeight) *outHeight = 1; + break; + case BUILDINGS_TOWER: + if (outWidth) *outWidth = 2; + if (outHeight) *outHeight = 2; + break; + case BUILDINGS_SMALL_TOWER: + if (outWidth) *outWidth = 1; + if (outHeight) *outHeight = 1; + break; + default: + if (outWidth) *outWidth = 0; + if (outHeight) *outHeight = 0; + break; + } +} + diff --git a/scripts/extract_tileset_classes.py b/scripts/extract_tileset_classes.py index 536db41..6dcf6d0 100755 --- a/scripts/extract_tileset_classes.py +++ b/scripts/extract_tileset_classes.py @@ -65,7 +65,7 @@ print() # ============================ -print(f"{indent()}static bool getTileBuilding(BzTile tile) {{") +print(f"{indent()}static {enum_name} getTileBuilding(BzTile tile) {{") indent_level += indent_offset print(f"{indent()}switch (tile) {{") for enum, ids in types.items(): diff --git a/tiled/buildings.tsx b/tiled/buildings.tsx index 61661ea..fda32ed 100644 --- a/tiled/buildings.tsx +++ b/tiled/buildings.tsx @@ -59,6 +59,8 @@ + +