diff --git a/game/main.c b/game/main.c index e56be02..7c32082 100644 --- a/game/main.c +++ b/game/main.c @@ -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();