Overhaul how building and ownership layers are handled at init
This commit is contained in:
@@ -9,6 +9,16 @@
|
||||
|
||||
BzTileMap BZ_TILEMAP_INVALID = {.isValid = false};
|
||||
|
||||
void bzTileLayerSkipRender(BzTileLayer *layer) {
|
||||
|
||||
}
|
||||
void bzTileObjectGroupSkipRender(BzTileObjectGroup *objectGroup) {
|
||||
|
||||
}
|
||||
|
||||
BzTileLayerRenderFunc BZ_TILE_LAYER_SKIP_RENDER = bzTileLayerSkipRender;
|
||||
BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER = bzTileObjectGroupSkipRender;
|
||||
|
||||
bool bzTileLayerClear(BzTileLayer *layer, BzTile *data, i32 dataCount) {
|
||||
return true;
|
||||
}
|
||||
@@ -22,6 +32,13 @@ BzTileObjectsFunc BZ_TILE_OBJECTS_CLEAR = bzTileObjectsClear;
|
||||
BzTile bzTileLayerGetTile(BzTileLayer *layer, i32 x, i32 y) {
|
||||
return layer->data[layer->width * y + x];
|
||||
}
|
||||
void bzTileLayerSetTile(BzTileLayer *layer, BzTile tile, i32 x, i32 y, i32 w, i32 h) {
|
||||
for (i32 yIdx = y; yIdx < y + h; yIdx++) {
|
||||
for (i32 xIdx = x; xIdx < x + w; xIdx++) {
|
||||
layer->data[yIdx * layer->width + xIdx] = tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
BzTileset *bzTileLayerGetTileset(BzTileMap *map, BzTileLayer *layer) {
|
||||
i32 idx = layer->tilesetIdx;
|
||||
if (idx < 0 || idx >= map->tilesetCount)
|
||||
|
||||
@@ -15,6 +15,9 @@ typedef struct BzTileObjectGroup BzTileObjectGroup;
|
||||
typedef void (*BzTileLayerRenderFunc)(BzTileLayer *layer);
|
||||
typedef void (*BzTileObjectGroupRenderFunc)(BzTileObjectGroup *objectGroup);
|
||||
|
||||
extern BzTileLayerRenderFunc BZ_TILE_LAYER_SKIP_RENDER;
|
||||
extern BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER;
|
||||
|
||||
typedef struct BzTileLayerDesc {
|
||||
const char *name; // Matches map layer names
|
||||
BzTileLayerRenderFunc renderer;
|
||||
@@ -105,6 +108,7 @@ extern BzTileLayerFunc BZ_TILE_LAYER_CLEAR;
|
||||
extern BzTileObjectsFunc BZ_TILE_OBJECTS_CLEAR;
|
||||
|
||||
BzTile bzTileLayerGetTile(BzTileLayer *layer, i32 x, i32 y);
|
||||
void bzTileLayerSetTile(BzTileLayer *layer, BzTile tile, i32 x, i32 y, i32 w, i32 h);
|
||||
BzTileset *bzTileLayerGetTileset(BzTileMap *map, BzTileLayer *layer);
|
||||
|
||||
BzTileMap bzTileMapCreate(const BzTileMapDesc *desc);
|
||||
|
||||
56
game/main.c
56
game/main.c
@@ -44,41 +44,47 @@ bool handleGameObjects(BzTileObjectGroup *objectLayer, BzTileObject *objects, i3
|
||||
return true;
|
||||
}
|
||||
|
||||
bool prepareBuildingLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
|
||||
BzTileset *tileset = bzTileLayerGetTileset(&GAME.map, layer);
|
||||
for (i32 i = 0; i < dataCount; i++) {
|
||||
BzTile tile = bzTilesetGetTile(tileset, data[i]);
|
||||
data[i] = getTileBuilding(tile);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
ecs_entity_t *entityMap = NULL;
|
||||
|
||||
bool handleBuildLayer(BzTileLayer *layer, BzTile *data, i32 dataCount) {
|
||||
bool prepareBuildings(BzTileLayer *layer, BzTile *data, i32 dataCount) {
|
||||
ECS_COMPONENT(ECS, TilePosition);
|
||||
ECS_COMPONENT(ECS, TileSize);
|
||||
|
||||
entityMap = bzCalloc(sizeof(*entityMap), layer->width * layer->height);
|
||||
|
||||
BzTileMap *map = &GAME.map;
|
||||
BzTileLayer *ownershipLayer = bzTileMapGetLayer(map, LAYER_BUILDING_OWNER);
|
||||
BzTile *ownData = ownershipLayer->data;
|
||||
BzTileLayer *ownershipLayer = layer;
|
||||
BzTileLayer *buildingLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
|
||||
BzTileset *buildingTileset = bzTileLayerGetTileset(map, buildingLayer);
|
||||
BzTile *buildingData = buildingLayer->data;
|
||||
|
||||
for (i32 y = 0; y < layer->height; y++) {
|
||||
for (i32 x = 0; x < layer->width; x++) {
|
||||
BzTile *tile = data + y * layer->width + x;
|
||||
BzTile *ownTile = ownData + y * layer->width + x;
|
||||
if (*tile == BUILDINGS_NONE) continue;
|
||||
BzTile ownerTile = data[y * layer->width + x];
|
||||
BzTile buildingRawTile = buildingData[y * layer->width + x];
|
||||
BzTile buildingTile = bzTilesetGetTile(buildingTileset, buildingRawTile);
|
||||
buildingTile = getTileBuilding(buildingTile);
|
||||
if (buildingTile == BUILDINGS_NONE || ownerTile == 0) continue;
|
||||
// We have a building
|
||||
TileSize size = {};
|
||||
getBuildingSize(*tile, &size.w, &size.h);
|
||||
getBuildingSize(buildingTile, &size.w, &size.h);
|
||||
bzTileLayerSetTile(ownershipLayer, 0, x, y, size.w, size.h);
|
||||
bzLogInfo("Got size: %2d %2d", size.w, size.h);
|
||||
|
||||
ecs_entity_t e = ecs_new_id(ECS);
|
||||
ecs_set(ECS, e, TilePosition, {.x=x, .y=y});
|
||||
ecs_set(ECS, e, TileSize, {.w=0, .h=0});
|
||||
ecs_set(ECS, e, TileSize, {.w=size.w, .h=size.h});
|
||||
|
||||
bzTileMapUpdateCollider(&GAME.map, x, y);
|
||||
for (i32 yIdx = y; yIdx < y + size.h; yIdx++) {
|
||||
for (i32 xIdx = x; xIdx < x + size.w; xIdx++) {
|
||||
entityMap[yIdx * layer->width + xIdx] = e;
|
||||
}
|
||||
}
|
||||
|
||||
//bzTileMapUpdateCollider(&GAME.map, x, y);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
|
||||
@@ -97,6 +103,7 @@ bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
|
||||
sizeY += 2;
|
||||
|
||||
BzTileLayer *buildLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
|
||||
BzTileset *tileset = bzTileLayerGetTileset(map, buildLayer);
|
||||
|
||||
for (i32 y = tileY; y < tileY + sizeY; y++) {
|
||||
for (i32 x = tileX; x < tileX + sizeX; x++) {
|
||||
@@ -104,6 +111,8 @@ bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
|
||||
y != tileY && y != tileY + sizeY - 1) {
|
||||
// Without padding
|
||||
BzTile tile = bzTileLayerGetTile(buildLayer, x, y);
|
||||
tile = bzTilesetGetTile(tileset, tile);
|
||||
tile = getTileBuilding(tile);
|
||||
if (tile == BUILDINGS_ROAD)
|
||||
return false;
|
||||
}
|
||||
@@ -168,15 +177,13 @@ bool init(Game *game) {
|
||||
.layers[LAYER_TREES]=(BzTileLayerDesc) {"Trees"},
|
||||
.layers[LAYER_TREES2]=(BzTileLayerDesc) {"TreesS"},
|
||||
.layers[LAYER_BUILDINGS]=(BzTileLayerDesc) {"Buildings"},
|
||||
.layers[LAYER_BUILDING_OWNER]=(BzTileLayerDesc) {"BuildingOwnership"},
|
||||
.layers[LAYER_BUILDING_OWNER]=(BzTileLayerDesc) {"BuildingOwnership", BZ_TILE_LAYER_SKIP_RENDER},
|
||||
|
||||
.objectGroups[OBJECTS_GAME]=(BzTileObjectsDesc) {"Game"},
|
||||
.objectGroups[OBJECTS_ENTITIES]=(BzTileObjectsDesc ) {"Entities"}
|
||||
});
|
||||
|
||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDINGS, prepareBuildingLayer);
|
||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDINGS, handleBuildLayer);
|
||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, BZ_TILE_LAYER_CLEAR);
|
||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, prepareBuildings);
|
||||
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, handleGameObjects);
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, BZ_TILE_OBJECTS_CLEAR);
|
||||
@@ -227,8 +234,9 @@ void render(float dt, Game *game) {
|
||||
|
||||
EndMode2D();
|
||||
|
||||
if (nk_begin(NK, "Show", nk_rect(50, 50, 220, 220),
|
||||
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
|
||||
if (nk_begin(NK, "DebugMenu", nk_rect(50, 50, 220, 220),
|
||||
NK_WINDOW_BORDER |NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
|
||||
NK_WINDOW_TITLE | NK_WINDOW_MINIMIZABLE)) {
|
||||
// fixed widget pixel width
|
||||
nk_layout_row_static(NK, 30, 80, 1);
|
||||
nk_labelf(NK, NK_TEXT_LEFT, "tileX: %d", tileX);
|
||||
|
||||
Reference in New Issue
Block a user