public override Subscription <NonBlittableComponentCommandSender> Subscribe(EntityId entityId)
        {
            if (entityIdToSenderSubscriptions == null)
            {
                entityIdToSenderSubscriptions = new Dictionary <EntityId, HashSet <Subscription <NonBlittableComponentCommandSender> > >();
            }

            if (entityId.Id < 0)
            {
                throw new ArgumentException("EntityId can not be < 0");
            }

            var subscription = new Subscription <NonBlittableComponentCommandSender>(this, entityId);

            if (!entityIdToSenderSubscriptions.TryGetValue(entityId, out var subscriptions))
            {
                subscriptions = new HashSet <Subscription <NonBlittableComponentCommandSender> >();
                entityIdToSenderSubscriptions.Add(entityId, subscriptions);
            }

            if (workerSystem.TryGetEntity(entityId, out var entity))
            {
                subscription.SetAvailable(new NonBlittableComponentCommandSender(entity, world));
            }
            else if (entityId.Id == 0)
            {
                subscription.SetAvailable(new NonBlittableComponentCommandSender(Entity.Null, world));
            }

            subscriptions.Add(subscription);
            return(subscription);
        }
        public override Subscription <NonBlittableComponentCommandReceiver> Subscribe(EntityId entityId)
        {
            if (entityIdToReceiveSubscriptions == null)
            {
                entityIdToReceiveSubscriptions = new Dictionary <EntityId, HashSet <Subscription <NonBlittableComponentCommandReceiver> > >();
            }

            var subscription = new Subscription <NonBlittableComponentCommandReceiver>(this, entityId);

            if (!entityIdToReceiveSubscriptions.TryGetValue(entityId, out var subscriptions))
            {
                subscriptions = new HashSet <Subscription <NonBlittableComponentCommandReceiver> >();
                entityIdToReceiveSubscriptions.Add(entityId, subscriptions);
            }

            if (workerSystem.TryGetEntity(entityId, out var entity) &&
                componentUpdateSystem.HasComponent(NonBlittableComponent.ComponentId, entityId) &&
                componentUpdateSystem.GetAuthority(entityId, NonBlittableComponent.ComponentId) != Authority.NotAuthoritative)
            {
                entitiesMatchingRequirements.Add(entityId);
                subscription.SetAvailable(new NonBlittableComponentCommandReceiver(world, entity, entityId));
            }
            else
            {
                entitiesNotMatchingRequirements.Add(entityId);
            }

            subscriptions.Add(subscription);
            return(subscription);
        }
コード例 #3
0
        public EntitySubscriptionManager(World world)
        {
            workerSystem = world.GetExistingManager <WorkerSystem>();
            if (workerSystem == null)
            {
                throw new ArgumentException("No worker");
            }

            var receiveSystem = world.GetExistingManager <SpatialOSReceiveSystem>();

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

            var constraintsSystem = world.GetExistingManager <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;
                }

                workerSystem.TryGetEntity(entityId, out var entity);
                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();
                    }
                }
            });
        }
コード例 #4
0
        public bool TryGetGameObjectForSpatialOSEntityId(EntityId entityId, out GameObject linkedGameObject)
        {
            linkedGameObject = default(GameObject);
            if (!Worker.TryGetEntity(entityId, out var entity))
            {
                return(false);
            }

            entityManager = entityManager ?? World.GetOrCreateManager <EntityManager>();
            if (!entityManager.HasComponent <GameObjectReference>(entity))
            {
                return(false);
            }

            linkedGameObject = entityManager.GetComponentObject <GameObjectReference>(entity).GameObject;
            return(true);
        }
コード例 #5
0
        protected override void OnUpdate()
        {
            var requests        = commandSystem.GetRequests <Launcher.IncreaseScore.ReceivedRequest>();
            var scoreComponents = GetComponentDataFromEntity <Score.Component>();

            for (var i = 0; i < requests.Count; i++)
            {
                var request = requests[i];
                if (!workerSystem.TryGetEntity(request.EntityId, out var entity))
                {
                    continue;
                }

                var component = scoreComponents[entity];
                component.Score        += request.Payload.Amount;
                scoreComponents[entity] = component;
            }
        }
コード例 #6
0
        public BlittableComponentCommandSenderSubscriptionManager(World world)
        {
            this.world = world;

            // Check that these are there
            workerSystem = world.GetExistingSystem <WorkerSystem>();

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

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

                workerSystem.TryGetEntity(entityId, out var entity);
                foreach (var subscription in subscriptions)
                {
                    if (!subscription.HasValue)
                    {
                        subscription.SetAvailable(new BlittableComponentCommandSender(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();
                    }
                }
            });
        }
