Advertisement
AlewAlow

bruh im suicidal

Jul 11th, 2023 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. local function LerpAngle(a, b, t)
  2.     local currentDirection = Vector2.new(math.cos(a), math.sin(a))
  3.     local targetDirection = Vector2.new(math.cos(b), math.sin(b))
  4.     local direction = Lerp(currentDirection, targetDirection, t)
  5.     return math.atan2(direction.Y, direction.X)
  6. end
  7.  
  8. --------------- SERVER
  9. -----------------
  10. -----------------
  11.  
  12. local Players = game:GetService("Players")
  13. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  14.  
  15. local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
  16. local Components = require(ReplicatedStorage.Shared.Components)
  17.  
  18. local SEND_TRANSFORMS_RATE = 0.015
  19.  
  20. local function ReplicatePlayerTransforms(world, state)
  21.     state.PendingPlayerTransforms = state.PendingPlayerTransforms or {}
  22.    
  23.     for _, instance, position, rotation in Matter.useEvent(ReplicatedStorage.Shared.SendPlayerPosition, "OnServerEvent") do
  24.         state.PendingPlayerTransforms[instance] = {
  25.             Position = position,
  26.             Rotation = rotation,
  27.         }
  28.     end
  29.    
  30.     if Matter.useThrottle(SEND_TRANSFORMS_RATE) then
  31.         for instance, transform in state.PendingPlayerTransforms do
  32.             for _, otherInstance in Players:GetPlayers() do
  33.                 if otherInstance ~= instance then
  34.                     ReplicatedStorage.Shared.SendPlayerPosition:FireClient(otherInstance, instance, transform.Position, transform.Rotation)
  35.                 end
  36.             end
  37.         end
  38.        
  39.         state.PendingPlayerTransforms = {}
  40.     end
  41. end
  42.  
  43. return ReplicatePlayerTransforms
  44.  
  45. ----------------- CLIENT
  46. ---------------------
  47. ----------------------
  48.  
  49. local function SendLocalTransform(world, state)
  50.     for id, transform, player in world:query(Components.Transform, Components.Player) do
  51.         if player.Instance == localPlayerInstance and Matter.useThrottle(SEND_TRANSFORM_RATE, id) then
  52.             ReplicatedStorage.Shared.SendPlayerPosition:FireServer(transform.Position, transform.Rotation)
  53.         end
  54.     end
  55.    
  56.     for _, instance, position, rotation in Matter.useEvent(ReplicatedStorage.Shared.SendPlayerPosition, "OnClientEvent") do
  57.         local id = instance:GetAttribute("EntityId")
  58.         if not id or not world:contains(id) then
  59.             continue
  60.         end
  61.        
  62.         local player = world:get(id, Components.Player)
  63.         if not player then
  64.             continue
  65.         end
  66.        
  67.         world:insert(id,
  68.             player:patch({
  69.                 RealPosition = position,
  70.                 RealRotation = rotation,
  71.             })
  72.         )
  73.     end
  74.    
  75.     for id, transform, player in world:query(Components.Transform, Components.Player) do
  76.         if player.Instance == localPlayerInstance then
  77.             continue
  78.         end
  79.        
  80.         world:insert(id, transform:patch({
  81.             -- some magic ass values
  82.             Position = Lerp(transform.Position, player.RealPosition, 3 * Matter.useDeltaTime()),
  83.             Rotation = LerpAngle(transform.Rotation, player.RealRotation, 15 * Matter.useDeltaTime()),
  84.         }))
  85.     end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement