util.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. local util = {}
  2. function util.lerp(x, y, z)
  3. return x + (y - x) * z
  4. end
  5. function util.anglediff(d1, d2) return math.rad((((math.deg(d2) - math.deg(d1) % 360) + 540) % 360) - 180) end
  6. function util.inside(px, py, rx, ry, rw, rh) return px >= rx and px <= rx + rw and py >= ry and py <= ry + rh end
  7. function util.insideCircle(px, py, cx, cy, r) return math.distance(px, py, cx, cy) <= r end
  8. function util.sign(x) if x == 0 then return 0 else return lib.lume.sign(x) end end
  9. function util.vector(...) return util.distance(...), util.angle(...) end
  10. function util.anglerp(d1, d2, z) return d1 + (util.anglediff(d1, d2) * z) end
  11. function util.choose(...)
  12. return lib.lume.randomchoice({...})
  13. end
  14. function util.dx(dis, dir)
  15. return dis * math.cos(dir)
  16. end
  17. function util.dy(dis, dir)
  18. return dis * math.sin(dir)
  19. end
  20. function util.copy(x, seen)
  21. seen = seen or {}
  22. local t = type(x)
  23. if t ~= 'table' then return x end
  24. if seen[x] then return seen[x] end
  25. local y = {}
  26. seen[x] = y
  27. for k, v in next, x, nil do y[k] = util.copy(v, seen) end
  28. setmetatable(y, getmetatable(x))
  29. return y
  30. end
  31. function util.interpolateTable(t1, t2, z)
  32. local interp = util.copy(t1)
  33. for k, v in pairs(interp) do
  34. if t2[k] then
  35. if type(v) == 'table' then interp[k] = util.interpolateTable(t1[k], t2[k], z)
  36. elseif type(v) == 'number' then
  37. if k == 'angle' then interp[k] = math.anglerp(t1[k], t2[k], z)
  38. else interp[k] = util.lerp(t1[k], t2[k], z) end
  39. end
  40. end
  41. end
  42. return interp
  43. end
  44. function util.get(t, path)
  45. local pieces = {}
  46. path:gsub('([^%.]+)', function(piece)
  47. table.insert(pieces, piece)
  48. end)
  49. local result = t
  50. for i = 1, #pieces do
  51. result = result[pieces[i]]
  52. end
  53. return result
  54. end
  55. function util.merge(t1, t2)
  56. if not t2 then return t1 end
  57. if not t1 then return t2 end
  58. for k, v in pairs(t1) do
  59. t2[k] = v
  60. end
  61. return t2
  62. end
  63. function util.isa(object, class)
  64. return getmetatable(object) and getmetatable(object).__index == class
  65. end
  66. function g.drawCenter(image, size, x, y, a, sx, sy)
  67. local scale = size / image:getWidth()
  68. sx = sx or 1
  69. sy = sy or 1
  70. g.draw(image, x, y, a, scale * sx, scale * sy, image:getWidth() / 2, image:getHeight() / 2)
  71. end
  72. function g.white(alpha)
  73. g.setColor(255, 255, 255, alpha)
  74. return g
  75. end
  76. function g.alpha(color, alpha, ...)
  77. if type(color) == 'table' then
  78. local result = util.copy(color)
  79. result[4] = alpha
  80. return result
  81. end
  82. return color, alpha, ...
  83. end
  84. function g.imageScale(image, size)
  85. return size / image:getWidth()
  86. end
  87. function lib.tick.getLerpFactor(factor)
  88. return ((1 / lib.tick.rate) * factor) * lib.tick.rate
  89. end
  90. function util.timeSince(tick)
  91. return (lib.tick.index - tick) * lib.tick.rate
  92. end
  93. return util