using ET; using System.Collections.Generic; using System.Linq; namespace ET { public class UnitComponentSystem : AwakeSystem { public override void Awake(UnitComponent self) { self.Awake(); } } public class UnitComponent : Entity { public static UnitComponent Instance { get; private set; } public static Unit MyUnit { get; set; } private readonly Dictionary idUnits = new Dictionary(); public void Awake() { Instance = this; } public override void Dispose() { if (this.IsDisposed) { return; } base.Dispose(); foreach (Unit unit in this.idUnits.Values) { unit.Dispose(); } this.idUnits.Clear(); Instance = null; } public void Add(Unit unit) { this.idUnits.Add(unit.Id, unit); } public Unit Get(long id) { this.idUnits.TryGetValue(id, out Unit unit); return unit; } public void Remove(long id, bool isOffLine = false) { if (id == GlobalVariable.MyId) return; if (this.idUnits.TryGetValue(id, out var unit)) { this.idUnits.Remove(id); unit.Dispose(); } else { Log.Error($"{id}不存在"); } } public void Remove(Unit unit, bool isOffLine = false) { this.idUnits.Remove(unit.Id); unit.Dispose(); } public void RemoveAll() { using var listComponent = ListComponent.Create(); foreach (var unit in GetAll()) { if (unit.Id != MyUnit.Id) { listComponent.List.Add(unit); } } foreach (var id in listComponent.List) { Remove(id); } } public void RemoveNoDispose(long id) { this.idUnits.Remove(id); } public int Count { get { return this.idUnits.Count; } } public IEnumerable GetAll() { return this.idUnits.Values; } } }