private void SetFromPacket(VelocityPacket packet, ConnectionEntityManager conEntityMgr)
        {
            var entity = packet.Entity;

            if (!conEntityMgr.HasEntity(entity))
            {
                Debug.LogError($"No entity {entity.Index}, {entity.Version} found");
                return;
            }

            entity = conEntityMgr.GetEntity(entity);

            entity.SetOrAddComponentData(new StVelocity(packet.Velocity));
        }
Exemple #2
0
        void ReadEntityPosition(int index, float timestamp, ConnectionEntityManager conEntityMgr, MessageReader reader, NetPeerInstance peerInstance)
        {
            var entity = conEntityMgr.GetEntity(reader.GetEntity());

            var position = reader.Data.GetVec3();
            var velocity = reader.Data.GetVec3();

            if (!entity.HasComponent <NetSnapshotPosition>())
            {
                Vector3 oldPosition = entity.GetComponentData <NetPosition>().Target;
                var     movDir      = (position - oldPosition).normalized * 0.001f;

                var predictedPosition = position + (movDir * peerInstance.Get <ConnectionNetManagerConfig>().ConfigUpdateTime);
                var oldProgress       = entity.GetComponentData <NetPosition>().DeltaProgress;
                entity.SetOrAddComponentData(new NetPosition
                {
                    Target        = position,
                    Predicted     = predictedPosition,
                    DeltaProgress = Mathf.Lerp(oldProgress, 0, 0.5f)
                });

                if (entity.HasComponent <NetPositionInterpolatorBuffer>() && entity.HasComponent <NetPositionInterpolator>())
                {
                    var bufferArray = EntityManager.GetBuffer <NetPositionInterpolatorBuffer>(entity);

                    // Too much elements, remove some
                    while (bufferArray.Length > 10)
                    {
                        bufferArray.RemoveAt(0);
                    }

                    bufferArray.Add(new NetPositionInterpolatorBuffer(timestamp, Time.time, position));

                    var interpolator = EntityManager.GetComponentData <NetPositionInterpolator>(entity);
                    interpolator.LatestPosition  = position;
                    interpolator.LatestTimestamp = timestamp;
                    EntityManager.SetComponentData(entity, interpolator);
                }

                if (entity.HasComponent <NetPositionDeadReckoning>() && entity.HasComponent <NetPositionDeadReckoningBuffer>())
                {
                    var bufferArray = EntityManager.GetBuffer <NetPositionDeadReckoningBuffer>(entity);

                    while (bufferArray.Length > 2)
                    {
                        bufferArray.RemoveAt(0);
                    }

                    bufferArray.Add(new NetPositionDeadReckoningBuffer(timestamp, position, velocity));
                }
            }
            else
            {
                var netDelta   = EntityManager.GetSharedComponentData <NetSnapshotPosition>(entity);
                var history    = netDelta.DeltaPositions;
                var dtDuration = entity.GetComponentData <NetPosition>().DeltaProgress;
                while (history.Count > 0 && dtDuration > 0)
                {
                    if (dtDuration >= history[0].Delta)
                    {
                        dtDuration -= history[0].Delta;
                        history.RemoveAt(0);
                    }
                    else
                    {
                        var t     = 1 - dtDuration / history[0].Delta;
                        var frame = history[0];
                        frame.Delta -= dtDuration;
                        frame.Value *= t;
                        history[0]   = frame;
                    }
                }

                var predictedPosition = (float3)position;
                foreach (var frame in history)
                {
                    predictedPosition += frame.Value;
                }

                entity.SetOrAddComponentData(new NetPosition {
                    Target = position, Predicted = predictedPosition, DeltaProgress = dtDuration
                });
            }
        }