/// <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); }
/// <summary> /// 获取一个组件,不允许获取SingleCase /// </summary> /// <typeparam name="T">组件类型。</typeparam> /// <param name="id">组件Id。</param> /// <returns>返回一个类型的多例(ManyCase)组件。</returns> public T GetComponent <T>(int id) where T : BaseComponent { var type = typeof(T); var isSingle = ObjectStorage.IsSingleType(type); if (isSingle) { return(null); } if (IdComponents.TryGetValue(id, out BaseComponent component)) { return(null); } return((T)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); }