Skip to content

Commit

Permalink
Update Data/Settings Handling
Browse files Browse the repository at this point in the history
- Refactored the `Emit` function to accept Data tables, merges changes with the clone of the main settings
- Droplets are not limited to one mesh now, which allows for you to mix between Decals and default Droplets
  • Loading branch information
rotntake committed Aug 6, 2024
1 parent d4037d4 commit 139a92d
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 100 deletions.
Binary file modified src/Assets.rbxm
Binary file not shown.
67 changes: 63 additions & 4 deletions src/Functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ local Assets = ParentClass.Assets
-- Asset definitions
local Images = Assets.Images
local Essentials = Assets.Essentials
local Effects = Assets.Effects

-- Effect definitions
local TrailEffects = Effects.Trail
local ImpactEffects = Effects.Impact

-- Essential definitions
local FastCast = require(Essentials.FastCast)
Expand Down Expand Up @@ -96,6 +101,62 @@ function Functions.SetupBehavior(Cache, CastParams): FastCast.Behavior
return Behavior
end

--[[
Clones and parents Droplet effects from a template part.
]]
function Functions.CreateEffects(Parent: MeshPart, ImpactName: string)
-- Variable definitions
local Trail = TrailEffects:Clone()

local Attachment0 = Instance.new("Attachment")
local Attachment1 = Instance.new("Attachment")
local ImpactAttachment = Instance.new("Attachment")

-- Update Trail-related properties
Trail.Attachment0 = Attachment0
Trail.Attachment1 = Attachment1

Attachment1.Position = Vector3.new(0.037, 0, 0)
Attachment0.Name = "Attachment0"
Attachment1.Name = "Attachment1"

Attachment0.Parent = Parent
Attachment1.Parent = Parent
Trail.Parent = Parent

-- Update Impact-related properties
for _, Effect in ipairs(ImpactEffects:GetChildren()) do
local Clone = Effect:Clone()
Clone.Parent = ImpactAttachment
end

ImpactAttachment.Name = ImpactName
ImpactAttachment.Parent = Parent
ImpactAttachment.Orientation = Vector3.new(0, 0, 0)
end

--[[
Returns an empty object template that's going to be used as a droplet.
]]
function Functions.GetDroplet(ImpactName: string, IsDecal: boolean): {}
-- Variable definitions
local Droplet = Instance.new("MeshPart")

-- Update properties
Droplet.Size = Vector3.new(0.1, 0.1, 0.1)
Droplet.Transparency = 0.25
Droplet.Material = Enum.Material.Glass

Droplet.Anchored = false
Droplet.CanCollide = false
Droplet.CanQuery = false
Droplet.CanTouch = false

-- Export droplet
Functions.CreateEffects(Droplet, ImpactName)
return Droplet
end

--[[
Returns a folder that handles droplets; If it doesn't exist,
make a new one in Workspace.Terrain.
Expand Down Expand Up @@ -268,13 +329,11 @@ end
Provides the target angles; utilized to
assign the orientation to base position or CFrame.
]]
function Functions.GetAngles(IsDecal: boolean): CFrame
function Functions.GetAngles(IsDecal: boolean, RandomAngles: boolean): CFrame
-- Variable definitions
local RandomAngle = Functions.NextNumber(0, 180)

local AngleX = (IsDecal and -math.pi / 2 or math.pi / 2)

local AngleY = (IsDecal and RandomAngle or 0)
local AngleY = (RandomAngles and RandomAngle or 0)

-- Export angles
return CFrame.Angles(AngleX, AngleY, 0)
Expand Down
147 changes: 83 additions & 64 deletions src/Operator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local Assets = ParentClass.Assets
-- Asset definitions
local Sounds = Assets.Sounds
local Essentials = Assets.Essentials
local Meshes = Assets.Meshes

-- Sound definitions
local EndFolder = Sounds.End:GetChildren()
Expand All @@ -26,6 +27,15 @@ local FastCast = require(Essentials.FastCast)
-- Globals
local Unpack = table.unpack

-- Constants definition
local TypeAttribute = "Type"
local DecayAttribute = "Decaying"
local ExpandAttribute = "Expanding"
local MeshMap = {
Default = Meshes.Droplet,
Decal = Meshes.Decal,
}

-- Class definition
local Operator = {}
Operator.__index = Operator
Expand All @@ -37,7 +47,6 @@ Operator.__index = Operator
function Operator.new(Class)
local self = setmetatable({
Handler = Class.ActiveHandler,
Types = Class.Types,
}, Operator)

return self, self:Initialize(), self:InitializeCast()
Expand All @@ -51,21 +60,20 @@ function Operator:Initialize()
-- Variable definitions
local Handler: Settings.Class = self.Handler
local FolderName: string = Handler.FolderName
local Types: {} = self.Types

-- Essential definitions
local Type = Handler.Type
local Limit = Handler.Limit
local CastParams = Handler.RaycastParams

local Folder = Functions.GetFolder(FolderName)
local Object = Types[Type]:Clone()
local Object = Functions.GetDroplet(Handler.SplashName)

