コード例 #1
0
        public void RemoveComponentWithConcreteType(int entityId, Type type)
        {
            var success = _entityComponentMap.TryGetValue(entityId, out var components);

            if (!success)
            {
                throw new Exception($"Trying to remove component from nonexistant entity with Id {entityId}");
            }

            components.Remove(type);

            // update matchers
            var entity = _entities[entityId];

            foreach (var pair in _matchers)
            {
                var signature = pair.Key;
                var entities  = pair.Value;
                if (ComponentSignatureManager.IsTypeInSignatureRequirements(signature, type) && entities.Entities.Contains(entity))
                {
                    entities.Entities.Remove(entity);
                }
                if (ComponentSignatureManager.IsTypeInSignatureRestrictions(signature, type) && DoesEntityMatchSignature(entityId, signature))
                {
                    entities.Entities.Add(entity);
                }
            }
        }
コード例 #2
0
        private bool DoesEntityMatchSignature(int entityId, BitSet signature)
        {
            var components     = _entityComponentMap[entityId];
            var componentTypes = new List <Type>(components.Keys);

            return(ComponentSignatureManager.CheckAgainstComponentSignature(signature, componentTypes));
        }
コード例 #3
0
ファイル: EntitySet.cs プロジェクト: kowsen/Minotaur
 public void RemoveComponentFromAll <T>() where T : IComponent
 {
     if (!ComponentSignatureManager.IsTypeInSignatureRequirements(_signature, typeof(T)))
     {
         throw new Exception("Trying to remove non-required component from all in EntitySet");
     }
     foreach (var entity in Entities)
     {
         entity.RemoveComponent <T>();
     }
 }
コード例 #4
0
        private void AddComponentWithConcreteType(int entityId, IComponent component, Type type)
        {
            if (!_entities.ContainsKey(entityId))
            {
                _entities[entityId] = new Entity(entityId, this);
            }

            var success = _entityComponentMap.TryGetValue(entityId, out var components);

            if (!success)
            {
                components = new Dictionary <Type, IComponent>();
                _entityComponentMap[entityId] = components;
            }

            if (components.ContainsKey(type))
            {
                throw new Exception($"Inserting duplicate component into entity with Id {entityId}");
            }

            component.Attach(entityId);
            components[type] = component;

            // update matchers
            var entity = _entities[entityId];

            foreach (var pair in _matchers)
            {
                var signature = pair.Key;
                var entities  = pair.Value;
                if (ComponentSignatureManager.IsTypeInSignatureRequirements(signature, type) && DoesEntityMatchSignature(entityId, signature))
                {
                    entities.Entities.Add(entity);
                }
                if (ComponentSignatureManager.IsTypeInSignatureRestrictions(signature, type) && entities.Entities.Contains(entity) && !DoesEntityMatchSignature(entityId, signature))
                {
                    entities.Entities.Remove(entity);
                }
            }
        }
コード例 #5
0
        public EntitySet Get(BitSet signature)
        {
            var success = _matchers.TryGetValue(signature, out var entities);

            if (!success)
            {
                if (!ComponentSignatureManager.ValidateSignature(signature))
                {
                    throw new Exception("Trying to fetch with invalid signature (including and excluding the same component)");
                }
                entities = new EntitySet(signature, this);
                foreach (var entityId in _entityComponentMap.Keys)
                {
                    if (DoesEntityMatchSignature(entityId, signature))
                    {
                        entities.Entities.Add(_entities[entityId]);
                    }
                }
                _matchers[signature] = entities;
            }
            return(entities);
        }