123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
-
- using CommonLang;
- namespace ET.Client
- {
- [ComponentOf(typeof(Scene))]
- public class UnitMgr : Entity, IAwake, IDestroy
- {
- public static UnitMgr Instance;
- public BattleActor Actor { get; set; }
- public uint ActorId
- {
- get
- {
- return Actor.Id;
- }
- }
- private HashMap<uint, BattleObject> UnitList = new();
- public HashMap<uint, BattleObject>.ValueCollection AllUnits
- {
- get { return UnitList.Values; }
- }
- public bool HasUnit(uint unitId)
- {
- return UnitList.ContainsKey(unitId);
- }
- public BattleObject GetUnit(uint id)
- {
- return UnitList.ContainsKey(id) ? UnitList[id] : null;
- }
- public void PutUnit(uint id, BattleObject obj)
- {
- if(UnitList.ContainsKey(id))
- {
- Log.Error($"Already exist unit({id}) @{obj}");
- UnitList.Remove(id);
- }
- UnitList[id] = obj;
- }
- public void RemoveUnit(uint id)
- {
- if (UnitList.ContainsKey(id))
- {
- var unit = UnitList[id];
- UnitList.Remove(id);
- }
- }
- public void RecycleUnits()
- {
- foreach (var kp in UnitList)
- {
- kp.Value.OnSleep();
- }
- UnitList.Clear();
- }
- }
- [ObjectSystem]
- public class UnitMgrAwakeSystem : AwakeSystem<UnitMgr>
- {
- protected override void Awake(UnitMgr self)
- {
- UnitMgr.Instance = self;
- self.RecycleUnits();
- }
- }
- [ObjectSystem]
- public class UnitMgrDestroySystem : DestroySystem<UnitMgr>
- {
- protected override void Destroy(UnitMgr self)
- {
- self.RecycleUnits();
- UnitMgr.Instance = null;
- }
- }
- }
|