Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module.Store("Fragmentation", [[
- Fragmentation.AXIS_DATA = {
- {"X", Vector3.new(1, 0, 0)},
- {"Y", Vector3.new(0, 1, 0)},
- {"Z", Vector3.new(0, 0, 1)}
- }
- function Fragmentation.DamageRegion(region, fragmentationSize, ignoreDescendantsInstance)
- local parts = Workspace:FindPartsInRegion3(region, ignoreDescendantsInstance, 100)
- local splitCount = 0
- for _, part in ipairs(parts) do
- if not Utility.SafeIsA(part, "Terrain") and Fragmentation.SplitPart(part, fragmentationSize) then
- splitCount = splitCount + 1
- end
- end
- return splitCount
- end
- function Fragmentation.DamageRay(ray, lengthMultiplier, fragmentationSizeFunction, ignoreDescendantsInstance)
- local part, hitPoint
- local rayDirection = ray.Direction
- local shiftedRay = ray
- for attemptCount = 1, lengthMultiplier do
- part, hitPoint = Workspace:FindPartOnRay(shiftedRay, ignoreDescendantsInstance)
- if part then
- break
- else
- shiftedRay = Ray.new(shiftedRay.Origin + rayDirection, rayDirection)
- end
- end
- if part then
- if Utility.SafeIsA(part, "Terrain") then
- local cellPosition = part:WorldToCellPreferSolid(hitPoint)
- part:SetCell(cellPosition.X, cellPosition.Y, cellPosition.Y, Enum.CellMaterial.Empty, Enum.CellBlock.Solid, Enum.CellOrientation.X)
- return true
- else
- return Fragmentation.SplitPart(part, fragmentationSizeFunction((hitPoint - ray.Origin).magnitude))
- end
- end
- return false
- end
- function Fragmentation.SplitPart(part, fragmentationSize)
- pcall(part.BreakJoints, part)
- local isBlock = Utility.SafeIsA(part, "Part") and part.Shape == Enum.PartType.Block
- local mass = part:GetMass()
- local size = part.Size
- if (isBlock and ((size.X < fragmentationSize and size.Y < fragmentationSize and size.Z < fragmentationSize)
- or (not part.Anchored and mass < 750))) or (not isBlock and mass < 250000) then
- pcall(Game.Destroy, part)
- elseif isBlock then
- local parts = {part}
- part.FormFactor = Enum.FormFactor.Custom
- local model = Instance.new("Model", part.Parent)
- model.Name = "Fragments"
- local partClone = Instance.new("Part")
- -- NOTE: this custom cloning function solves problems with children in the part (especially parts inside the part).
- -- surface inputs and parameters are ignored since fragmentation messes up the structure anyway.
- -- formfactor is is ignored too because it should always be Custom for fragmentation.
- -- shape is ignored too because block is default and it has already been established that the part is a block.
- -- cframe is ignored too because it is set later on.
- partClone.Anchored = part.Anchored
- partClone.Archivable = part.Archivable
- partClone.BackSurface = part.BackSurface
- partClone.BottomSurface = part.BottomSurface
- partClone.BrickColor = part.BrickColor
- partClone.CanCollide = part.CanCollide
- partClone.Elasticity = part.Elasticity
- partClone.FormFactor = Enum.FormFactor.Custom
- partClone.Friction = part.Friction
- partClone.FrontSurface = part.FrontSurface
- partClone.LeftSurface = part.LeftSurface
- partClone.Locked = part.Locked
- partClone.Material = part.Material
- partClone.Reflectance = part.Reflectance
- partClone.RightSurface = part.RightSurface
- partClone.RotVelocity = part.RotVelocity
- partClone.Size = part.Size
- partClone.TopSurface = part.TopSurface
- partClone.Transparency = part.Transparency
- partClone.Velocity = part.Velocity
- for _, data in ipairs(Fragmentation.AXIS_DATA) do
- local axisName, axisNormal = data[1], data[2]
- local axisSize = size[axisName]
- if axisSize >= fragmentationSize then
- size = (Vector3.new(1, 1, 1) - 0.5 * data[2]) * size
- partClone.size = size
- for partIndex = 1, #parts do
- local part = parts[partIndex]
- local cframe = part.CFrame
- part.Size = size
- local clone = partClone:Clone()
- part.CFrame = cframe * CFrame.new((-0.25 * axisSize) * axisNormal)
- clone.CFrame = cframe * CFrame.new((0.25 * axisSize) * axisNormal)
- clone.Parent = model
- parts[#parts + 1] = clone
- end
- end
- end
- for _, part in ipairs(parts) do
- part:MakeJoints()
- end
- else
- return false
- end
- return true
- end
- ]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement