Esempio n. 1
0
        internal void RegisterTransformSyncType <T>(ITransformSync <T> impl)
            where T : class
        {
            var componentQueryDesc = TransformUtils.ConstructEntityQueryDesc <T>(requireAuthority: true, baseComponentTypes);

            componentQueryDesc.None = baseExcludeComponentTypes;

            var entityQuery = GetEntityQuery(componentQueryDesc);

            resetAuthorityActions.Add(typeof(T), () => Entities.With(entityQuery).ForEach(
                                          (Entity entity,
                                           DynamicBuffer <BufferedTransform> buffer,
                                           ref TicksSinceLastTransformUpdate ticksSinceLastTransformUpdate,
                                           ref TransformInternal.Component transformInternal,
                                           ref SpatialEntityId spatialEntityId) =>
            {
                if (updateSystem
                    .GetAuthorityChangesReceived(spatialEntityId.EntityId, TransformInternal.ComponentId)
                    .Count == 0)
                {
                    return;
                }

                var component = EntityManager.GetComponentObject <T>(entity);

                impl.OnResetAuth(worker, entity, ref transformInternal, component);

                buffer.Clear();
                ticksSinceLastTransformUpdate = new TicksSinceLastTransformUpdate();
            }));

            UpdateTransformQuery();
        }
        private void UpdateRigidbodyData()
        {
            Entities.With(rigidbodyGroup).ForEach(
                (Entity entity, DynamicBuffer <BufferedTransform> buffer,
                 ref TicksSinceLastTransformUpdate ticksSinceLastTransformUpdate,
                 ref TransformInternal.Component transformInternal,
                 ref SpatialEntityId spatialEntityId) =>
            {
                if (updateSystem
                    .GetAuthorityChangesReceived(spatialEntityId.EntityId, TransformInternal.ComponentId)
                    .Count == 0)
                {
                    return;
                }

                var rigidbody = EntityManager.GetComponentObject <Rigidbody>(entity);
                rigidbody.MovePosition(TransformUtils.ToUnityVector3(transformInternal.Location) + worker.Origin);
                rigidbody.MoveRotation(TransformUtils.ToUnityQuaternion(transformInternal.Rotation));
                rigidbody.AddForce(TransformUtils.ToUnityVector3(transformInternal.Velocity) - rigidbody.velocity,
                                   ForceMode.VelocityChange);

                buffer.Clear();
                ticksSinceLastTransformUpdate = new TicksSinceLastTransformUpdate();
            });
        }
        private void UpdateRigidbodyData()
        {
            transformGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative);

            var ticksSinceLastUpdateArray = transformGroup.GetComponentDataArray <TicksSinceLastTransformUpdate>();
            var transformComponentArray   = transformGroup.GetComponentDataArray <TransformInternal.Component>();
            var bufferedTransformArray    = transformGroup.GetBufferArray <BufferedTransform>();
            var unityTransformArray       = transformGroup.GetComponentArray <UnityEngine.Transform>();
            var spatialEntityIdArray      = transformGroup.GetComponentDataArray <SpatialEntityId>();

            for (int i = 0; i < transformComponentArray.Length; ++i)
            {
                if (updateSystem
                    .GetAuthorityChangesReceived(spatialEntityIdArray[i].EntityId, TransformInternal.ComponentId)
                    .Count == 0)
                {
                    continue;
                }

                var t = transformComponentArray[i];
                var unityTransform = unityTransformArray[i];
                unityTransform.position = t.Location.ToUnityVector3() + worker.Origin;
                unityTransform.rotation = t.Rotation.ToUnityQuaternion();
                bufferedTransformArray[i].Clear();
                ticksSinceLastUpdateArray[i] = new TicksSinceLastTransformUpdate();
            }
        }
        private void UpdateTransformData()
        {
            rigidbodyGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative);

            var ticksSinceLastUpdateArray = rigidbodyGroup.GetComponentDataArray <TicksSinceLastTransformUpdate>();
            var transformComponentArray   = rigidbodyGroup.GetComponentDataArray <TransformInternal.Component>();
            var bufferedTransformArray    = rigidbodyGroup.GetBufferArray <BufferedTransform>();
            var rigidbodyArray            = rigidbodyGroup.GetComponentArray <Rigidbody>();
            var spatialEntityIdArray      = rigidbodyGroup.GetComponentDataArray <SpatialEntityId>();

            for (int i = 0; i < transformComponentArray.Length; ++i)
            {
                // todo this is not a correct constraint. Needs a the auth loss temporary exposed to correctly do this
                // alternatively this needs an authority changed component that is filled at the beginning of the tick
                if (updateSystem
                    .GetAuthorityChangesReceived(spatialEntityIdArray[i].EntityId, TransformInternal.ComponentId)
                    .Count == 0)
                {
                    continue;
                }

                var t         = transformComponentArray[i];
                var rigidbody = rigidbodyArray[i];
                rigidbody.MovePosition(t.Location.ToUnityVector3() + worker.Origin);
                rigidbody.MoveRotation(t.Rotation.ToUnityQuaternion());
                rigidbody.AddForce(t.Velocity.ToUnityVector3() - rigidbody.velocity, ForceMode.VelocityChange);
                bufferedTransformArray[i].Clear();
                ticksSinceLastUpdateArray[i] = new TicksSinceLastTransformUpdate();
            }
        }
