public static T Fetch <T, K1>(K1 k1) where T : BaseComponent { var type = typeof(T); BaseComponent result; if (IsSingleType(type)) { if (!SingleCaseComponents.TryGetValue(type, out result)) { result = ComponentFactory.CreateComponent(type, k1); SetId(result); SingleCaseComponents[type] = result; } } else { if (!TypeComponents.TryGetValue(type, out ConcurrentQueue <BaseComponent> queue)) { queue = new ConcurrentQueue <BaseComponent>(); TypeComponents[type] = queue; } if (!queue.TryDequeue(out result)) { result = ComponentFactory.CreateComponent(type, k1); } SetId(result); IdComponents[result.Id] = result; } return((T)result); }
public static BaseComponent Fetch(Type type) { BaseComponent result; if (IsSingleType(type)) { if (!SingleCaseComponents.TryGetValue(type, out result)) { result = ComponentFactory.CreateComponent(type); SetId(result); SingleCaseComponents[type] = result; } } else { if (!TypeComponents.TryGetValue(type, out ConcurrentQueue <BaseComponent> queue)) { queue = new ConcurrentQueue <BaseComponent>(); TypeComponents[type] = queue; } if (!queue.TryDequeue(out result)) { result = ComponentFactory.CreateComponent(type); } SetId(result); IdComponents[result.Id] = result; } return(result); }
/// <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); }
/// <summary> /// 删除一个组件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="component">组件实体</param> /// <returns>删除成功返回true,失败返回false</returns> public bool Remove <T>(T component) where T : BaseComponent { if (!IdComponents.TryGetValue(component.Id, out BaseComponent value)) { return(false); } var type = typeof(T); if (TypeComponents.TryGetValue(type, out HashSet <BaseComponent> hashVal)) { hashVal.Remove(component); } return(false); }
/// <summary> /// 获取一个组件集合,不允许获取SingleCase /// </summary> /// <typeparam name="T">组件类型。</typeparam> /// <returns>返回一个类型的多例(ManyCase)组件集合。</returns> public HashSet <BaseComponent> GetComponents <T>() where T : BaseComponent { var type = typeof(T); var isSingle = ObjectStorage.IsSingleType(type); if (isSingle) { return(null); } if (!TypeComponents.TryGetValue(type, out HashSet <BaseComponent> components)) { return(null); } return(components); }
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); }
public static void Load() { var types = ComponentFactory.CmponentTypes; foreach (var type in types) { var attribute = type.GetCustomAttribute <SingleCaseAttribute>(); if (attribute != null) { SingleTypes.Add(type); continue; } if (!TypeComponents.TryGetValue(type, out ConcurrentQueue <BaseComponent> queue)) { queue = new ConcurrentQueue <BaseComponent>(); TypeComponents[type] = queue; } } }
/// <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); }