-- Class definitions
local Cache = PartCache.new(Object, Limit, Folder)

-- Insert variables
Functions.MultiInsert(self, {
Registry = {},
Droplet = Object,
Cache = Cache,
Container = Folder,
Expand Down Expand Up @@ -93,10 +101,6 @@ function Operator:InitializeCast()
local LengthChanged = Caster.LengthChanged
local RayHit = Caster.RayHit

-- Info definitions
local Tweens = Handler.Tweens
local Landed = Tweens.Landed

-- Caster Listeners
LengthChanged:Connect(function(_, Origin, Direction, Length, _, Object: BasePart)
if not Object then
Expand All @@ -121,11 +125,12 @@ function Operator:InitializeCast()
end

-- Options definitions
local Size = Handler.StartingSize
local SizeRange = Handler.DefaultSize
local Distance = Handler.Distance
local Expansion = Handler.Expansion
local IsDecal = Handler.Type == "Decal"
local RegistryData = self.Registry[Object] or Handler
local Size = RegistryData.StartingSize
local SizeRange = RegistryData.DefaultSize
local Distance = RegistryData.Distance
local Expansion = RegistryData.Expansion
local IsDecal = RegistryData.Type == "Decal"

-- Variable definitions
local CastInstance = RaycastResult.Instance
Expand All @@ -135,38 +140,43 @@ function Operator:InitializeCast()
local VectorSize = Functions.GetVector(SizeRange)
local GoalSize = Functions.RefineVectors(IsDecal, Vector3.new(VectorSize.X, VectorSize.Y / 4, VectorSize.X))

local GoalAngles = Functions.GetAngles(IsDecal)
local GoalAngles = Functions.GetAngles(IsDecal, IsDecal)
local GoalCFrame = Functions.GetCFrame(Position, Normal, IsDecal) * GoalAngles

local ClosestPart = Functions.GetClosest(Object, Distance, Container)

local ExpansionLogic = (
Expansion
and ClosestPart
and (not ClosestPart:GetAttribute("Decaying") and not ClosestPart:GetAttribute("Expanding"))
and not ClosestPart:GetAttribute(DecayAttribute)
and not ClosestPart:GetAttribute(ExpandAttribute)
and ClosestPart:GetAttribute(TypeAttribute) == RegistryData.Type
)

-- Evaluates if the droplet is close to another pool
-- Clear the registry entry
self.Registry[Object] = nil

-- Evaluates if the droplet is close to another pool, if so, expand.
if ExpansionLogic then
self:Expanse(Object, ClosestPart, Velocity, GoalSize)
self:Expanse(Object, ClosestPart, Velocity, GoalSize, RegistryData)
return nil
end

-- Update properties
Object.Anchored = true
Object.Size = Size
Object.CFrame = GoalCFrame
Object.Transparency = Functions.NextNumber(Unpack(Handler.DefaultTransparency))
Object.Transparency = Functions.NextNumber(Unpack(RegistryData.DefaultTransparency))

--[[
Transitions the droplet into a pool,
then handles its later functionality.
(Decay, Sounds, etc...)
]]
Functions.CreateTween(Object, Landed, { Size = GoalSize }):Play()

self:HandleDroplet(Object)
self:HitEffects(Object, Velocity)
Transitions the droplet into a pool,
then handles its later functionality.
(Decay, Sounds, etc...)
]]
Functions.CreateTween(Object, RegistryData.Tweens.Landed, { Size = GoalSize }):Play()

self:HandleDroplet(Object, RegistryData)
self:HitEffects(Object, Velocity, RegistryData)
Functions.Weld(CastInstance, Object)

return nil
Expand All @@ -177,24 +187,27 @@ end
Emitter, emits a certain amount of droplets,
at a certain point of origin, with a certain given direction.
]]
function Operator:Emit(Origin: Vector3, Direction: Vector3)
function Operator:Emit(Origin: Vector3, Direction: Vector3, Data: Settings.Class?)
-- Class definitions
local Caster: FastCast.Class = self.Caster
local Behavior: FastCast.Behavior = self.Behavior
local Cache: PartCache.Class = self.Cache
local Handler: Settings.Class = self.Handler

local Clone = table.clone(Handler)
Clone:UpdateSettings(Data)
Data = Clone

-- Variable definitions
local DropletVelocity = Handler.DropletVelocity
local DropletVelocity = Data.DropletVelocity
local Velocity = Functions.NextNumber(Unpack(DropletVelocity)) * 10

local RandomOffset = Handler.RandomOffset
local OffsetRange = Handler.OffsetRange
local RandomOffset = Data.RandomOffset
local OffsetRange = Data.OffsetRange
local Position = Functions.GetVector(OffsetRange) / 10

-- Final definitions
local FinalPosition = Origin + Vector3.new(Position.X, 0, Position.Z)

local FinalStart = (RandomOffset and FinalPosition or Origin)

