/// <inheritdoc/> public override void Update(GameTime gameTime) { int currentlyToRemoveCount; lock (_toRemove) { currentlyToRemoveCount = Math.Min(_toRemove.Count, INITIAL_ARRAY_SIZE); _toRemove.CopyTo(0, _currentlyToRemove, 0, currentlyToRemoveCount); _toRemove.RemoveRange(0, currentlyToRemoveCount); } // ReSharper disable once InconsistentlySynchronizedField for (int i = 0; i < currentlyToRemoveCount; i++) { // ReSharper disable once InconsistentlySynchronizedField Entity entity = _currentlyToRemove[i]; for (int si = _entitySystemsCount - 1; si >= 0; si--) { EntitySystemBase system = _entitySystems[si]; if (entity.SystemFlags == 0 || (entity.SystemFlags & system.SystemMask) == system.SystemMask) { system.Remove(entity); } } } int currentlyToChangedCount; lock (_toChanged) { currentlyToChangedCount = Math.Min(_toChanged.Count, INITIAL_ARRAY_SIZE); _toChanged.CopyTo(0, _currentlyToChanged, 0, currentlyToChangedCount); _toChanged.RemoveRange(0, currentlyToChangedCount); } // ReSharper disable once InconsistentlySynchronizedField for (int i = 0; i < currentlyToChangedCount; i++) { // ReSharper disable once InconsistentlySynchronizedField Entity entity = _currentlyToChanged[i]; for (int si = _entitySystemsCount - 1; si >= 0; si--) { EntitySystemBase system = _entitySystems[si]; if (entity.SystemFlags == 0 || (entity.SystemFlags & system.SystemMask) == system.SystemMask) { system.Changed(entity); } } } for (int i = 0; i < _entityUpdateableSystemsCount; i++) { IUpdateableSystem system = _entityUpdateableSystems[i]; if (system.Enabled) { system.Update(gameTime); } } }
public void RemoveSystem(EntitySystemBase entitySystem) { if (_systems.Remove(entitySystem)) { if (IsRunning) { entitySystem.Deinitialize(); } } }
public void AddSystem(EntitySystemBase entitySystem) { if (!_systems.Contains(entitySystem)) { _systems.Add(entitySystem); if (IsRunning) { entitySystem.Initialize(this); } } }
internal EntitySystemConfiguration(EntitySystemConfigurationAttribute attribute, EntitySystemBase entitySystemBase) { Attribute = attribute; EntitySystemBase = entitySystemBase; }
private void InitializeEntitySystems(uint managerMask) { List <EntitySystemConfiguration> updateableConfigurations = new List <EntitySystemConfiguration>(32); List <EntitySystemConfiguration> drawableConfigurations = new List <EntitySystemConfiguration>(32); foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) { if (a.FullName.StartsWith("System")) { continue; } if (a.FullName.StartsWith("ms")) { continue; } foreach (Type t in a.GetTypes()) { if (!t.IsClass || t.IsAbstract || t.IsInterface) { continue; } if (typeof(EntitySystemBase).IsAssignableFrom(t)) { EntitySystemConfigurationAttribute attribute; if ((attribute = t.GetCustomAttribute <EntitySystemConfigurationAttribute>(false)) != null) { if (attribute.ManagerFlags == 0 || (managerMask & attribute.ManagerFlags) == attribute.ManagerFlags) { EntitySystemBase entitySystemBase = (EntitySystemBase)Activator.CreateInstance(t, this); entitySystemBase.SystemMask = attribute.SystemMask; _entitySystemsMap.Add(attribute.Name, entitySystemBase); foreach (var it in t.GetInterfaces()) { if (it == typeof(IUpdateableSystem)) { updateableConfigurations.Add(new EntitySystemConfiguration(attribute, entitySystemBase)); } if (it == typeof(IDrawableSystem)) { drawableConfigurations.Add(new EntitySystemConfiguration(attribute, entitySystemBase)); } if (it != typeof(IDisposable) && it != typeof(IUpdateableSystem) && it != typeof(IDrawableSystem)) { _entitySystemInterfaces.Add(it, entitySystemBase); } } } } } } } _entitySystems = updateableConfigurations.Concat(drawableConfigurations).Select(c => c.EntitySystemBase).ToArray(); SortEntitySystems(ref updateableConfigurations); // ReSharper disable once SuspiciousTypeConversion.Global _entityUpdateableSystems = updateableConfigurations.Select(c => (IUpdateableSystem)c.EntitySystemBase).ToArray(); _entityUpdateableSystemsCount = _entityUpdateableSystems.Length; updateableConfigurations.Clear(); SortEntitySystems(ref drawableConfigurations); // ReSharper disable once SuspiciousTypeConversion.Global _entityDrawableSystems = drawableConfigurations.Select(c => (IDrawableSystem)c.EntitySystemBase).ToArray(); _entityDrawableSystemsCount = _entityDrawableSystems.Length; drawableConfigurations.Clear(); _entitySystemsCount = _entityUpdateableSystemsCount + _entityDrawableSystemsCount; }
public bool TryGetSystem(string name, out EntitySystemBase system) { return(_entitySystemsMap.TryGetValue(name, out system)); }