Convert buildings to entities (partially)

This commit is contained in:
2023-11-09 18:11:46 +01:00
parent 37dd2a8bc4
commit fed67a61e6
8 changed files with 227 additions and 24 deletions

View File

@@ -5,6 +5,23 @@
#define BZ_ENTRYPOINT
#include <breeze.h>
#include "utils/buildings.h"
#include "components.h"
typedef enum Layers {
LAYER_TERRAIN = 0,
LAYER_FOLIAGE,
LAYER_TREES,
LAYER_TREES2,
LAYER_BUILDINGS,
LAYER_BUILDING_OWNER,
} Layers;
typedef enum ObjectGroup {
OBJECTS_GAME = 0,
OBJECTS_ENTITIES,
} ObjectGroup;
typedef struct Game {
Camera2D camera;
@@ -27,12 +44,64 @@ 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;
}
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);
BzTileMap *map = &GAME.map;
BzTileLayer *ownershipLayer = bzTileMapGetLayer(map, LAYER_BUILDING_OWNER);
BzTile *ownData = ownershipLayer->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 == 0) continue;
*tile = 0;
// 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;
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});
bzTileMapUpdateCollider(&GAME.map, x, y);
}
}
@@ -49,7 +118,6 @@ bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
sizeX * map->tileWidth, sizeY * map->tileHeight};
// Need to check neighbour tiles
// FIXME: Can't place right next to obstacle
tileX -= 1;
tileY -= 1;
sizeX += 2;
@@ -90,20 +158,6 @@ bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
return true;
}
typedef enum Layers {
LAYER_TERRAIN = 0,
LAYER_FOLIAGE,
LAYER_TREES,
LAYER_TREES2,
LAYER_BUILDINGS,
LAYER_BUILDING_OWNER,
} Layers;
typedef enum ObjectGroup {
OBJECTS_GAME = 0,
OBJECTS_ENTITIES,
} ObjectGroup;
bool init(Game *game) {
int screenWidth = 1280;
int screenHeight = 720;
@@ -139,6 +193,7 @@ bool init(Game *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);
@@ -223,6 +278,7 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
appDesc->userData = &GAME;
appDesc->useNuklear = true;
appDesc->useFlecs = true;
return true;
}