Jump to content

Bloomer equivalent for Dynamic Shadow


Recommended Posts

This component lets DynamicShadow to stack from different sources similar to Bloomer/ColourAdder components.

783196374_DesktopScreenshot2021_08.30-22_27_22_09.png.b0b76554485e7323d04155235ab43838.png

local SourceModifierList = require "util/sourcemodifierlist"

local function onmounted(inst, data)
	if data.target ~= nil and data.target.DynamicShadow ~= nil then
		inst.components.dynamicshadow:PushShadow(data.target, data.target.DynamicShadow:GetSize())
	end
end

local function ondismounted(inst, data)
	if data.target ~= nil then
		inst.components.dynamicshadow:PopShadow(data.target)
	end
end

local function onequip(inst, data)
	if data.item ~= nil and data.item:HasTag("umbrella") then
		inst.components.dynamicshadow:PushShadow(data.item, inst.DynamicShadow:GetSize())
	end
end

local function onunequip(inst, data)
	if data.item ~= nil and data.item:HasTag("umbrella") then
		inst.components.dynamicshadow:PopShadow(data.item)
	end
end

--this name is probably fine since GroundCreep/groundcreep is a thing?
local DynamicShadow = Class(function(self, inst)
    self.inst = inst
	
	local width, height = inst.DynamicShadow:GetSize()
	self.width = SourceModifierList(inst, width, math.max)
	self.height = SourceModifierList(inst, height, math.max)
	
	if inst.components.rider ~= nil then
		inst:ListenForEvent("mounted", onmounted)
		inst:ListenForEvent("dismounted", ondismounted)
	end
	if inst.components.inventory ~= nil then
		inst:ListenForEvent("equip", onequip)
		inst:ListenForEvent("unequip", onunequip)
	end
end)

function DynamicShadow:PushShadow(source, width, height)
	self.width:SetModifier(source, width)
	self.height:SetModifier(source, height)
	self:Recalc()
end

function DynamicShadow:PopShadow(source)
	self.width:RemoveModifier(source)
	self.height:RemoveModifier(source)
	self:Recalc()
end

function DynamicShadow:GetSize()
	return self.width:Get(), self.height:Get()
end

function DynamicShadow:Recalc()
	self.inst.DynamicShadow:SetSize(self:GetSize())
end

function DynamicShadow:GetDebugString()
	local str = string.format("Current Size: (%.2f, %.2f)", self:GetSize())
	for k, v in pairs(self.width._modifiers) do
		str = str .. string.format("\n\t%s: (%.2f, %.2f)", tostring(k), self.width:CalculateModifierFromSource(k), self.height:CalculateModifierFromSource(k))
	end
	return str
end

return DynamicShadow
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...