Esempio n. 5
0
 protected override void OnUpdate()
 {
     for (int i = 0; i < data.Length; ++i)
     {
         TicksSinceLastTransformUpdate t = data.TicksSinceLastUpdate[i];
         t.NumberOfTicks += 1;
         data.TicksSinceLastUpdate[i] = t;
     }
 }
        protected override void OnUpdate()
        {
            transformGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative);

            Entities.With(transformGroup).ForEach(
                (RateLimitedSendConfig config, ref TransformInternal.Component transform,
                 ref TransformToSend transformToSend, ref LastTransformSentData lastTransformSent,
                 ref TicksSinceLastTransformUpdate ticksSinceLastTransformUpdate) =>
            {
                lastTransformSent.TimeSinceLastUpdate += Time.deltaTime;

                if (lastTransformSent.TimeSinceLastUpdate < 1.0f / config.MaxTransformUpdateRateHz)
                {
                    return;
                }

                var transformHasChanged = false;

                var newVelocity = TransformUtils.ToFixedPointVector3(transformToSend.Velocity);
                if (TransformUtils.HasChanged(newVelocity, transform.Velocity))
                {
                    transform.Velocity  = newVelocity;
                    transformHasChanged = true;
                }

                var newLocation = TransformUtils.ToFixedPointVector3(transformToSend.Position - worker.Origin);
                if (TransformUtils.HasChanged(newLocation, transform.Location))
                {
                    transform.Location  = newLocation;
                    transformHasChanged = true;
                }

                var newRotation = TransformUtils.ToCompressedQuaternion(transformToSend.Orientation);
                if (TransformUtils.HasChanged(newRotation, transform.Rotation))
                {
                    transform.Rotation  = newRotation;
                    transformHasChanged = true;
                }

                if (!transformHasChanged)
                {
                    return;
                }

                transform.PhysicsTick   += ticksSinceLastTransformUpdate.NumberOfTicks;
                transform.TicksPerSecond = tickRate.PhysicsTicksPerRealSecond;

                lastTransformSent.TimeSinceLastUpdate = 0.0f;
                lastTransformSent.Transform           = transform;

                ticksSinceLastTransformUpdate = new TicksSinceLastTransformUpdate
                {
                    NumberOfTicks = 0
                };
            });
        }
