108 lines
2.4 KiB
C#
108 lines
2.4 KiB
C#
using ET;
|
|
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace ET
|
|
{
|
|
|
|
public class UnitComponentSystem : AwakeSystem<UnitComponent>
|
|
{
|
|
public override void Awake(UnitComponent self)
|
|
{
|
|
self.Awake();
|
|
}
|
|
}
|
|
|
|
public class UnitComponent : Entity
|
|
{
|
|
public Unit MyUnit { get; set; }
|
|
|
|
private readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
|
|
|
|
public void Awake()
|
|
{
|
|
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
if (this.IsDisposed)
|
|
{
|
|
return;
|
|
}
|
|
base.Dispose();
|
|
|
|
foreach (Unit unit in this.idUnits.Values)
|
|
{
|
|
unit.Dispose();
|
|
}
|
|
|
|
this.idUnits.Clear();
|
|
}
|
|
|
|
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 ==this.ZoneScene().GetComponent<GlobalVariable>().MyId) return;
|
|
if (this.idUnits.TryGetValue(id, out Unit 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<Unit>.Create();
|
|
foreach (Unit unit in GetAll())
|
|
{
|
|
if (unit.Id != MyUnit.Id)
|
|
{
|
|
listComponent.List.Add(unit);
|
|
}
|
|
}
|
|
foreach (Unit 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<Unit> GetAll()
|
|
{
|
|
return this.idUnits.Values;
|
|
}
|
|
}
|
|
} |