/// <summary> /// 删除一个组件 /// </summary> /// <param name="component">组件实体</param> /// <returns>删除成功返回true,失败返回false</returns> public bool Remove(BaseComponent component) { if (!IdComponents.TryGetValue(component.Id, out BaseComponent value)) { return(false); } if (TypeComponents.TryGetValue(component.GetType(), out HashSet <BaseComponent> hashVal)) { hashVal.Remove(component); } return(false); }
public static void PutBack(this BaseComponent component) { if (!IdComponents.TryRemove(component.Id, out BaseComponent val)) { return; } var type = component.GetType(); if (!TypeComponents.TryGetValue(type, out ConcurrentQueue <BaseComponent> queue)) { queue = new ConcurrentQueue <BaseComponent>(); TypeComponents[type] = queue; } component.Id = 0; queue.Enqueue(component); }
/// <summary> /// 添加一个组件。 /// </summary> /// <typeparam name="T">组件类型。</typeparam> /// <param name="component">组件实体。</param> /// <returns>新增成功返回true,失败返回false,如果返回false表示池中已经存在该组件。</returns> public bool AddComponent(BaseComponent component) { var type = component.GetType(); var isSingle = ObjectStorage.IsSingleType(type); var isNew = false; if (isSingle) { isNew = !SingleComponents.ContainsKey(type); if (isNew) { SingleComponents[type] = component; } } else { isNew = !IdComponents.ContainsKey(component.Id); if (isNew) { IdComponents[component.Id] = component; if (!TypeComponents.TryGetValue(type, out HashSet <BaseComponent> components)) { components = new HashSet <BaseComponent>(); TypeComponents[type] = components; } components.Add(component); } } if (isNew) { Game.Event.Add(component); } return(isNew); }