diff --git a/main.lua b/main.lua index 9fa8313..fca4c60 100644 --- a/main.lua +++ b/main.lua @@ -41,7 +41,7 @@ inventory.draw = window.draw(function (self) end) function inventory:pickup(item) - self.items[item.name] = item + table.insert(self.items, item) end log = { diff --git a/util.lua b/util.lua index 16ee1e3..4e2bb61 100644 --- a/util.lua +++ b/util.lua @@ -1,5 +1,17 @@ local m = {} +local function rext(a, i) + i = i - 1 + local v = a[i] + if v then + return i, v + end +end + +function m.ipairs_rev(a) + return rext, a, (#a + 1) +end + function m.between(x, a, b) return a <= x and x <= b end @@ -8,3 +20,5 @@ function m.inside(px, py, x, y, w, h) return m.between(px, x, x+w) and m.between(py, y, y+h) end + +return m diff --git a/window.lua b/window.lua index 5671777..c5a077d 100644 --- a/window.lua +++ b/window.lua @@ -1,3 +1,4 @@ +local util = require('util') -- function generator for window functions local w = {} @@ -17,7 +18,10 @@ function w.mousepressed(f) if 0 <= x and x <= self.width and 0 <= y and y <= self.height then f(self, x, y, button) + return true end + + return false end end @@ -26,8 +30,10 @@ function w.propagate(tnames) local x, y = x - self.x, y - self.y for _, tname in ipairs(tnames) do - for i, v in pairs(self[tname]) do - v:mousepressed(x, y, button) + for i, v in util.ipairs_rev(self[tname]) do + if v:mousepressed(x, y, button) then + return + end end end end