Merge PR #39: add ripairs

minor: touched up whitespace/naming/comments
This commit is contained in:
Max Cahill 2021-11-11 13:47:24 +11:00
commit 63a1cf5438
3 changed files with 21 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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