コード例 #1
0
        public EntitySubscriptionManager(World world) : base(world)
        {
            var receiveSystem = world.GetExistingSystem <SpatialOSReceiveSystem>();

            if (receiveSystem == null)
            {
                throw new ArgumentException("No worker");
            }

            var constraintsSystem = world.GetExistingSystem <ComponentConstraintsCallbackSystem>();

            if (constraintsSystem == null)
            {
                // todo real error messages
                throw new ArgumentException("Subscriptions systems missing");
            }

            constraintsSystem.RegisterEntityAddedCallback(entityId =>
            {
                if (!entityIdToSubscriptions.TryGetValue(entityId, out var subscriptions))
                {
                    return;
                }

                var entity = WorkerSystem.GetEntity(entityId);
                foreach (var subscription in subscriptions)
                {
                    if (!subscription.HasValue)
                    {
                        subscription.SetAvailable(entity);
                    }
                }
            });

            constraintsSystem.RegisterEntityRemovedCallback(entityId =>
            {
                if (!entityIdToSubscriptions.TryGetValue(entityId, out var subscriptions))
                {
                    return;
                }

                foreach (var subscription in subscriptions)
                {
                    if (subscription.HasValue)
                    {
                        subscription.SetUnavailable();
                    }
                }
            });
        }
コード例 #2
0
        protected CommandReceiverSubscriptionManagerBase(World world, uint componentId) : base(world)
        {
            var componentMetaClass = ComponentDatabase.GetMetaclass(componentId);

            componentAuthType = componentMetaClass.Authority;

            entityManager = world.EntityManager;

            var constraintSystem = world.GetExistingSystem <ComponentConstraintsCallbackSystem>();

            constraintSystem.RegisterAuthorityCallback(componentId, authorityChange =>
            {
                if (authorityChange.Authority == Authority.Authoritative)
                {
                    if (!entitiesNotMatchingRequirements.Contains(authorityChange.EntityId))
                    {
                        return;
                    }

                    var entity = WorkerSystem.GetEntity(authorityChange.EntityId);

                    foreach (var subscription in entityIdToReceiveSubscriptions[authorityChange.EntityId])
                    {
                        subscription.SetAvailable(CreateReceiver(world, entity, authorityChange.EntityId));
                    }

                    entitiesMatchingRequirements.Add(authorityChange.EntityId);
                    entitiesNotMatchingRequirements.Remove(authorityChange.EntityId);
                }
                else if (authorityChange.Authority == Authority.NotAuthoritative)
                {
                    if (!entitiesMatchingRequirements.Contains(authorityChange.EntityId))
                    {
                        return;
                    }

                    foreach (var subscription in entityIdToReceiveSubscriptions[authorityChange.EntityId])
                    {
                        ResetValue(subscription);
                        subscription.SetUnavailable();
                    }

                    entitiesNotMatchingRequirements.Add(authorityChange.EntityId);
                    entitiesMatchingRequirements.Remove(authorityChange.EntityId);
                }
            });
        }
コード例 #3
0
        private void RegisterComponentCallbacks()
        {
            var constraintCallbackSystem = World.GetExistingSystem <ComponentConstraintsCallbackSystem>();

            constraintCallbackSystem.RegisterAuthorityCallback(ComponentId, authorityChange =>
            {
                if (authorityChange.Authority == Authority.Authoritative)
                {
                    if (!entitiesNotMatchingRequirements.Contains(authorityChange.EntityId))
                    {
                        return;
                    }

                    var entity = WorkerSystem.GetEntity(authorityChange.EntityId);

                    foreach (var subscription in entityIdToWriterSubscriptions[authorityChange.EntityId])
                    {
                        subscription.SetAvailable(CreateWriter(entity, authorityChange.EntityId));
                    }

                    entitiesMatchingRequirements.Add(authorityChange.EntityId);
                    entitiesNotMatchingRequirements.Remove(authorityChange.EntityId);
                }
                else if (authorityChange.Authority == Authority.NotAuthoritative)
                {
                    if (!entitiesMatchingRequirements.Contains(authorityChange.EntityId))
                    {
                        return;
                    }

                    foreach (var subscription in entityIdToWriterSubscriptions[authorityChange.EntityId])
                    {
                        ResetValue(subscription);
                        subscription.SetUnavailable();
                    }

                    entitiesNotMatchingRequirements.Add(authorityChange.EntityId);
                    entitiesMatchingRequirements.Remove(authorityChange.EntityId);
                }
            });
        }
コード例 #4
0
        protected CommandSenderSubscriptionManagerBase(World world) : base(world)
        {
            var constraintSystem = world.GetExistingSystem <ComponentConstraintsCallbackSystem>();

            constraintSystem.RegisterEntityAddedCallback(entityId =>
            {
                if (!entityIdToSenderSubscriptions.TryGetValue(entityId, out var subscriptions))
                {
                    return;
                }

                var entity = WorkerSystem.GetEntity(entityId);

                foreach (var subscription in subscriptions)
                {
                    if (!subscription.HasValue)
                    {
                        subscription.SetAvailable(CreateSender(entity, world));
                    }
                }
            });

            constraintSystem.RegisterEntityRemovedCallback(entityId =>
            {
                if (!entityIdToSenderSubscriptions.TryGetValue(entityId, out var subscriptions))
                {
                    return;
                }

                foreach (var subscription in subscriptions)
                {
                    if (subscription.HasValue)
                    {
                        ResetValue(subscription);
                        subscription.SetUnavailable();
                    }
                }
            });
        }
コード例 #5
0
        private void RegisterComponentCallbacks()
        {
            var constraintCallbackSystem = World.GetExistingSystem <ComponentConstraintsCallbackSystem>();

            constraintCallbackSystem.RegisterComponentAddedCallback(ComponentId, entityId =>
            {
                if (!entitiesNotMatchingRequirements.Contains(entityId))
                {
                    return;
                }

                var entity = WorkerSystem.GetEntity(entityId);

                foreach (var subscription in entityIdToReaderSubscriptions[entityId])
                {
                    subscription.SetAvailable(CreateReader(entity, entityId));
                }

                entitiesMatchingRequirements.Add(entityId);
                entitiesNotMatchingRequirements.Remove(entityId);
            });

            constraintCallbackSystem.RegisterComponentRemovedCallback(ComponentId, entityId =>
            {
                if (!entitiesMatchingRequirements.Contains(entityId))
                {
                    return;
                }

                foreach (var subscription in entityIdToReaderSubscriptions[entityId])
                {
                    ResetValue(subscription);
                    subscription.SetUnavailable();
                }

                entitiesNotMatchingRequirements.Add(entityId);
                entitiesMatchingRequirements.Remove(entityId);
            });
        }