Fix collision checking when building

This commit is contained in:
2023-11-09 10:48:33 +01:00
parent 1ba6cd424c
commit 185c9ded6e
3 changed files with 39 additions and 6 deletions

View File

@@ -264,9 +264,7 @@ void bzTileMapDrawColliders(BzTileMap *map) {
DrawRectangleLines(posX, posY, sizeX, sizeY, color);
break;
case BZ_TILE_SHAPE_ELLIPSE:
sizeX *= 0.5f;
sizeY *= 0.5f;
DrawEllipseLines(posX + sizeX, posY + sizeY, sizeX, sizeY, color);
DrawEllipseLines(posX, posY, sizeX, sizeY, color);
break;
}
}

View File

@@ -22,6 +22,14 @@ BzTileShape bzCuteObjectToTileShape(cute_tiled_object_t *object) {
shape.sizeX = object->width;
shape.sizeY = object->height;
if (shape.type == BZ_TILE_SHAPE_ELLIPSE) {
// Adjust to use radius and position to be center
shape.sizeX *= 0.5f;
shape.sizeY *= 0.5f;
shape.x += shape.sizeX;
shape.y += shape.sizeY;
}
return shape;
}

View File

@@ -29,10 +29,13 @@ bool handleGameObjects(BzTileObjectLayer *objectLayer) {
bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
// Ensure that it is within the map
if (tileX < 0 || tileX >= map->width ||
tileY < 0 || tileY >= map->height)
if (tileX < 0 || tileX + sizeX > map->width ||
tileY < 0 || tileY + sizeY > map->height)
return false;
Rectangle buildArea = {tileX * map->tileWidth, tileY * map->tileHeight,
sizeX * map->tileWidth, sizeY * map->tileHeight};
// Need to check neighbour tiles
// FIXME: Can't place right next to obstacle
tileX -= 1;
@@ -44,7 +47,31 @@ bool canBuildOn(BzTileMap *map, i32 tileX, i32 tileY, i32 sizeX, i32 sizeY) {
for (i32 y = tileY; y < tileY + sizeY; y++) {
for (i32 x = tileX; x < tileX + sizeX; x++) {
BzTileCollider collider = bzTileMapGetCollider(map, x, y);
if (collider.shapes[0].type != BZ_TILE_SHAPE_NONE) return false;
f32 posX = x * map->tileWidth;
f32 posY = y * map->tileHeight;
for (int i = 0; i < BZ_MAP_COLLIDER_DEPTH; i++) {
BzTileShape shape = collider.shapes[i];
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;
}
}
}
}
}