コード例 #7
0
        public override Subscription <Entity> Subscribe(EntityId entityId)
        {
            if (!entityIdToSubscriptions.TryGetValue(entityId, out var subscriptions))
            {
                subscriptions = new HashSet <Subscription <Entity> >();
                entityIdToSubscriptions.Add(entityId, subscriptions);
            }

            var subscription = new Subscription <Entity>(this, entityId);

            subscriptions.Add(subscription);

            if (workerSystem.TryGetEntity(entityId, out var entity))
            {
                subscription.SetAvailable(entity);
            }

            return(subscription);
        }
コード例 #8
0
        protected override void OnUpdate()
        {
            foreach (var entityId in entitySystem.GetEntitiesAdded())
            {
                foreach (var manager in managers)
                {
                    workerSystem.TryGetEntity(entityId, out var entity);
                    manager.AddComponents(entity, EntityManager, World);
                }
            }

            foreach (var entityId in entitySystem.GetEntitiesRemoved())
            {
                foreach (var manager in managers)
                {
                    manager.RemoveComponents(entityId, EntityManager, World);
                }
            }
        }
コード例 #9
0
        protected override void OnUpdate()
        {
            var changeColorEvents = updateSystem.GetEventsReceived <CubeColor.ChangeColor.Event>();

            for (var i = 0; i < changeColorEvents.Count; i++)
            {
                var colorEvent = changeColorEvents[i];
                if (!workerSystem.TryGetEntity(colorEvent.EntityId, out var entity))
                {
                    continue;
                }

                if (EntityManager.HasComponent <MeshRenderer>(entity))
                {
                    var renderer   = EntityManager.GetComponentObject <MeshRenderer>(entity);
                    var eventColor = colorEvent.Event.Payload.Color;
                    renderer.SetPropertyBlock(materialPropertyBlocks[eventColor]);
                }
            }
        }
コード例 #10
0
        protected override void OnUpdate()
        {
            foreach (var entityId in entitySystem.GetEntitiesAdded())
            {
                workerSystem.TryGetEntity(entityId, out var entity);
                gameObjectCreator.OnEntityCreated(new SpatialOSEntity(entity, EntityManager), Linker);
            }

            var removedEntities = entitySystem.GetEntitiesRemoved();

            foreach (var entityId in removedEntities)
            {
                Linker.UnlinkAllGameObjectsFromEntityId(entityId);
            }

            Linker.FlushCommandBuffer();

            foreach (var entityId in removedEntities)
            {
                gameObjectCreator.OnEntityRemoved(entityId);
            }
        }
コード例 #11
0
            public void PopulateReactiveCommandComponents(CommandSystem commandSystem, EntityManager entityManager, WorkerSystem workerSystem, World world)
            {
                var receivedRequests = commandSystem.GetRequests <Cmd.ReceivedRequest>();

                // todo Not efficient if it keeps jumping all over entities but don't care right now
                for (int i = 0; i < receivedRequests.Count; ++i)
                {
                    if (!workerSystem.TryGetEntity(receivedRequests[i].EntityId, out var entity))
                    {
                        continue;
                    }

                    List <Cmd.ReceivedRequest> requests;
                    if (entityManager.HasComponent <global::Improbable.Gdk.Tests.ComponentsWithNoFields.ComponentWithNoFieldsWithCommands.CommandRequests.Cmd>(entity))
                    {
                        requests = entityManager.GetComponentData <global::Improbable.Gdk.Tests.ComponentsWithNoFields.ComponentWithNoFieldsWithCommands.CommandRequests.Cmd>(entity).Requests;
                    }
                    else
                    {
                        var data = new global::Improbable.Gdk.Tests.ComponentsWithNoFields.ComponentWithNoFieldsWithCommands.CommandRequests.Cmd
                        {
                            CommandListHandle = ReferenceTypeProviders.CmdRequestsProvider.Allocate(world)
                        };
                        data.Requests = new List <Cmd.ReceivedRequest>();
                        requests      = data.Requests;
                        entityManager.AddComponentData(entity, data);
                    }

                    requests.Add(receivedRequests[i]);
                }


                var receivedResponses = commandSystem.GetResponses <Cmd.ReceivedResponse>();

                // todo Not efficient if it keeps jumping all over entities but don't care right now
                for (int i = 0; i < receivedResponses.Count; ++i)
                {
                    ref readonly var response = ref receivedResponses[i];