Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. hook.Add("CFrame Create", "Mass", function(Contraption) -- Initialize the Mass table when a contraption is created
  2. Contraption.Mass = {
  3. Total = 0,
  4. Physical = 0,
  5. Parented = 0
  6. }
  7. end)
  8.  
  9. hook.Add("CFrame Connect", "Mass", function(Contraption, Entity, Parent) -- Add mass to contraption
  10. local Phys = Entity:GetPhysicsObject()
  11.  
  12. if not IsValid(Phys) then return end
  13.  
  14. local Mass = Contraption.Mass
  15. local Delta = Phys:GetMass()
  16.  
  17. Mass.Total = Mass.Total + Delta
  18.  
  19. if Parent then Mass.Parented = Mass.Parented + Delta
  20. else Mass.Physical = Mass.Physical + Delta end
  21. end)
  22.  
  23. hook.Add("CFrame Disconnect", "Mass", function(Contraption, Entity, Parent) -- Subtract mass from contraption
  24. local Phys = Entity:GetPhysicsObject()
  25.  
  26. if not IsValid(Phys) then return end
  27.  
  28. local Mass = Contraption.Mass
  29. local Delta = Phys:GetMass()
  30.  
  31. Mass.Total = Mass.Total - Delta
  32.  
  33. if Parent then Mass.Parented = Mass.Parented - Delta
  34. else Mass.Physical = Mass.Physical - Delta end
  35. end)
  36.  
  37. ------------------------------------
  38. -- Tracking mass/physicality changes
  39. ------------------------------------
  40. hook.Add("Initialize", "CFrame Mass Module", function()
  41. local Meta = FindMetaTable("PhysObj")
  42. Meta.LegacyMass = Meta.SetMass
  43.  
  44. function Meta:SetMass(NewMass)
  45. if IsValid(self) then
  46. local CF = cframe.Get(self:GetEntity())
  47.  
  48. if CF then
  49. local Mass = CF.Mass
  50. local OldMass = self:GetMass()
  51. local Delta = NewMass - OldMass
  52.  
  53. local NewTotal = Mass.Total + Delta
  54.  
  55. if NewTotal > 0 then -- Sanity checking because spawning dupes does some freaky shit
  56.  
  57. Mass.Total = NewTotal
  58.  
  59. if CF.IsPhysical then Mass.Physical = Mass.Physical + Delta
  60. else Mass.Parented = Mass.Parented + Delta end
  61.  
  62. hook.Run("CFrame MassChange", self, OldMass, NewMass)
  63. end
  64. end
  65. end
  66.  
  67. self:LegacyMass(NewMass)
  68. end
  69.  
  70. hook.Remove("Initialize", "CFrame Mass Module")
  71. end)
  72.  
  73. hook.Add("CFrame PhysChange", "CFrame Mass Module", function(Entity, IsPhysical)
  74. local Phys = Entity:GetPhysicsObject()
  75.  
  76. if not IsValid(Phys) then return end
  77.  
  78. local Mass = Entity.CFWRK.Contraption.Mass
  79. local Delta = Phys:GetMass()
  80.  
  81. if IsPhysical then
  82. Mass.Physical = Mass.Physical + Delta
  83. Mass.Parented = Mass.Parented - Delta
  84. else
  85. Mass.Physical = Mass.Physical - Delta
  86. Mass.Parented = Mass.Parented + Delta
  87. end
  88. end)
  89.  
  90. ------------------------------------
  91. ------------------- Helper functions
  92. ------------------------------------
  93.  
  94. function cframe.GetMass(Var)
  95. if not Var then return 0 end
  96.  
  97. if Var.IsContraption then -- Is a contraption table
  98. return Var.Mass.Total
  99. elseif Var.CFWRK then -- Is an entity
  100. return Var.CFWRK.Contraption.Mass.Total
  101. else -- Isn't a contraption or entity attached to one
  102. return 0
  103. end
  104. end
  105.  
  106. function cframe.GetPhysMass(Var)
  107. if not Var then return 0 end
  108.  
  109. if Var.IsContraption then
  110. return Var.Mass.Physical
  111. elseif Var.CFWRK then
  112. return Var.CFWRK.Contraption.Mass.Physical
  113. else
  114. return 0
  115. end
  116. end
  117.  
  118. function cframe.GetParentedMass(Var)
  119. if not Var then return 0 end
  120.  
  121. if Var.IsContraption then
  122. return Var.Mass.Parented
  123. elseif Var.CFWRK then
  124. return Var.CFWRK.Contraption.Mass.Parented
  125. else
  126. return 0
  127. end
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement