/// Returns a group for the specified matcher. /// Calling context.GetGroup(matcher) with the same matcher will always /// return the same instance of the group. public IGroup <TEntity> GetGroup(IMatcher <TEntity> matcher) { if (!_groups.TryGetValue(matcher, out IGroup <TEntity> group)) { group = new Group <TEntity>(matcher); var entities = GetEntities(); for (int i = 0; i < entities.Length; i++) { group.HandleEntitySilently(entities[i]); } _groups.Add(matcher, group); for (int i = 0; i < matcher.indices.Length; i++) { var index = matcher.indices[i]; if (_groupsForIndex[index] == null) { _groupsForIndex[index] = new List <IGroup <TEntity> >(); } _groupsForIndex[index].Add(group); } OnGroupCreated?.Invoke(this, group); } return(group); }
public Group <T> GetGroup(IMatcher matcher) { Group <T> group; if (!_groups.TryGetValue(matcher, out group)) { group = new Group <T>(matcher); var entities = GetEntities(); for (int i = 0; i < entities.Length; i++) { group.HandleEntitySilently(entities[i]); } _groups.Add(matcher, group); for (int i = 0; i < matcher.Types.Length; i++) { var type = matcher.Types[i]; if (_groupsForTypes[type] == null) { _groupsForTypes[type] = new List <Group <T> >(); } _groupsForTypes[type].Add(group); } OnGroupCreated?.Invoke(this, @group); } return(group); }
static void OverwriteGroup(int index, ShipGroup sg) { if (_groups[index] != null) { RemoveGroup(index); } _groups[index] = sg; OnGroupCreated?.Invoke(index); }
public static void CreateGroup(int index) { if (_selectedShips.Count == 0) { return; } //need to remove selected ships from any old group for (int i = 0; i < _groups.Length; i++) { if (i == index) { continue; } for (int j = 0; j < _selectedShips.Count; j++) { if (_groups[i] != null && _groups[i].Contains(_selectedShips[j])) { _groups[i].Remove(_selectedShips[j]); if (_groups[i].ships.Count == 0) { RemoveGroup(i); } else { OverwriteGroup(i, new ShipGroup(_groups[i].ships.ToList())); } } } } if (_groups[index] != null) { RemoveGroup(index); } _groups[index] = new ShipGroup(_selectedShips.ToList()); OnGroupCreated?.Invoke(index); }