public MonoBehaviourActivationManager(GameObject gameObject, RequiredFieldInjector injector,
                                              InjectableStore store, ILogDispatcher logger)
        {
            this.logger   = logger;
            this.store    = store;
            this.injector = injector;

            var spatialComponent = gameObject.GetComponent <SpatialOSComponent>();
            var workerType       = spatialComponent.Worker.WorkerType;

            entity = spatialComponent.Entity;

            foreach (var behaviour in gameObject.GetComponents <MonoBehaviour>())
            {
                if (ReferenceEquals(behaviour, null))
                {
                    continue;
                }

                var behaviourType = behaviour.GetType();
                if (injector.IsSpatialOSBehaviour(behaviourType))
                {
                    var componentReadRequirements = injector.GetComponentPresenceRequirements(behaviourType);
                    var componentAuthRequirements = injector.GetComponentAuthorityRequirements(behaviourType);
                    var workerTypeRequirements    =
                        injector.GetComponentWorkerTypeRequirementsForBehaviours(behaviourType);

                    if (workerTypeRequirements != null && !workerTypeRequirements.Contains(workerType))
                    {
                        // This behaviour does not want to be enabled for this worker.
                        RunWithExceptionHandling(() => behaviour.enabled = false);
                        continue;
                    }

                    var readRequirementCount = componentReadRequirements.Count;
                    var authRequirementCount = componentAuthRequirements.Count;

                    // This is the case when a MonoBehaviour only requires CommandRequestSenders.
                    if (readRequirementCount == 0 && authRequirementCount == 0)
                    {
                        behavioursToEnable.Add(behaviour);
                    }

                    if (readRequirementCount > 0)
                    {
                        AddBehaviourForComponentIds(behaviour, componentReadRequirements, behavioursRequiringComponentsPresent);
                    }

                    if (authRequirementCount > 0)
                    {
                        AddBehaviourForComponentIds(behaviour, componentAuthRequirements, behavioursRequiringComponentsWithAuth);
                    }

                    numUnsatisfiedRequirements[behaviour] = componentReadRequirements.Count + componentAuthRequirements.Count;

                    RunWithExceptionHandling(() => behaviour.enabled = false);
                }
            }
        }
Exemplo n.º 2
0
        private void CreateActivationManagerAndReaderWriterStore(Entity entity)
        {
            if (entityToActivationManager.ContainsKey(entity))
            {
                throw new ArgumentException($"{nameof(MonoBehaviourActivationManager)} already exists for entity {entity.Index}.");
            }

            var store = new InjectableStore();

            EntityToReaderWriterStore.Add(entity, store);
            var gameObject = EntityManager.GetComponentObject <GameObjectReference>(entity).GameObject;
            var manager    = new MonoBehaviourActivationManager(gameObject, injector, store, logger);

            entityToActivationManager.Add(entity, manager);
        }