diff --git a/init.lua b/init.lua index 2acf119..08d3a1e 100644 --- a/init.lua +++ b/init.lua @@ -78,6 +78,9 @@ function _batteries:export() --overwrite assert wholesale (it's compatible) assert = self.assert + --like ipairs, but in reverse + ripairs = self.tablex.ripairs + --export the whole library to global `batteries` batteries = self diff --git a/intersect.lua b/intersect.lua index 18b3a3c..f77d7b5 100644 --- a/intersect.lua +++ b/intersect.lua @@ -17,6 +17,7 @@ local path = (...):gsub("intersect", "") local vec2 = require(path .. "vec2") +local mathx = require(path .. "mathx") --module storage local intersect = {} @@ -337,10 +338,10 @@ function intersect.aabb_aabb_collide(a_pos, a_hs, b_pos, b_hs, into) --actually collided if abs_amount.x <= abs_amount.y then --x min - res = into:scalar_set(abs_amount.x * math.sign(delta.x), 0) + res = into:scalar_set(abs_amount.x * mathx.sign(delta.x), 0) else --y min - res = into:scalar_set(0, abs_amount.y * math.sign(delta.y)) + res = into:scalar_set(0, abs_amount.y * mathx.sign(delta.y)) end end return res diff --git a/tablex.lua b/tablex.lua index 3719d88..effab38 100644 --- a/tablex.lua +++ b/tablex.lua @@ -417,4 +417,19 @@ function tablex.unpack8(t) return t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8] end +--internal: reverse iterator function +local function _ripairs_iter(t, i) + i = i - 1 + local v = t[i] + if v then + return i, v + end +end + +--iterator that works like ipairs, but in reverse order, with indices from #t to 1 +--similar to ipairs, it will only consider sequential until the first nil value in the table. +function tablex.ripairs(t) + return _ripairs_iter, t, #t + 1 +end + return tablex