summaryrefslogtreecommitdiff
path: root/liquid.lua
diff options
context:
space:
mode:
Diffstat (limited to 'liquid.lua')
-rw-r--r--liquid.lua98
1 files changed, 98 insertions, 0 deletions
diff --git a/liquid.lua b/liquid.lua
new file mode 100644
index 0000000..cdddb02
--- /dev/null
+++ b/liquid.lua
@@ -0,0 +1,98 @@
+local Liquid = {}
+Liquid.__index = Liquid
+
+local function buildWorldPolygonPoints(entity)
+ local points = {}
+
+ if not entity.polygon then
+ return points
+ end
+
+ for i, point in ipairs(entity.polygon) do
+ points[#points+1] = {
+ x = entity.x + point.x,
+ y = entity.y + point.y
+ }
+ end
+
+ return points
+end
+
+function Liquid:new(source)
+ local self = setmetatable({}, Liquid)
+ self.entity = source
+ self.x = source.x or 0
+ self.y = source.y or 0
+ self.width = source.width or 0
+ self.height = source.height or 0
+ self.shape = source.shape
+ self.polygon = source.polygon or {}
+ self.worldPolygon = buildWorldPolygonPoints(source)
+ self.properties = source.properties or {}
+ self.type = "liquid"
+ self.triangles = {}
+
+ local flat = {}
+ for _, p in ipairs(self.worldPolygon) do
+ table.insert(flat, p.x)
+ table.insert(flat, p.y)
+ end
+
+ if #flat >= 6 then
+ self.triangles = love.math.triangulate(flat)
+ end
+ return self
+end
+
+function Liquid:getWorldPolygon()
+ return self.worldPolygon
+end
+
+function Liquid:containsEntityFully(entity)
+ local corners = getEntityCorners(entity)
+
+ for _, c in ipairs(corners) do
+ if not self:containsPoint(c.x, c.y) then
+ return false
+ end
+ end
+
+ return true
+end
+
+function Liquid:containsPoint(px, py)
+ local points = self.worldPolygon
+ local count = #points
+ if count < 3 then return false end
+
+ local inside = false
+ local j = count
+ for i = 1, count do
+ local xi, yi = points[i].x, points[i].y
+ local xj, yj = points[j].x, points[j].y
+ local intersects = ((yi > py) ~= (yj > py))
+ and (px < (xj - xi) * (py - yi) / ((yj - yi) + 1e-9) + xi)
+ if intersects then
+ inside = not inside
+ end
+ j = i
+ end
+ return inside
+end
+
+function Liquid:containsEntity(entity)
+ if not entity then return false end
+ local ex = entity.x or 0
+ local ey = entity.y or 0
+ local ew = entity.width or 0
+ local eh = entity.height or 0
+
+ local sampleX = ex + ew * 0.5
+ local sampleY = ey + eh
+ return self:containsPoint(sampleX, sampleY)
+end
+
+function Liquid:update(dt)
+end
+
+return Liquid \ No newline at end of file