Change collisionMap to boolean value
This commit is contained in:
@@ -111,14 +111,14 @@ static void handleTileObjectLayer(BzTileObjectGroup *layer, cute_tiled_layer_t *
|
||||
|
||||
}
|
||||
|
||||
static void updateColliders(BzTileMap *map, i32 startX, i32 startY, i32 endX, i32 endY) {
|
||||
BZ_ASSERT(map->colliderMap);
|
||||
static void updateCollisionMap(BzTileMap *map, i32 startX, i32 startY, i32 endX, i32 endY) {
|
||||
BZ_ASSERT(map->collisionMap);
|
||||
BZ_ASSERT(startX >= 0 && endX <= map->width &&
|
||||
startY >= 0 && endY <= map->height);
|
||||
|
||||
for (i32 y = startY; y < endY; y++) {
|
||||
for (i32 x = startX; x < endX; x++) {
|
||||
map->colliderMap[y * map->width + x] = (BzTileShape){.type=BZ_TILE_SHAPE_NONE};
|
||||
map->collisionMap[y * map->width + x] = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,26 +131,24 @@ static void updateColliders(BzTileMap *map, i32 startX, i32 startY, i32 endX, i3
|
||||
BzTileset *tileset = map->tilesets + layer->tilesetIdx;
|
||||
for (i32 y = startY; y < endY; y++) {
|
||||
for (i32 x = startX; x < endX; x++) {
|
||||
i32 idx = y * map->width + x;
|
||||
if (map->collisionMap[idx]) continue;
|
||||
|
||||
i32 tile = bzTileLayerGetTile(layer, x, y);
|
||||
BzTileShape tilesetShape = bzTilesetGetTileCollider(tileset, tile);
|
||||
if (tilesetShape.type == BZ_TILE_SHAPE_NONE ||
|
||||
tilesetShape.type == BZ_TILE_SHAPE_POINT)
|
||||
continue;
|
||||
tilesetShape.x += layer->offsetX;
|
||||
tilesetShape.y += layer->offsetY;
|
||||
|
||||
BzTileShape *shape = &map->colliderMap[y * map->width + x];
|
||||
if (shape->type == BZ_TILE_SHAPE_NONE) {
|
||||
*shape = tilesetShape;
|
||||
}
|
||||
map->collisionMap[idx] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void createColliders(BzTileMap *map) {
|
||||
map->colliderMap = bzAlloc(map->width * map->height * sizeof(*map->colliderMap));
|
||||
updateColliders(map, 0, 0, map->width, map->height);
|
||||
map->collisionMap = bzAlloc(map->width * map->height * sizeof(*map->collisionMap));
|
||||
updateCollisionMap(map, 0, 0, map->width, map->height);
|
||||
}
|
||||
|
||||
BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
|
||||
@@ -257,7 +255,7 @@ BzTileMap bzTileMapCreate(const BzTileMapDesc *desc) {
|
||||
}
|
||||
cute_tiled_free_map(cuteMap);
|
||||
|
||||
if (desc->generateColliderMap)
|
||||
if (desc->generateCollisionMap)
|
||||
createColliders(&map);
|
||||
|
||||
map.isValid = true;
|
||||
@@ -282,7 +280,7 @@ void bzTileMapDestroy(BzTileMap *map) {
|
||||
}
|
||||
}
|
||||
|
||||
bzFree(map->colliderMap);
|
||||
bzFree(map->collisionMap);
|
||||
|
||||
*map = BZ_TILEMAP_INVALID;
|
||||
}
|
||||
@@ -400,50 +398,30 @@ void bzTileMapDraw(BzTileMap *map) {
|
||||
}
|
||||
}
|
||||
|
||||
void bzTileMapDrawColliders(BzTileMap *map) {
|
||||
void bzTileMapDrawCollisions(BzTileMap *map) {
|
||||
if (!map->collisionMap) return;
|
||||
Color color = RED;
|
||||
color.a = 150;
|
||||
i32 sizeX = map->tileWidth;
|
||||
i32 sizeY = map->tileHeight;
|
||||
for (i32 y = 0; y < map->height; y++) {
|
||||
for (i32 x = 0; x < map->width; x++) {
|
||||
i32 idx = y * map->width + x;
|
||||
BzTileShape shape = map->colliderMap[idx];
|
||||
if (shape.type == BZ_TILE_SHAPE_NONE)
|
||||
continue;
|
||||
|
||||
i32 posX = x * map->tileWidth + shape.x;
|
||||
i32 posY = y * map->tileHeight + shape.y;
|
||||
f32 sizeX = shape.sizeX;
|
||||
f32 sizeY = shape.sizeY;
|
||||
|
||||
switch (shape.type) {
|
||||
case BZ_TILE_SHAPE_NONE:
|
||||
default:
|
||||
break;
|
||||
case BZ_TILE_SHAPE_RECT:
|
||||
DrawRectangleLines(posX, posY, sizeX, sizeY, color);
|
||||
break;
|
||||
case BZ_TILE_SHAPE_ELLIPSE:
|
||||
DrawEllipseLines(posX, posY, sizeX, sizeY, color);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
i32 posX = x * sizeX;
|
||||
i32 posY = y * sizeY;
|
||||
if (map->collisionMap[idx])
|
||||
DrawRectangleLines(posX, posY, sizeX, sizeY, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BzTileShape bzTileMapGetCollider(BzTileMap *map, i32 x, i32 y) {
|
||||
BzTileShape invalid = {.type = BZ_TILE_SHAPE_NONE};
|
||||
if (map->colliderMap == 0) return invalid;
|
||||
bool bzTileMapHasCollision(BzTileMap *map, i32 x, i32 y) {
|
||||
if (!map->collisionMap) return false;
|
||||
i32 idx = y * map->width + x;
|
||||
if (idx < 0 || idx >= map->width * map->height) {
|
||||
return invalid;
|
||||
}
|
||||
return map->colliderMap[idx];
|
||||
return map->collisionMap[idx];
|
||||
}
|
||||
|
||||
void bzTileMapUpdateColliders(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY) {
|
||||
if (!map->colliderMap) return;
|
||||
updateColliders(map, x, y, x + sizeX, y + sizeY);
|
||||
void bzTileMapUpdateCollisions(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY) {
|
||||
if (!map->collisionMap) return;
|
||||
updateCollisionMap(map, x, y, sizeX, sizeY);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ typedef struct BzTileObjectsDesc {
|
||||
|
||||
typedef struct BzTileMapDesc {
|
||||
const char *path;
|
||||
bool generateColliderMap;
|
||||
bool generateCollisionMap;
|
||||
BzTileset tilesets[BZ_MAP_MAX_TILESETS];
|
||||
|
||||
BzTileLayerDesc layers[BZ_MAP_MAX_LAYERS];
|
||||
@@ -85,7 +85,7 @@ typedef struct BzTileMap {
|
||||
i32 tileWidth;
|
||||
i32 tileHeight;
|
||||
|
||||
BzTileShape *colliderMap;
|
||||
bool *collisionMap;
|
||||
|
||||
BzTileLayer layers[BZ_MAP_MAX_LAYERS];
|
||||
i32 layerCount;
|
||||
@@ -123,9 +123,9 @@ BzTileLayer *bzTileMapGetLayer(BzTileMap *map, i32 slotID);
|
||||
BzTileObjectGroup *bzTileMapGetObjects(BzTileMap *map, i32 slotID);
|
||||
|
||||
void bzTileMapDraw(BzTileMap *map);
|
||||
void bzTileMapDrawColliders(BzTileMap *map);
|
||||
BzTileShape bzTileMapGetCollider(BzTileMap *map, i32 x, i32 y);
|
||||
void bzTileMapUpdateColliders(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY);
|
||||
void bzTileMapDrawCollisions(BzTileMap *map);
|
||||
bool bzTileMapHasCollision(BzTileMap *map, i32 x, i32 y);
|
||||
void bzTileMapUpdateCollisions(BzTileMap *map, i32 x, i32 y, i32 sizeX, i32 sizeY);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -37,28 +37,10 @@ bool canPlaceBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile ti
|
||||
if (tile == BUILDINGS_ROAD)
|
||||
return false;
|
||||
}
|
||||
BzTileShape shape = bzTileMapGetCollider(map, x, y);
|
||||
f32 posX = x * map->tileWidth;
|
||||
f32 posY = y * map->tileHeight;
|
||||
shape.x += posX;
|
||||
shape.y += posY;
|
||||
switch (shape.type) {
|
||||
case BZ_TILE_SHAPE_NONE:
|
||||
case BZ_TILE_SHAPE_POINT:
|
||||
break;
|
||||
case BZ_TILE_SHAPE_RECT: {
|
||||
Rectangle shapeRec = {shape.x, shape.y, shape.sizeX, shape.sizeY};
|
||||
if (CheckCollisionRecs(buildArea, shapeRec))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
case BZ_TILE_SHAPE_ELLIPSE: {
|
||||
Vector2 pos = {shape.x, shape.y};
|
||||
f32 radius = (shape.sizeX + shape.sizeY) * 0.5f;
|
||||
if (CheckCollisionCircleRec(pos, radius, buildArea))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
if (bzTileMapHasCollision(map, posX, posY)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,7 +71,7 @@ ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTi
|
||||
bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
|
||||
buildingTile++;
|
||||
|
||||
bzTileMapUpdateColliders(map, x, y, 1, 1);
|
||||
bzTileMapUpdateCollisions(map, x, y, map->tileWidth, map->tileHeight);
|
||||
}
|
||||
buildingTile += buildingTileset->width - sizeX;
|
||||
}
|
||||
|
||||
12
game/main.c
12
game/main.c
@@ -91,7 +91,7 @@ bool init(void *userData) {
|
||||
|
||||
game->map = bzTileMapCreate(&(BzTileMapDesc) {
|
||||
.path="assets/maps/test.tmj",
|
||||
.generateColliderMap=true,
|
||||
.generateCollisionMap=true,
|
||||
.tilesets[0]=game->terrainTileset,
|
||||
.tilesets[1]=game->buildingsTileset,
|
||||
.tilesets[2]=game->entitiesTileset,
|
||||
@@ -140,6 +140,8 @@ bool init(void *userData) {
|
||||
renderDebugPathSystem = renderDebugPath;
|
||||
renderCollidersSystem = renderColliders;
|
||||
|
||||
game->debugDraw.mapColliders = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
void deinit(void *userData) {
|
||||
@@ -151,11 +153,13 @@ void deinit(void *userData) {
|
||||
bzTilesetDestroy(&game->buildingsTileset);
|
||||
bzTilesetDestroy(&game->entitiesTileset);
|
||||
|
||||
Game gameCopy = *game;
|
||||
|
||||
ecs_fini(ECS);
|
||||
ECS = NULL;
|
||||
|
||||
bzObjectPoolDestroy(game->pools.pathData);
|
||||
bzSpatialGridDestroy(game->entityGrid);
|
||||
bzObjectPoolDestroy(gameCopy.pools.pathData);
|
||||
bzSpatialGridDestroy(gameCopy.entityGrid);
|
||||
}
|
||||
|
||||
|
||||
@@ -288,7 +292,7 @@ void render(float dt, void *userData) {
|
||||
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
|
||||
ecs_enable(ECS, renderCollidersSystem, game->debugDraw.entityColliders);
|
||||
if (game->debugDraw.mapColliders)
|
||||
bzTileMapDrawColliders(&game->map);
|
||||
bzTileMapDrawCollisions(&game->map);
|
||||
if (game->debugDraw.spatialGrid)
|
||||
bzSpatialGridDrawDebugGrid(game->entityGrid);
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ bool findPath(const PathfindingDesc *desc) {
|
||||
if (y < 0 || y >= map->height ||
|
||||
x < 0 || x >= map->width)
|
||||
continue;
|
||||
if (bzTileMapGetCollider(map, x, y).type != BZ_TILE_SHAPE_NONE)
|
||||
if (bzTileMapHasCollision(map, x, y))
|
||||
continue;
|
||||
Visited *curVisited = &visited[y * map->width + x];
|
||||
if (curVisited->visited)
|
||||
|
||||
Reference in New Issue
Block a user