local util = {} function util.merge2(t1, t2) for k, v in pairs(t2) do if (type(v) == "table") and (type(t1[k] or false) == "table") then merge(t1[k], t2[k]) else t1[k] = v end end return t1 end function util.merge(t1, ...) for _, t2 in ipairs({...}) do t1 = util.merge2(t1, t2) end return t1 end function util.lerp(a, b, t) return a + (b - a) * t end function util.lerpAngle(a, b, k) local pi = math.pi if (math.abs(a - b) > pi) then if (b > a) then a = a + pi * 2 else b = b + pi * 2 end end local value = (a + ((b - a) * k)) local rangeZero = pi * 2 if (value >= 0 and value <= pi * 2) then return value end return (value % rangeZero) end function util.rotateVector(x, y, r) local a = math.atan2(y, x) local d = math.sqrt(x * x + y * y) a = a + r return d * math.cos(a), d * math.sin(a) end function util.foreachi(t, f) for i = 1, #t do f(t[i], i) end end function util.foreachirev(t, f) for i = #t, 1, -1 do f(t[i], i) end end function util.foreach(t, f) for k, v in pairs(t) do f(v, k) end end function util.withShader(shader, f) local _shader = lovr.graphics.getShader() lovr.graphics.setShader(shader) f() lovr.graphics.setShader(_shader) end return util