public void OnEntityComponentChange(Entity entity, IComponent component) { List <ECSGroup> oldSystems; if (!entityToGroupDic.TryGetValue(entity, out oldSystems)) { oldSystems = new List <ECSGroup>(); entityToGroupDic.Add(entity, oldSystems); } List <ECSGroup> newGroupList = GetEntitySuportGroup(entity); entityToGroupDic[entity] = newGroupList; for (int i = 0; i < newGroupList.Count; i++) { ECSGroup sys = newGroupList[i]; if (!oldSystems.Contains(sys)) { List <Entity> list = groupToEntityDic[sys]; if (list == null) { list = new List <Entity>(); } list.Add(entity); } } for (int i = 0; i < oldSystems.Count; i++) { ECSGroup sys = oldSystems[i]; if (!newGroupList.Contains(sys)) { List <Entity> list = groupToEntityDic[sys]; if (list == null) { list = new List <Entity>(); } list.Remove(entity); } } }
private bool AddGroup(int key, string[] componentFilter) { if (componentFilter == null || componentFilter.Length == 0) { Debug.LogError("AddGroup 失败,参数不能为空!"); return(false); } if (allGroupDic.ContainsKey(key)) { Debug.LogError("AddGroup 失败,名字重复!"); return(false); } ECSGroup group = new ECSGroup(key, componentFilter); allGroupDic.Add(key, group); List <Entity> newListEntity = new List <Entity>(); List <Entity> listEntity = new List <Entity>(entityToGroupDic.Keys); for (int i = 0; i < listEntity.Count; i++) { Entity entity = listEntity[i]; List <ECSGroup> newGroupList = GetEntitySuportGroup(entity); bool isContains = true; for (int j = 0; j < componentFilter.Length; j++) { if (!entity.Components.ContainsKey(componentFilter[j])) { isContains = false; } } if (isContains) { newListEntity.Add(entity); entityToGroupDic[entity].Add(group); } } groupToEntityDic.Add(group, newListEntity); return(true); }