75 lines
2.4 KiB
Lua
75 lines
2.4 KiB
Lua
local AnimationNode = require "lib.animation_node"
|
|
local easing = require "lib.utils.easing"
|
|
|
|
--- @class LightBehavior : Behavior
|
|
--- @field intensity number
|
|
--- @field color Vec3
|
|
--- @field seed integer
|
|
--- @field colorAnimationNode? AnimationNode
|
|
--- @field private animateColorCallback? fun(): nil
|
|
--- @field targetColor? Vec3
|
|
--- @field sourceColor? Vec3
|
|
local behavior = {}
|
|
behavior.__index = behavior
|
|
behavior.id = "light"
|
|
|
|
---@param values {intensity: number?, color: Vec3?, seed: integer?}
|
|
---@return LightBehavior
|
|
function behavior.new(values)
|
|
return setmetatable({
|
|
intensity = values.intensity or 1,
|
|
color = values.color or Vec3 { 1, 1, 1 },
|
|
seed = values.seed or math.random(math.pow(2, 16))
|
|
}, behavior)
|
|
end
|
|
|
|
function behavior:update(dt)
|
|
if not self.colorAnimationNode then return end
|
|
local delta = self.targetColor - self.sourceColor
|
|
self.color = self.sourceColor + delta * self.colorAnimationNode:getValue()
|
|
self.colorAnimationNode:update(dt)
|
|
end
|
|
|
|
--- @TODO: refactor
|
|
function behavior:animateColor(targetColor)
|
|
if self.colorAnimationNode then self.colorAnimationNode:finish() end
|
|
self.colorAnimationNode = AnimationNode {
|
|
function(_) end,
|
|
easing = easing.easeInQuad,
|
|
duration = 800,
|
|
onEnd = function()
|
|
if self.animateColorCallback then self.animateColorCallback() end
|
|
end
|
|
}
|
|
self.colorAnimationNode:run()
|
|
self.sourceColor = self.color
|
|
self.targetColor = targetColor
|
|
|
|
return function(callback)
|
|
self.animateColorCallback = callback
|
|
end
|
|
end
|
|
|
|
function behavior:draw()
|
|
local positioned = self.owner:has(Tree.behaviors.positioned)
|
|
if not positioned then return end
|
|
|
|
Tree.level.camera:attach()
|
|
love.graphics.setCanvas(Tree.level.render.textures.lightLayer)
|
|
local shader = Tree.assets.files.shaders.light
|
|
shader:send("color", { self.color.x, self.color.y, self.color.z })
|
|
shader:send("time", love.timer.getTime() + self.seed)
|
|
love.graphics.setShader(shader)
|
|
love.graphics.draw(Tree.assets.files.masks.circle128, positioned.position.x - self.intensity / 2,
|
|
positioned.position.y - self.intensity / 2, 0, self.intensity / 128,
|
|
self.intensity / 128)
|
|
|
|
love.graphics.setBlendMode("alpha")
|
|
|
|
love.graphics.setShader()
|
|
love.graphics.setCanvas()
|
|
Tree.level.camera:detach()
|
|
end
|
|
|
|
return behavior
|