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); } } }
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>(); } }
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); } } }