Files
kryon/tests/tile_map_behavior.zi
T

42 lines
2.4 KiB
Ziran

// TileMap's geometry off any host: square cells fitted and centred in the
// bounds, each cell's rectangle, and the cell under a point.
#import "geometry"
TileMap :: #import "kryon/tile_map";
#import "image_mesh"
failure: s32;
Require :: (condition: bool, code: s32) {
if !condition && failure == 0 { failure = code }
}
#program_export
main :: () -> s32 {
// A wide area: the height limits the cells and the grid is centred across.
grid := TileMap.TileMapFit(Rectangle.{10.0, 20.0, 1000.0, 240.0}, 20, 12)
Require(grid.cell == 20.0 && grid.columns == 20 && grid.rows == 12, 1)
Require(grid.board.x == 310.0 && grid.board.y == 20.0 && grid.board.width == 400.0 && grid.board.height == 240.0, 2)
// A tall area: the width limits the cells and the grid is centred down.
tall := TileMap.TileMapFit(Rectangle.{0.0, 0.0, 200.0, 500.0}, 20, 12)
Require(tall.cell == 10.0 && tall.board.y == 190.0, 3)
cell := TileMap.TileMapCell(grid, 21)
Require(cell.x == 330.0 && cell.y == 40.0 && cell.width == 20.0 && cell.height == 20.0, 4)
Require(TileMap.TileMapCellAt(grid, 335.0, 45.0) == 21, 5)
Require(TileMap.TileMapCellAt(grid, 709.0, 259.0) == 239, 6)
Require(TileMap.TileMapCellAt(grid, 309.0, 45.0) == -1 && TileMap.TileMapCellAt(grid, 710.0, 45.0) == -1, 7)
Require(TileMap.TileMapCellAt(grid, 335.0, 260.0) == -1, 8)
// An empty grid has no cells.
Require(TileMap.TileMapFit(Rectangle.{0.0, 0.0, 100.0, 100.0}, 0, 12).cell == 0.0, 9)
snapped := TileMap.TileMapFit(Rectangle.{1.75, 2.75, 101.0, 80.0}, 3, 2, true)
Require(snapped.cell == 33.0 && snapped.board.x == 2.0 && snapped.board.y == 9.0, 10)
Require(TileMap.TileMapCell(snapped, 1).x == TileMap.TileMapCell(snapped, 0).x + snapped.cell, 11)
Require(TileMap.TileMapFit(Rectangle.{0.0, 0.0, 1.0, 1.0}, 3, 3, true).cell == 0.0, 12)
points: [4]Vector2
mesh := ImageMeshQuad(Rectangle.{10.0, 20.0, 20.0, 10.0},
Camera.{.target = Vector2.{20.0, 25.0}, .offset = Vector2.{100.0, 200.0}, .zoom = 2.0, .rotation = 90.0}, points[:])
Require(mesh.columns == 2 && mesh.rows == 2 && mesh.points.count == 4, 13)
Require(points[0].x > 109.99 && points[0].x < 110.01 && points[0].y > 179.99 && points[0].y < 180.01, 14)
Require(points[3].x > 89.99 && points[3].x < 90.01 && points[3].y > 219.99 && points[3].y < 220.01, 15)
short: [3]Vector2
Require(ImageMeshQuad(Rectangle.{}, Camera.{}, short[:]).points.count == 0, 16)
return failure
}