Esempio n. 7
0
        protected override void OnUpdate()
        {
            transformGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative);

            var rateLimitedConfigArray    = transformGroup.GetSharedComponentDataArray <RateLimitedSendConfig>();
            var transformArray            = transformGroup.GetComponentDataArray <TransformInternal.Component>();
            var transformToSendArray      = transformGroup.GetComponentDataArray <TransformToSend>();
            var lastTransformSentArray    = transformGroup.GetComponentDataArray <LastTransformSentData>();
            var ticksSinceLastUpdateArray = transformGroup.GetComponentDataArray <TicksSinceLastTransformUpdate>();

            for (int i = 0; i < transformArray.Length; ++i)
            {
                var lastTransformSent = lastTransformSentArray[i];
                lastTransformSent.TimeSinceLastUpdate += Time.deltaTime;
                lastTransformSentArray[i]              = lastTransformSent;

                if (lastTransformSent.TimeSinceLastUpdate <
                    1.0f / rateLimitedConfigArray[i].MaxTransformUpdateRateHz)
                {
                    continue;
                }

                var transform = transformArray[i];

                var transformToSend = transformToSendArray[i];

                var currentTransform = new TransformInternal.Component
                {
                    Location       = (transformToSend.Position - worker.Origin).ToImprobableLocation(),
                    Rotation       = transformToSend.Orientation.ToImprobableQuaternion(),
                    Velocity       = transformToSend.Velocity.ToImprobableVelocity(),
                    PhysicsTick    = transform.PhysicsTick + ticksSinceLastUpdateArray[i].NumberOfTicks,
                    TicksPerSecond = tickRate.PhysicsTicksPerRealSecond
                };

                if (!(TransformUtils.HasChanged(currentTransform.Location, transform.Location) ||
                      TransformUtils.HasChanged(currentTransform.Rotation, transform.Rotation)))
                {
                    continue;
                }

                lastTransformSent.TimeSinceLastUpdate = 0.0f;
                lastTransformSent.Transform           = transform;
                lastTransformSentArray[i]             = lastTransformSent;

                transformArray[i] = currentTransform;

                ticksSinceLastUpdateArray[i] = new TicksSinceLastTransformUpdate
                {
                    NumberOfTicks = 0
                };
            }
        }
 protected override void OnUpdate()
 {
     for (int i = 0; i < data.Length; ++i)
     {
         // Need to split this out into server / client / whatever
         var transform    = data.Transform[i];
         var defaultToSet = new TransformToSet
         {
             Position    = transform.Location.ToUnityVector3() + worker.Origin,
             Velocity    = transform.Velocity.ToUnityVector3(),
             Orientation = transform.Rotation.ToUnityQuaternion()
         };
         var defaultToSend = new TransformToSend
         {
             Position    = transform.Location.ToUnityVector3() - worker.Origin,
             Velocity    = transform.Velocity.ToUnityVector3(),
             Orientation = transform.Rotation.ToUnityQuaternion()
         };
         var previousTransform = new DefferedUpdateTransform
         {
             Transform = transform
         };
         var ticksSinceLastUpdate = new TicksSinceLastTransformUpdate
         {
             NumberOfTicks = 0
         };
         var lastTransform = new LastTransformSentData
         {
             // could set this to the max time if we want to immediately send something
             TimeSinceLastUpdate = 0.0f,
             Transform           = transform
         };
         var lastPosition = new LastPositionSentData
         {
             // could set this to the max time if we want to immediately send something
             TimeSinceLastUpdate = 0.0f,
             Position            = data.Position[i]
         };
         PostUpdateCommands.AddComponent(data.Entity[i], defaultToSet);
         PostUpdateCommands.AddComponent(data.Entity[i], defaultToSend);
         PostUpdateCommands.AddComponent(data.Entity[i], previousTransform);
         PostUpdateCommands.AddComponent(data.Entity[i], ticksSinceLastUpdate);
         PostUpdateCommands.AddComponent(data.Entity[i], lastPosition);
         PostUpdateCommands.AddComponent(data.Entity[i], lastTransform);
         PostUpdateCommands.AddBuffer <BufferedTransform>(data.Entity[i]);
     }
 }
Esempio n. 9
0
        private void AddCommonSendComponents(EntityCommandBuffer commandBuffer)
        {
            commandBuffer.AddComponent(entity, new GetTransformFromGameObjectTag());

            var transformComponent = transformReader.Data;

            var defaultToSend = new TransformToSend
            {
                Position    = transformComponent.Location.ToUnityVector() - world.GetExistingSystem <WorkerSystem>().Origin,
                Velocity    = transformComponent.Velocity.ToUnityVector(),
                Orientation = transformComponent.Rotation.ToUnityQuaternion()
            };

            var ticksSinceLastUpdate = new TicksSinceLastTransformUpdate
            {
                NumberOfTicks = 0
            };

            var lastTransform = new LastTransformSentData
            {
                // could set this to the max time if we want to immediately send something
                TimeSinceLastUpdate = 0.0f,
                Transform           = transformComponent
            };

            var position     = entityManager.GetComponentData <Position.Component>(entity);
            var lastPosition = new LastPositionSentData
            {
                // could set this to the max time if we want to immediately send something
                TimeSinceLastUpdate = 0.0f,
                Position            = position
            };

            commandBuffer.AddComponent(entity, defaultToSend);
            commandBuffer.AddComponent(entity, ticksSinceLastUpdate);
            commandBuffer.AddComponent(entity, lastPosition);
            commandBuffer.AddComponent(entity, lastTransform);
        }
Esempio n. 10
0
        private void ResetTransforms()
        {
            Entities.With(transformQuery).ForEach((Entity entity,
                                                   DynamicBuffer <BufferedTransform> buffer,
                                                   ref TicksSinceLastTransformUpdate ticksSinceLastTransformUpdate,
                                                   ref TransformInternal.Component transformInternal,
                                                   ref SpatialEntityId spatialEntityId) =>
            {
                if (updateSystem
                    .GetAuthorityChangesReceived(spatialEntityId.EntityId, TransformInternal.ComponentId)
                    .Count == 0)
                {
                    return;
                }

                var unityTransform      = EntityManager.GetComponentObject <UnityEngine.Transform>(entity);
                unityTransform.position = transformInternal.Location.ToUnityVector() + worker.Origin;
                unityTransform.rotation = transformInternal.Rotation.ToUnityQuaternion();

                buffer.Clear();
                ticksSinceLastTransformUpdate = new TicksSinceLastTransformUpdate();
            });
        }