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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user