Advertisement
epicodascripter

Untitled

Feb 12th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. Module.Store("Fragmentation", [[
  2. Fragmentation.AXIS_DATA = {
  3. {"X", Vector3.new(1, 0, 0)},
  4. {"Y", Vector3.new(0, 1, 0)},
  5. {"Z", Vector3.new(0, 0, 1)}
  6. }
  7. function Fragmentation.DamageRegion(region, fragmentationSize, ignoreDescendantsInstance)
  8. local parts = Workspace:FindPartsInRegion3(region, ignoreDescendantsInstance, 100)
  9. local splitCount = 0
  10. for _, part in ipairs(parts) do
  11. if not Utility.SafeIsA(part, "Terrain") and Fragmentation.SplitPart(part, fragmentationSize) then
  12. splitCount = splitCount + 1
  13. end
  14. end
  15. return splitCount
  16. end
  17. function Fragmentation.DamageRay(ray, lengthMultiplier, fragmentationSizeFunction, ignoreDescendantsInstance)
  18. local part, hitPoint
  19. local rayDirection = ray.Direction
  20. local shiftedRay = ray
  21. for attemptCount = 1, lengthMultiplier do
  22. part, hitPoint = Workspace:FindPartOnRay(shiftedRay, ignoreDescendantsInstance)
  23. if part then
  24. break
  25. else
  26. shiftedRay = Ray.new(shiftedRay.Origin + rayDirection, rayDirection)
  27. end
  28. end
  29. if part then
  30. if Utility.SafeIsA(part, "Terrain") then
  31. local cellPosition = part:WorldToCellPreferSolid(hitPoint)
  32. part:SetCell(cellPosition.X, cellPosition.Y, cellPosition.Y, Enum.CellMaterial.Empty, Enum.CellBlock.Solid, Enum.CellOrientation.X)
  33. return true
  34. else
  35. return Fragmentation.SplitPart(part, fragmentationSizeFunction((hitPoint - ray.Origin).magnitude))
  36. end
  37. end
  38. return false
  39. end
  40. function Fragmentation.SplitPart(part, fragmentationSize)
  41. pcall(part.BreakJoints, part)
  42. local isBlock = Utility.SafeIsA(part, "Part") and part.Shape == Enum.PartType.Block
  43. local mass = part:GetMass()
  44. local size = part.Size
  45. if (isBlock and ((size.X < fragmentationSize and size.Y < fragmentationSize and size.Z < fragmentationSize)
  46. or (not part.Anchored and mass < 750))) or (not isBlock and mass < 250000) then
  47. pcall(Game.Destroy, part)
  48. elseif isBlock then
  49. local parts = {part}
  50. part.FormFactor = Enum.FormFactor.Custom
  51. local model = Instance.new("Model", part.Parent)
  52. model.Name = "Fragments"
  53. local partClone = Instance.new("Part")
  54. -- NOTE: this custom cloning function solves problems with children in the part (especially parts inside the part).
  55. -- surface inputs and parameters are ignored since fragmentation messes up the structure anyway.
  56. -- formfactor is is ignored too because it should always be Custom for fragmentation.
  57. -- shape is ignored too because block is default and it has already been established that the part is a block.
  58. -- cframe is ignored too because it is set later on.
  59. partClone.Anchored = part.Anchored
  60. partClone.Archivable = part.Archivable
  61. partClone.BackSurface = part.BackSurface
  62. partClone.BottomSurface = part.BottomSurface
  63. partClone.BrickColor = part.BrickColor
  64. partClone.CanCollide = part.CanCollide
  65. partClone.Elasticity = part.Elasticity
  66. partClone.FormFactor = Enum.FormFactor.Custom
  67. partClone.Friction = part.Friction
  68. partClone.FrontSurface = part.FrontSurface
  69. partClone.LeftSurface = part.LeftSurface
  70. partClone.Locked = part.Locked
  71. partClone.Material = part.Material
  72. partClone.Reflectance = part.Reflectance
  73. partClone.RightSurface = part.RightSurface
  74. partClone.RotVelocity = part.RotVelocity
  75. partClone.Size = part.Size
  76. partClone.TopSurface = part.TopSurface
  77. partClone.Transparency = part.Transparency
  78. partClone.Velocity = part.Velocity
  79. for _, data in ipairs(Fragmentation.AXIS_DATA) do
  80. local axisName, axisNormal = data[1], data[2]
  81. local axisSize = size[axisName]
  82. if axisSize >= fragmentationSize then
  83. size = (Vector3.new(1, 1, 1) - 0.5 * data[2]) * size
  84. partClone.size = size
  85. for partIndex = 1, #parts do
  86. local part = parts[partIndex]
  87. local cframe = part.CFrame
  88. part.Size = size
  89. local clone = partClone:Clone()
  90. part.CFrame = cframe * CFrame.new((-0.25 * axisSize) * axisNormal)
  91. clone.CFrame = cframe * CFrame.new((0.25 * axisSize) * axisNormal)
  92. clone.Parent = model
  93. parts[#parts + 1] = clone
  94. end
  95. end
  96. end
  97. for _, part in ipairs(parts) do
  98. part:MakeJoints()
  99. end
  100. else
  101. return false
  102. end
  103. return true
  104. end
  105. ]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement