Ejemplo n.º 1
0
        private void CheckEntityWithProcessors(Entity entity, bool forceRemove, bool collecComponentTypesAndProcessors)
        {
            var             components = entity.Components;
            EntityComponent tc         = null;

            for (int i = 0; i < components.Count; i++)
            {
                var component = components[i];

                if (component is TransformComponent)
                {
                    // process the transform last, so parent stuff gets done first
                    tc = component;
                }
                else
                {
                    CheckEntityComponentWithProcessors(entity, component, forceRemove, null);
                    if (collecComponentTypesAndProcessors)
                    {
                        CollectNewProcessorsByComponentType(component.GetType().GetTypeInfo());
                    }
                }
            }

            if (tc != null)
            {
                CheckEntityComponentWithProcessors(entity, tc, forceRemove, null);
                if (collecComponentTypesAndProcessors)
                {
                    CollectNewProcessorsByComponentType(tc.GetType().GetTypeInfo());
                }
            }
        }
Ejemplo n.º 2
0
        private void CheckEntityComponentWithProcessors(Entity entity, EntityComponent component, bool forceRemove, List <EntityProcessor> dependentProcessors)
        {
            var componentType = component.GetType().GetTypeInfo();
            EntityProcessorCollectionPerComponentType processorsForComponent;

            if (MapComponentTypeToProcessors.TryGetValue(componentType, out processorsForComponent))
            {
                for (int i = 0; i < processorsForComponent.Count; i++)
                {
                    processorsForComponent[i].ProcessEntityComponent(entity, component, forceRemove);
                }
            }
            else
            {
                processorsForComponent = new EntityProcessorCollectionPerComponentType();
                for (int j = 0; j < processors.Count; j++)
                {
                    var processor = processors[j];
                    if (processor.Accept(componentType))
                    {
                        processorsForComponent.Add(processor);
                        processor.ProcessEntityComponent(entity, component, forceRemove);
                    }

                    if (processor.IsDependentOnComponentType(componentType))
                    {
                        if (processorsForComponent.Dependencies == null)
                        {
                            processorsForComponent.Dependencies = new List <EntityProcessor>();
                        }
                        processorsForComponent.Dependencies.Add(processor);
                    }
                }
                MapComponentTypeToProcessors.Add(componentType, processorsForComponent);
            }

            // Collect dependent processors
            var processorsForComponentDependencies = processorsForComponent.Dependencies;

            if (dependentProcessors != null && processorsForComponentDependencies != null)
            {
                for (int i = 0; i < processorsForComponentDependencies.Count; i++)
                {
                    var processor = processorsForComponentDependencies[i];
                    if (!dependentProcessors.Contains(processor))
                    {
                        dependentProcessors.Add(processor);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        internal void NotifyComponentChanged(Entity entity, int index, EntityComponent oldComponent, EntityComponent newComponent)
        {
            // No real update
            if (oldComponent == newComponent)
            {
                return;
            }

            // If we have a new component we can try to collect processors for it
            if (newComponent != null)
            {
                CollectNewProcessorsByComponentType(newComponent.GetType().GetTypeInfo());
                RegisterPendingProcessors();
            }

            // Remove previous component from processors
            currentDependentProcessors.Clear();
            if (oldComponent != null)
            {
                CheckEntityComponentWithProcessors(entity, oldComponent, true, currentDependentProcessors);
            }

            // Add new component to processors
            if (newComponent != null)
            {
                CheckEntityComponentWithProcessors(entity, newComponent, false, currentDependentProcessors);
            }

            // Update all dependencies
            if (currentDependentProcessors.Count > 0)
            {
                UpdateDependentProcessors(entity, oldComponent, newComponent);
                currentDependentProcessors.Clear();
            }

            // Notify component changes
            OnComponentChanged(entity, index, oldComponent, newComponent);
        }