garden/entity.lua
2025-01-06 20:21:23 +09:00

73 lines
1.3 KiB
Lua

local window = require('window')
-- base prototype of all drawn things
local m = {
_t = 'entity',
x = 0,
y = 0,
width = 0,
height = 0,
}
m.__index = m
function m:from(t)
t.__index = t
return setmetatable(t, self)
end
function m:new()
return self:from({})
end
function m:setpos(x, y)
self.x = x
self.y = y
end
local function draw_line(l)
local mode = l[1]
local segs = l[2]
if mode == 'line' then
love.graphics.line(segs)
elseif mode == 'fill' then
local ts = love.math.triangulate(segs)
love.graphics.setColor(0, 0, 0)
for i, t in ipairs(ts) do
love.graphics.polygon('fill', t)
end
love.graphics.setColor(1, 1, 1)
love.graphics.polygon('line', segs)
else
error(string.format("mode must be one of line or fill, not %s", mode))
end
end
function m:draw_lines(lines)
love.graphics.push('all')
love.graphics.translate(self.x, self.y)
love.graphics.setLineWidth(2)
if type(lines[1]) == 'string' then
draw_line(lines)
else
for _, l in ipairs(lines) do
draw_line(l)
end
end
love.graphics.pop()
end
function m:draw()
self:draw_lines(self.lines)
end
function m:install(room, x, y)
self:setpos(x, y)
room:insert(self)
end
return m