Soft fail, when checking for collisions outside map bounds

This commit is contained in:
2024-02-12 09:33:12 +01:00
parent bfc1bc425f
commit c5b6dc0501

View File

@@ -488,11 +488,15 @@ void bzTileMapClearCollisionLayer(BzTileMap *map, i32 layer) {
} }
bool bzTileMapHasAnyCollision(BzTileMap *map, i32 x, i32 y) { bool bzTileMapHasAnyCollision(BzTileMap *map, i32 x, i32 y) {
if (!map->collisionMap) return false; if (!map->collisionMap) return false;
if (x < 0 || x >= map->width || y < 0 || y >= map->height)
return false;
i32 idx = y * map->width + x; i32 idx = y * map->width + x;
return map->collisionMap[idx] != 0; return map->collisionMap[idx] != 0;
} }
bool bzTileMapHasCollision(BzTileMap *map, i32 layer, i32 x, i32 y) { bool bzTileMapHasCollision(BzTileMap *map, i32 layer, i32 x, i32 y) {
BZ_ASSERT(layer >= 0 && layer < 8); BZ_ASSERT(layer >= 0 && layer < 8);
if (x < 0 || x >= map->width || y < 0 || y >= map->height)
return false;
if (!map->collisionMap) return false; if (!map->collisionMap) return false;
i32 idx = y * map->width + x; i32 idx = y * map->width + x;
return (map->collisionMap[idx] & (1 << layer)) != 0; return (map->collisionMap[idx] & (1 << layer)) != 0;
@@ -502,9 +506,8 @@ void bzTileMapSetCollisions(BzTileMap *map, bool collision, i32 layer, i32 start
i32 endY = startY + sizeY; i32 endY = startY + sizeY;
BZ_ASSERT(layer >= 0 && layer < 8); BZ_ASSERT(layer >= 0 && layer < 8);
BZ_ASSERT(map->collisionMap); BZ_ASSERT(map->collisionMap);
BZ_ASSERT(startX >= 0 && endX <= map->width && if (startX < 0 || endX >= map->width || startY < 0 || endY >= map->height)
startY >= 0 && endY <= map->height); return;
for (i32 y = startY; y < endY; y++) { for (i32 y = startY; y < endY; y++) {
for (i32 x = startX; x < endX; x++) { for (i32 x = startX; x < endX; x++) {
i32 idxOffset = y * map->width + x; i32 idxOffset = y * map->width + x;