public override bool HandlePropertyValuesChanged(
            IReadOnlyList <IPropertyValueChange> changes, TEntity entity, IEntityGroup <TEntity> group)
        {
            if (base.HandlePropertyValuesChanged(changes, entity, group))
            {
                return(true);
            }

            Component componentUsed = null;

            for (var i = 0; i < changes.Count; i++)
            {
                var change = changes[i];
                if (_sortValueGetter.PropertyUsed(change.ChangedComponent.ComponentId, change.ChangedPropertyName))
                {
                    componentUsed = change.ChangedComponent;
                    break;
                }

                // The component might have been removed by the previous change listener
                if (!entity.HasComponent(change.ChangedComponent.ComponentId))
                {
                    return(true);
                }
            }

            if (componentUsed == null)
            {
                return(false);
            }

            if (!KeyValueGetter.TryGetKey(entity, new IPropertyValueChange[0], getOldValue: false, out var key))
            {
                return(true);
            }

            var entities = GetOrAddEntities(key);

            if (_sortValueGetter.TryGetKey(entity, changes, getOldValue: true, out var oldSortKey))
            {
                entities.Remove(oldSortKey);
            }

            if (_sortValueGetter.TryGetKey(entity, changes, getOldValue: false, out var newSortKey))
            {
                entities.Add(newSortKey, entity);
            }

            if (entities.Count == 0)
            {
                Index.Remove(key);
            }

            return(true);
        }
Exemplo n.º 2
0
 public void HandleEntityRemoved(
     TEntity entity, Component removedComponent, IEntityGroup <TEntity> group)
 {
     if (KeyValueGetter.TryGetKey(
             entity,
             new IPropertyValueChange[] { new PropertyValueChange <object>(removedComponent) },
             getOldValue: false,
             out var keyValue))
     {
         TryRemoveEntity(keyValue, entity, removedComponent);
     }
 }
Exemplo n.º 3
0
        public virtual bool HandlePropertyValuesChanged(
            IReadOnlyList <IPropertyValueChange> changes, TEntity entity, IEntityGroup <TEntity> group)
        {
            Component componentUsed = null;

            for (var i = 0; i < changes.Count; i++)
            {
                var change = changes[i];
                if (KeyValueGetter.PropertyUsed(change.ChangedComponent.ComponentId, change.ChangedPropertyName))
                {
                    componentUsed = change.ChangedComponent;
                    break;
                }

                // The component might have been removed by the previous change listener
                if (!entity.HasComponent(change.ChangedComponent.ComponentId))
                {
                    return(true);
                }
            }

            if (componentUsed == null)
            {
                return(false);
            }

            if (KeyValueGetter.TryGetKey(entity, changes, getOldValue: true, out var oldKey))
            {
                TryRemoveEntity(oldKey, entity, componentUsed);
            }

            if (KeyValueGetter.TryGetKey(entity, changes, getOldValue: false, out var newKey))
            {
                TryAddEntity(newKey, entity, componentUsed);
            }

            return(true);
        }
Exemplo n.º 4
0
 public SortedUniqueEntityIndex(
     string name,
     IEntityGroup <TEntity> group,
     KeyValueGetter <TEntity, TKey> keyValueGetter)
     : base(name, group, keyValueGetter)
     => Index = new SortedDictionary <TKey, TEntity>();
Exemplo n.º 5
0
 public EntityIndex(
     string name,
     IEntityGroup <TEntity> group,
     KeyValueGetter <TEntity, TKey> keyValueGetter)
     : base(name, group, keyValueGetter)
     => Index = new Dictionary <TKey, HashSet <TEntity> >();
 public bool ContainsEntity(TEntity entity)
 => KeyValueGetter.TryGetKey(entity, new IPropertyValueChange[0], getOldValue: false, out var referencedId) &&
 ReferencedGroup.ContainsEntity(referencedId);