Tykvesh

Bloomer equivalent for Dynamic Shadow

Recommended Posts

Tykvesh    663

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
Edited by Tykvesh
Note: DynamicShadow:GetSize does not exist in vanilla.
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now