garden/room/vivarium_table.lua
2025-01-14 19:32:59 +09:00

162 lines
3.5 KiB
Lua

local util = require("util")
local window = require("window")
local entity = require("entity")
local room = require("room")
local edge = require("edge")
local button = require("button")
local flask = require("item/flask")
local function getColor(c)
local t = {
Red = { 1, 0, 0 },
Green = { 0, 1, 0 },
Blue = { 0, 0, 1 },
}
if type(c) == "string" then
return t[c]
else
return c
end
end
local mixer = entity:from{
width = 130,
height = 110,
con_width = 60,
con_height = 100,
colours = {},
timer = 0,
anim = 0,
}
function mixer:empty()
self.colours = {}
self.anim = 0
end
local mempty = button.new("Empty", function ()
mixer:empty()
end,
75, 5, 50, 45)
local mmix = button.new("Mix", function ()
local len = #mixer.colours
local c = {0, 0, 0}
for k, v in ipairs(mixer.colours) do
local v = getColor(v)
for i = 1, 3 do
c[i] = c[i] + v[i]
end
end
for i = 1, 3 do
c[i] = c[i] / len
end
mixer.colours = {}
for i = 1, len do
mixer.colours[i] = c
end
end,
75, 60, 50, 45)
mixer.draw = window.draw(function (self)
local y = self.con_height + 5
for i, v in ipairs(self.colours) do
love.graphics.setColor(getColor(v))
local h = 5
if i == #self.colours then
h = h * self.anim
end
y = y - h
love.graphics.rectangle('fill', 5, y, self.con_width, h)
end
love.graphics.setColor(1, 1, 1)
mempty:draw()
mmix:draw()
love.graphics.rectangle('line', 5, 5, self.con_width, self.con_height)
love.graphics.rectangle('line', 0, 0, self.width, self.height)
end)
mixer.mousepressed = window.mousepressed(function (self, x, y, button)
if util.inside(x, y, 5, 5, self.con_width, self.con_height) and #self.colours > 0 then
local f = flask:from{ colour = getColor(self.colours[1]) }
f:itemMode()
inventory:pickup(f)
self:empty()
return true
end
window.iterate({mempty, mmix}, x, y, button)
end)
mixer.update = function (self, dt)
self.timer = self.timer + dt
if self.timer > 1/20 then
self.timer = self.timer - 1/20
self.anim = self.anim + 1/20
if self.anim > 1 then
self.anim = 1
end
end
end
function mixer:add(colour)
table.insert(self.colours, colour)
self.anim = 0
end
local vial = entity:from {
width = 20,
height = 60,
colour = ""
}
vial.draw = window.draw(function (self)
love.graphics.setColor(getColor(self.colour))
love.graphics.rectangle('fill', 0, 0, 20, 50)
love.graphics.arc('fill', 'open', 10, 50, 10, 0, math.pi)
love.graphics.setColor(1, 1, 1)
love.graphics.line(0, 50, 0, 0, 20, 0, 20, 50)
love.graphics.arc('line', 'open', 10, 50, 10, 0, math.pi)
end)
vial.mousepressed = window.mousepressed(function (self, x, y, button)
mixer:add(self.colour)
end)
local m = room:new()
mixer:install(m, 160, 40)
vial:from{ colour = "Red" }:install(m, 20, 40)
vial:from{ colour = "Green" }:install(m, 60, 40)
vial:from{ colour = "Blue" }:install(m, 100, 40)
edge.set(m, 'down', "Vivarium")
m.draw = window.draw(function (self)
love.graphics.setLineWidth(3)
love.graphics.line(0, 200, 320, 200)
love.graphics.rectangle('line', 10, 200, 20, 40)
love.graphics.rectangle('line', 290, 200, 20, 40)
for i, v in ipairs(self.objects) do
v:draw()
end
self.edges[1]:draw()
end)
m.update = function (self, dt)
mixer:update(dt)
end
return m