zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Hotfix/Logic/Model/Game/Entity/User/UnitComponent.cs

108 lines
2.4 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
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; }
2021-04-08 20:09:59 +08:00
private readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
public void Awake()
{
2021-04-08 20:09:59 +08:00
}
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;
2021-04-11 19:50:39 +08:00
if (this.idUnits.TryGetValue(id, out Unit unit))
2021-04-08 20:09:59 +08:00
{
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();
2021-04-11 19:50:39 +08:00
foreach (Unit unit in GetAll())
2021-04-08 20:09:59 +08:00
{
if (unit.Id != MyUnit.Id)
{
listComponent.List.Add(unit);
}
}
2021-04-11 19:50:39 +08:00
foreach (Unit id in listComponent.List)
2021-04-08 20:09:59 +08:00
{
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;
}
}
}