Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function LerpAngle(a, b, t)
- local currentDirection = Vector2.new(math.cos(a), math.sin(a))
- local targetDirection = Vector2.new(math.cos(b), math.sin(b))
- local direction = Lerp(currentDirection, targetDirection, t)
- return math.atan2(direction.Y, direction.X)
- end
- --------------- SERVER
- -----------------
- -----------------
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Matter = require(ReplicatedStorage.Shared.Libs.Matter)
- local Components = require(ReplicatedStorage.Shared.Components)
- local SEND_TRANSFORMS_RATE = 0.015
- local function ReplicatePlayerTransforms(world, state)
- state.PendingPlayerTransforms = state.PendingPlayerTransforms or {}
- for _, instance, position, rotation in Matter.useEvent(ReplicatedStorage.Shared.SendPlayerPosition, "OnServerEvent") do
- state.PendingPlayerTransforms[instance] = {
- Position = position,
- Rotation = rotation,
- }
- end
- if Matter.useThrottle(SEND_TRANSFORMS_RATE) then
- for instance, transform in state.PendingPlayerTransforms do
- for _, otherInstance in Players:GetPlayers() do
- if otherInstance ~= instance then
- ReplicatedStorage.Shared.SendPlayerPosition:FireClient(otherInstance, instance, transform.Position, transform.Rotation)
- end
- end
- end
- state.PendingPlayerTransforms = {}
- end
- end
- return ReplicatePlayerTransforms
- ----------------- CLIENT
- ---------------------
- ----------------------
- local function SendLocalTransform(world, state)
- for id, transform, player in world:query(Components.Transform, Components.Player) do
- if player.Instance == localPlayerInstance and Matter.useThrottle(SEND_TRANSFORM_RATE, id) then
- ReplicatedStorage.Shared.SendPlayerPosition:FireServer(transform.Position, transform.Rotation)
- end
- end
- for _, instance, position, rotation in Matter.useEvent(ReplicatedStorage.Shared.SendPlayerPosition, "OnClientEvent") do
- local id = instance:GetAttribute("EntityId")
- if not id or not world:contains(id) then
- continue
- end
- local player = world:get(id, Components.Player)
- if not player then
- continue
- end
- world:insert(id,
- player:patch({
- RealPosition = position,
- RealRotation = rotation,
- })
- )
- end
- for id, transform, player in world:query(Components.Transform, Components.Player) do
- if player.Instance == localPlayerInstance then
- continue
- end
- world:insert(id, transform:patch({
- -- some magic ass values
- Position = Lerp(transform.Position, player.RealPosition, 3 * Matter.useDeltaTime()),
- Rotation = LerpAngle(transform.Rotation, player.RealRotation, 15 * Matter.useDeltaTime()),
- }))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement