diff options
| author | ne_mene <[email protected]> | 2026-03-12 17:36:06 +0100 |
|---|---|---|
| committer | ne_mene <[email protected]> | 2026-03-12 17:36:06 +0100 |
| commit | bedfad9ae704c9f6a72d71f37dd090170da3a908 (patch) | |
| tree | 95ff5d5b26a8f0171c1a76afe63192f6439b1db6 /src/objs/tilemap.lua | |
| parent | 6fc7035db506b648ab27f21cd4ccbe2e874e3750 (diff) | |
tiles.
Diffstat (limited to 'src/objs/tilemap.lua')
| -rw-r--r-- | src/objs/tilemap.lua | 90 |
1 files changed, 82 insertions, 8 deletions
diff --git a/src/objs/tilemap.lua b/src/objs/tilemap.lua index 572a99e..8b1ab76 100644 --- a/src/objs/tilemap.lua +++ b/src/objs/tilemap.lua @@ -9,6 +9,29 @@ function new_tilemap(width, height) return map end +function AUTO_ID(tl, tr, bl, br) + return tl + 2 * tr + 4 * bl + 8 * br +end + +local AUTOTILE_TBL = { + ["0001"] = { 0, 0 }, + ["0010"] = { 1, 0 }, + ["0101"] = { 2, 0 }, + ["1010"] = { 3, 0 }, + ["0100"] = { 0, 1 }, + ["1000"] = { 1, 1 }, + ["0011"] = { 2, 1 }, + ["1100"] = { 3, 1 }, + ["1110"] = { 0, 2 }, + ["1101"] = { 1, 2 }, + ["0110"] = { 2, 2 }, + ["1001"] = { 3, 2 }, + ["1011"] = { 0, 3 }, + ["0111"] = { 1, 3 }, + ["1111"] = { 2, 3 }, + ["0000"] = {} +} + local function ID(map, x, y) return x + y * map.width + 1 end @@ -20,7 +43,7 @@ end register_comp("Tilemap", function(tilemap, width, height) tilemap.tiledata = {} - for i=1, width*height do + for i = 1, width * height do tilemap.tiledata[i] = 0 end tilemap.width = width @@ -30,36 +53,83 @@ register_comp("Tilemap", function(tilemap, width, height) tilemap.needs_rebuild = false end) +function AUTOTILE_CODE(map, x, y) + local typ + + local tl = get_tile(map, x, y) + if tl ~= 0 then + typ = tl + tl = 1 + end + + local tr = get_tile(map, x + 1, y) + if tr ~= 0 then + typ = tr + tr = 1 + end + + local bl = get_tile(map, x, y + 1) + if bl ~= 0 then + typ = bl + bl = 1 + end + + local br = get_tile(map, x + 1, y + 1) + if br ~= 0 then + typ = br + br = 1 + end + return tl .. tr .. bl .. br, typ +end + function rebuild_tilemap(map) map.cache = {} + map.cachesize = 0 + + for x = -1, map.width do + for y = -1, map.height do + local code, typ = AUTOTILE_CODE(map, x, y) + local tex_x, tex_y = unpack(AUTOTILE_TBL[code]) - for x = 0, map.width - 1 do - for y = 0, map.height - 1 do + if typ then + -- Adjust for tile type fr + tex_y = tex_y + (typ - 1) * 4 - local tile = get_tile(map, x, y) - if tile ~= 0 then map.cachesize = map.cachesize + 1 map.cache[map.cachesize] = { - x = x * TILESIZE, y = y * TILESIZE, + x = x * TILESIZE + TILESIZE/2, + y = y * TILESIZE + TILESIZE/2, + tex_x = tex_x * TILESIZE, + tex_y = tex_y * TILESIZE, } end end end end +local TILE_TEX = get_tex("res/img/tilesets.png") +local TILE_QUAD = lg.newQuad( + 0, 0, + TILESIZE, TILESIZE, + TILE_TEX:getWidth(), TILE_TEX:getHeight()) + function tilemap_draw_sys(tilemap) if tilemap.needs_rebuild then rebuild_tilemap(tilemap) tilemap.needs_rebuild = false end - for i=1, tilemap.cachesize do + for i = 1, tilemap.cachesize do local tile = tilemap.cache[i] - lg.circle("fill", tile.x, tile.y, 4) + TILE_QUAD:setViewport(tile.tex_x, tile.tex_y, TILESIZE, TILESIZE) + lg.draw(TILE_TEX, TILE_QUAD, tile.x, tile.y) end end function get_tile(map, x, y) + if x < 0 or x >= map.width or y < 0 or y >= map.height then + return 0 + end return map.tiledata[ID(map, x, y)] end @@ -72,6 +142,10 @@ function set_tile(map, x, y, tileid) map.needs_rebuild = true end +function to_tile_coords(x, y) + return math.floor(x / TILESIZE), math.floor(y / TILESIZE) +end + function remove_tile(map, x, y) map.tiledata[ID(map, x, y)] = 0 map.needs_rebuild = true |
