Пример #1
0
    void Register(JoshECSComponent component)
    {
        //Cache component's entity
        GameObject entity = component.gameObject;

        //For each processor that deals with same-typed components and is interested in the entity
        foreach (JoshECSProcessor processor in processorsAndECs.Keys.Where(processor => processor.RequiredTypes.Contains(component.GetType()) && processor.isInterestedIn(entity)))
        {
            //Get EntityAndComponent link
            EntityAndComponents componentEntity = processorsAndECs[processor].FirstOrDefault(e => e.entity == entity);
            if (componentEntity == null)
            {
                componentEntity = new EntityAndComponents {
                    entity = entity
                };
                processorsAndECs [processor].Add(componentEntity);
            }

            //If a Component of the Same Type is already registered, do not register this component
            if (componentEntity.components.Any(c => c.GetType() == component.GetType()))
            {
                return;
            }

            //Add Component
            componentEntity.components.Add(component);

            //Add any missing components that are required for this processor
            if (!processor.RequiredTypes.All(t => componentEntity.components.Exists(c => c.GetType().Equals(t))))
            {
                List <JoshECSComponent> MissingComponents = entity.GetComponents <JoshECSComponent>().Where(c => !componentEntity.components.Contains(c) && processor.RequiredTypes.Contains(c.GetType())).ToList();
                componentEntity.components.AddRange(MissingComponents);
            }
        }
    }
Пример #2
0
    void Unregister(JoshECSComponent component)
    {
        GameObject entity = component.gameObject;

        foreach (JoshECSProcessor processor in processorsAndECs.Keys.Where(processor => processor.RequiredTypes.Contains(component.GetType()) && entity != null))
        {
            EntityAndComponents ec = processorsAndECs[processor].Find(e => e.entity == entity);
            //Debug.Log (ec.entity);
            processorsAndECs [processor].Remove(ec);
        }

        component.enabled = false;
    }