if #Cache.Open <= 0 then
Expand All @@ -205,25 +218,32 @@ function Operator:Emit(Origin: Vector3, Direction: Vector3)
local ActiveDroplet = Caster:Fire(FinalStart, Direction, Velocity, Behavior)

local RayInfo = ActiveDroplet.RayInfo
local Droplet: Instance = RayInfo.CosmeticBulletObject
local Droplet: MeshPart = RayInfo.CosmeticBulletObject

-- Update the mesh's look and color
Droplet:ApplyMesh(MeshMap[Data.Type])
Droplet.Color = Data.DropletColor

-- Assign the registry entry and update the attributes
self.Registry[Droplet] = Data
Droplet:SetAttribute(TypeAttribute, Data.Type)
Droplet:SetAttribute(DecayAttribute, false)
Droplet:SetAttribute(ExpandAttribute, false)

-- Execute essential functions
self:UpdateDroplet(Droplet)
self:UpdateDroplet(Droplet, Data)
Functions.PlaySound(Functions.GetRandom(StartFolder), Droplet)
end

--[[
A small function, designed to update the properties
of a recently emitted droplet.
]]
function Operator:UpdateDroplet(Object: BasePart)
-- Class definitions
local Handler: Settings.Class = self.Handler

function Operator:UpdateDroplet(Object: BasePart, Data: Settings.Class)
-- Variable definitions
local DropletTrail = Handler.Trail
local DropletVisible = Handler.DropletVisible
local IsDecal = Handler.Type == "Decal"
local DropletTrail = Data.Trail
local DropletVisible = Data.DropletVisible
local IsDecal = Data.Type == "Decal"

-- Object definitions
local Trail = Object:FindFirstChildOfClass("Trail")
Expand All @@ -240,21 +260,18 @@ end
Handles the given droplet/object after
it landed on a surface.
]]
function Operator:HandleDroplet(Object: BasePart)
-- Class definitions
local Handler: Settings.Class = self.Handler

function Operator:HandleDroplet(Object: BasePart, Data: Settings.Class)
-- Object definitions
local Trail = Object:FindFirstChildOfClass("Trail")

-- Variable definitions
local Tweens = Handler.Tweens
local DecayDelay = Handler.DecayDelay
local Tweens = Data.Tweens
local DecayDelay = Data.DecayDelay

local DecayInfo = Tweens.Decay
local DecayTime = Functions.NextNumber(Unpack(DecayDelay))

local ScaleDown = Handler.ScaleDown
local ScaleDown = Data.ScaleDown
local FinalSize = ScaleDown and Vector3.new(0.01, 0.01, 0.01) or Object.Size

-- Tween definitions
Expand All @@ -281,22 +298,21 @@ end
HitEffects, a sequence of effects to enhance
the visuals of the droplet->pool
]]
function Operator:HitEffects(Object, Velocity: Vector3)
-- Class definitions
local Handler: Settings.Class = self.Handler

function Operator:HitEffects(Object, Velocity: Vector3, Data: Settings.Class)
-- Variable definitions
local SplashName = Handler.SplashName
local SplashAmount = Handler.SplashAmount
local SplashByVelocity = Handler.SplashByVelocity
local Divider = Handler.VelocityDivider
local SplashName = Data.SplashName
local SplashAmount = Data.SplashAmount
local SplashByVelocity = Data.SplashByVelocity
local Divider = Data.VelocityDivider
local IsDecal = Data.Type == "Decal"

local Magnitude = Velocity.Magnitude
local FinalVelocity = Magnitude / Divider
local FinalAmount = (SplashByVelocity and FinalVelocity or Functions.NextNumber(Unpack(SplashAmount)))
local Splash = Object:FindFirstChild(SplashName)
local Splash: Attachment = Object:FindFirstChild(SplashName)

-- Execute essential functions
Splash.Orientation = Vector3.new(0, 0, IsDecal and 0 or 180)
Functions.PlaySound(Functions.GetRandom(EndFolder), Object)
Functions.EmitParticles(Splash, FinalAmount)
end
Expand All @@ -310,17 +326,20 @@ end
a threshold, then triggers changes
on the droplet & pool.
]]
function Operator:Expanse(Object: BasePart, ClosestPart: BasePart, Velocity: Vector3, Size: Vector3)
-- Self definitions
local Handler: Settings.Class = self.Handler

function Operator:Expanse(
Object: BasePart,
ClosestPart: BasePart,
Velocity: Vector3,
Size: Vector3,
Data: Settings.Class
)
-- Variable definitions
local Divider = Handler.ExpanseDivider
local MaximumSize = Handler.MaximumSize
local IsDecal = Handler.Type == "Decal"
local Divider = Data.ExpanseDivider
local MaximumSize = Data.MaximumSize
local IsDecal = Data.Type == "Decal"

-- Info definitions
local Tweens = Handler.Tweens
local Tweens = Data.Tweens
local Expand = Tweens.Expand

-- Value definitions
Expand Down
Loading

0 comments on commit 139a92d

Please sign in to comment.