Add map bounds checking when building

This commit is contained in:
2023-11-09 09:52:29 +01:00
parent 6cc3ce9750
commit 1ba6cd424c

View File

@@ -28,7 +28,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)
return false;
// Need to check neighbour tiles
// FIXME: Can't place right next to obstacle
tileX -= 1;
tileY -= 1;
sizeX += 2;
@@ -116,15 +122,12 @@ void render(float dt, Game *game) {
int tileX = (int) worldPos.x / 16;
int tileY = (int) worldPos.y / 16;
if (tileX != 0 && tileY != 0) {
bool canPlace = canBuildOn(&game->map, tileX, tileY, sizeX, sizeY);
Color placeColor = canPlace ?
(Color) {0, 255, 0, 200} :
(Color) {255, 0, 0, 200};
bool canPlace = canBuildOn(&game->map, tileX, tileY, sizeX, sizeY);
Color placeColor = canPlace ?
(Color) {0, 255, 0, 200} :
(Color) {255, 0, 0, 200};
DrawRectangleLines(tileX * 16, tileY * 16, sizeX * 16, sizeY * 16, placeColor);
}
DrawRectangleLines(tileX * 16, tileY * 16, sizeX * 16, sizeY * 16, placeColor);
EndMode2D();