UnitMgr.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 
  2. using CommonLang;
  3. namespace ET.Client
  4. {
  5. [ComponentOf(typeof(Scene))]
  6. public class UnitMgr : Entity, IAwake, IDestroy
  7. {
  8. public static UnitMgr Instance;
  9. public BattleActor Actor { get; set; }
  10. public uint ActorId
  11. {
  12. get
  13. {
  14. return Actor.Id;
  15. }
  16. }
  17. private HashMap<uint, BattleObject> UnitList = new();
  18. public HashMap<uint, BattleObject>.ValueCollection AllUnits
  19. {
  20. get { return UnitList.Values; }
  21. }
  22. public bool HasUnit(uint unitId)
  23. {
  24. return UnitList.ContainsKey(unitId);
  25. }
  26. public BattleObject GetUnit(uint id)
  27. {
  28. return UnitList.ContainsKey(id) ? UnitList[id] : null;
  29. }
  30. public void PutUnit(uint id, BattleObject obj)
  31. {
  32. if(UnitList.ContainsKey(id))
  33. {
  34. Log.Error($"Already exist unit({id}) @{obj}");
  35. UnitList.Remove(id);
  36. }
  37. UnitList[id] = obj;
  38. }
  39. public void RemoveUnit(uint id)
  40. {
  41. if (UnitList.ContainsKey(id))
  42. {
  43. var unit = UnitList[id];
  44. UnitList.Remove(id);
  45. }
  46. }
  47. public void RecycleUnits()
  48. {
  49. foreach (var kp in UnitList)
  50. {
  51. kp.Value.OnSleep();
  52. }
  53. UnitList.Clear();
  54. }
  55. }
  56. [ObjectSystem]
  57. public class UnitMgrAwakeSystem : AwakeSystem<UnitMgr>
  58. {
  59. protected override void Awake(UnitMgr self)
  60. {
  61. UnitMgr.Instance = self;
  62. self.RecycleUnits();
  63. }
  64. }
  65. [ObjectSystem]
  66. public class UnitMgrDestroySystem : DestroySystem<UnitMgr>
  67. {
  68. protected override void Destroy(UnitMgr self)
  69. {
  70. self.RecycleUnits();
  71. UnitMgr.Instance = null;
  72. }
  73. }
  74. }