diff options
| -rw-r--r-- | main.lua | 15 | ||||
| -rw-r--r-- | src/objs/tilemap.lua | 78 |
2 files changed, 87 insertions, 6 deletions
@@ -10,17 +10,21 @@ function love.load() event_bind(scn.on_update, "Sprite", sprite_anim_sys) event_bind(scn.on_draw, "Sprite", sprite_draw_sys) + event_bind(scn.on_draw, "Tilemap", tilemap_draw_sys) set_scene(scn) new_player(100, 100) box1 = phys.Box.new(20, 20, 16, 16, { layers = {}, - mask = {"hard"}, + mask = { "hard" }, }) ground1 = phys.Box.new(5, 50, 64, 16, {}) - ground2 = phys.Box.new(64-16, 50+16, 16, 64, {}) + ground2 = phys.Box.new(64 - 16, 50 + 16, 16, 64, {}) + + local tilemap = new_tilemap(64, 64) + scn.tilemap = tilemap end function love.update(dt) @@ -49,15 +53,14 @@ function love.draw(dt) -- TODO: Take care of weird displays lg.setCanvas() local scr_width, scr_height = lg.getDimensions() - WindowScale = min(scr_width/SCR_WIDTH, scr_height/SCR_HEIGHT) + WindowScale = min(scr_width / SCR_WIDTH, scr_height / SCR_HEIGHT) lg.draw( viewport, - scr_width/2, scr_height/2, 0, + scr_width / 2, scr_height / 2, 0, WindowScale, WindowScale, - SCR_WIDTH/2, SCR_HEIGHT/2) + SCR_WIDTH / 2, SCR_HEIGHT / 2) lg.print(tostring(love.timer.getFPS())) input_step() end - diff --git a/src/objs/tilemap.lua b/src/objs/tilemap.lua new file mode 100644 index 0000000..572a99e --- /dev/null +++ b/src/objs/tilemap.lua @@ -0,0 +1,78 @@ +-- Tilemap entity, it dynamically rebuilds its draw cache thingy +-- You just need to worry not and use the tile getting/setting functions + +TILESIZE = 16 +function new_tilemap(width, height) + local map = new_entity() + add_comp(map, "Tilemap", width, height) + + return map +end + +local function ID(map, x, y) + return x + y * map.width + 1 +end + +-- local function POS(map, id) +-- id = id - 1 +-- return id % map.width, math.floor(id / map.width) +-- end + +register_comp("Tilemap", function(tilemap, width, height) + tilemap.tiledata = {} + for i=1, width*height do + tilemap.tiledata[i] = 0 + end + tilemap.width = width + tilemap.height = height + tilemap.cache = {} + tilemap.cachesize = 0 + tilemap.needs_rebuild = false +end) + +function rebuild_tilemap(map) + map.cache = {} + + for x = 0, map.width - 1 do + for y = 0, map.height - 1 do + + 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, + } + end + end + end +end + +function tilemap_draw_sys(tilemap) + if tilemap.needs_rebuild then + rebuild_tilemap(tilemap) + tilemap.needs_rebuild = false + end + + for i=1, tilemap.cachesize do + local tile = tilemap.cache[i] + lg.circle("fill", tile.x, tile.y, 4) + end +end + +function get_tile(map, x, y) + return map.tiledata[ID(map, x, y)] +end + +function queue_tilemap_rebuild(tilemap) + tilemap.needs_rebuild = true +end + +function set_tile(map, x, y, tileid) + map.tiledata[ID(map, x, y)] = tileid + map.needs_rebuild = true +end + +function remove_tile(map, x, y) + map.tiledata[ID(map, x, y)] = 0 + map.needs_rebuild = true +end |
