58 lines
1.9 KiB
Lua
58 lines
1.9 KiB
Lua
--- @class LightBehavior : Behavior
|
|
--- @field intensity number
|
|
--- @field color Vec3
|
|
--- @field seed integer
|
|
--- @field colorAnimationNode? AnimationNode
|
|
--- @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
|
|
|
|
function behavior:animateColor(targetColor, animationNode)
|
|
if self.colorAnimationNode then self.colorAnimationNode:finish() end
|
|
self.colorAnimationNode = animationNode
|
|
self.sourceColor = self.color
|
|
self.targetColor = targetColor
|
|
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
|