Skip to content

Instantly share code, notes, and snippets.

@Minotorious
Last active January 18, 2021 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Minotorious/907e11df1706af4fad644b162c947585 to your computer and use it in GitHub Desktop.
Save Minotorious/907e11df1706af4fad644b162c947585 to your computer and use it in GitHub Desktop.
A custom component for activating a particle emitter when a villager walks by an object in Foundation by Polymorph Games
--[[---------------------------------------------------------------------------\
| ||\\ //|| /|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\ |
| || \\ // || (o_ / | CUSTOM COMPONENT | |
| || \\// || //\/ | ---- | |
| || \/ || V_/_ | PROXIMITY TRIGGER | |
| || || |‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗/ |
\---------------------------------------------------------------------------]]--
--[[ Anyone is free to use this code for their mods so long as
credits are attributed to Minotorious in form of a link to
his profile on mod.io: https://mod.io/members/minotorious ]]--
local proximity = foundation.createMod();
local COMP_PROXIMITY_TRIGGER = {
TypeName = "COMP_PROXIMITY_TRIGGER",
ParentType = "COMPONENT",
Properties = {
{ Name = "Emitting", Type = "boolean", Default = false },
{ Name = "EmitterNodeName", Type = "string", Default = "NAME_OF_YOUR_EMITTER_NODE_IN_THE_FBX" }
}
}
function COMP_PROXIMITY_TRIGGER:create()
self.timer = 0
end
function COMP_PROXIMITY_TRIGGER:update()
if self.timer >= 0 then
local dt = self:getLevel():getDeltaTime()
self.timer = self.timer - dt
elseif self.timer < 0 then
if self.Emitting == true then
self:getOwner():getParent():forEachChild(
function(child)
if child.Name == self.EmitterNodeName then
local emitter = child:getComponent("COMP_PARTICLE_EMITTER")
if emitter.IsEmitting == true then
emitter.IsEmitting = false
self.Emitting = false
end
end
end
)
end
end
if self.Emitting == false then
local pos1 = self:getOwner():getGlobalPosition()
self:getLevel():getComponentManager("COMP_AGENT"):getAllComponent():forEach(
function(comp)
local pos2 = comp:getOwner():getGlobalPosition()
local distance = math.sqrt( (pos1.x - pos2.x)^2 + (pos1.y - pos2.y)^2 + (pos1.z - pos2.z)^2 )
if distance < 4 then
if self.timer <= 0 then
self.timer = 5
self:getOwner():getParent():forEachChild(
function(child)
if child.Name == self.EmitterNodeName then
local emitter = child:getComponent("COMP_PARTICLE_EMITTER")
if emitter.IsEmitting == false then
emitter.IsEmitting = true
self.Emitting = true
end
end
end
)
end
end
end
)
end
end
proximity:registerClass(COMP_PROXIMITY_TRIGGER)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment