aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.lua6
-rw-r--r--res/img/tilesets.pngbin2904 -> 3075 bytes
-rw-r--r--src/objs/player.lua18
-rw-r--r--src/objs/tilemap.lua90
-rw-r--r--src/textures.lua4
-rw-r--r--src/utils.lua7
6 files changed, 110 insertions, 15 deletions
diff --git a/main.lua b/main.lua
index c739bf4..21eefbe 100644
--- a/main.lua
+++ b/main.lua
@@ -25,6 +25,8 @@ function love.load()
local tilemap = new_tilemap(64, 64)
scn.tilemap = tilemap
+
+ set_tile(tilemap, 0, 0, 1)
end
function love.update(dt)
@@ -46,10 +48,6 @@ function love.draw(dt)
fire_event(scn.on_draw, dt)
- box1:draw()
- ground1:draw()
- ground2:draw()
-
-- TODO: Take care of weird displays
lg.setCanvas()
local scr_width, scr_height = lg.getDimensions()
diff --git a/res/img/tilesets.png b/res/img/tilesets.png
index 4a431fe..d6f4521 100644
--- a/res/img/tilesets.png
+++ b/res/img/tilesets.png
Binary files differ
diff --git a/src/objs/player.lua b/src/objs/player.lua
index 74d28e8..4f078f0 100644
--- a/src/objs/player.lua
+++ b/src/objs/player.lua
@@ -19,6 +19,23 @@ function player_movement_sys(player, dt)
inpx, inpy = normalize(inpx, inpy)
player.vx = dlerp(player.vx, inpx * PLAYER_SPEED, 25 * dt)
player.vy = dlerp(player.vy, inpy * PLAYER_SPEED, 25 * dt)
+
+ if is_input_pressed("Right_Click") then
+ local scn = get_current_scene()
+ assert(scn, "no scene set.")
+
+ local mx, my = get_mouse_pos()
+ local tx, ty = to_tile_coords(mx, my)
+ set_tile(scn.tilemap, tx, ty, 1)
+ end
+ if is_input_pressed("Left_Click") then
+ local scn = get_current_scene()
+ assert(scn, "no scene set.")
+
+ local mx, my = get_mouse_pos()
+ local tx, ty = to_tile_coords(mx, my)
+ remove_tile(scn.tilemap, tx, ty)
+ end
end
function new_player(x, y)
@@ -32,3 +49,4 @@ function new_player(x, y)
add_comp(ent, "Player")
add_comp(ent, "Sprite", "res/img/player.ase")
end
+
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
diff --git a/src/textures.lua b/src/textures.lua
index 0738912..8cf98a8 100644
--- a/src/textures.lua
+++ b/src/textures.lua
@@ -73,9 +73,7 @@ function load_textures_from(path)
load_textures_from(filepath)
else
if filepath:match("%.png$") then
- local name = string.gsub(filepath, ".png", "")
- name = string.gsub(name, "assets/images/", "")
- img_bank[name] = lg.newImage(filepath)
+ img_bank[filepath] = lg.newImage(filepath)
elseif filepath:match("%.ase$") then
load_ase(filepath)
end
diff --git a/src/utils.lua b/src/utils.lua
index b2186cc..29c3024 100644
--- a/src/utils.lua
+++ b/src/utils.lua
@@ -92,3 +92,10 @@ function rotate_vec(x, y, r)
return math.cos(angle) * len, math.sin(angle) * len
end
+function table.shallow_copy(t)
+ local t2 = {}
+ for k,v in pairs(t) do
+ t2[k] = v
+ end
+ return t2
+end