/// <summary> /// 清空缓冲区 /// </summary> public void Clear() { ActorTypeComponents.Clear(); TypeComponents.Clear(); IdComponents.Clear(); SingleComponents.Clear(); }
/// <summary> /// 获取一个SingleCase组件,不允许获取非SingleCase /// </summary> /// <typeparam name="T"></typeparam> /// <exception cref="ComponentException">非SingleCase类型组件异常。</exception> /// <returns>返回一个单例(SigleCase)组件实体</returns> public T GetComponent <T>() where T : BaseComponent { var type = typeof(T); var isSingle = ObjectStorage.IsSingleType(type); if (!isSingle) { return(null); } if (!SingleComponents.TryGetValue(type, 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); }