NodeTree.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using XGame.Framework.Asyncs;
  4. using XGame.Framework.Interfaces;
  5. namespace XGame.Framework.Nodes
  6. {
  7. public class NodeTree : INodeTree, IUpdate, ILateUpdate, IDisposable
  8. {
  9. public INode Root { get; private set; }
  10. private NodeContext _context;
  11. private Dictionary<uint, NodeGroup> _nodeGroupMap;
  12. private List<IUpdate> _updates;
  13. private List<ILateUpdate> _lateUpdate;
  14. public NodeTree(NodeContext context, INode root)
  15. {
  16. _context = context;
  17. Root = root;
  18. _nodeGroupMap = new Dictionary<uint, NodeGroup>();
  19. _updates = new List<IUpdate>();
  20. _lateUpdate = new List<ILateUpdate>();
  21. if (root is IUpdate update)
  22. {
  23. _updates.Add(update);
  24. }
  25. if (root is ILateUpdate lateUpdate)
  26. {
  27. _lateUpdate.Add(lateUpdate);
  28. }
  29. }
  30. #region ½Ó¿ÚʵÏÖ
  31. void INodeTree.RemoveGroup(uint groupId, bool isDestroy)
  32. {
  33. if (_nodeGroupMap.TryGetValue(groupId, out var group))
  34. {
  35. var removeAsync = group.RemoveAll(isDestroy);
  36. if (isDestroy)
  37. {
  38. _nodeGroupMap.Remove(groupId);
  39. removeAsync.On(_ =>
  40. {
  41. Log.Debug($"NodeTree RemoveGroup Callback. GroupId:{groupId}");
  42. (group as IDisposable)?.Dispose();
  43. });
  44. }
  45. }
  46. }
  47. public IAsync AddAsync(NodeKey nodeKey, object intent)
  48. {
  49. return GetGroup(nodeKey.GroupId).AddAsync(nodeKey, intent);
  50. }
  51. public bool IsActive(NodeKey nodeKey)
  52. {
  53. return GetGroup(nodeKey.GroupId).IsActive(nodeKey);
  54. }
  55. public IAsync Preload(NodeKey nodeKey)
  56. {
  57. return GetGroup(nodeKey.GroupId).Preload(nodeKey);
  58. }
  59. public void Remove(NodeKey nodeKey, bool isDestroy)
  60. {
  61. GetGroup(nodeKey.GroupId).Remove(nodeKey, isDestroy);
  62. }
  63. void IUpdate.Update(int millisecond)
  64. {
  65. foreach(var impl in _updates)
  66. {
  67. impl.Update(millisecond);
  68. }
  69. }
  70. void ILateUpdate.LateUpdate(int millisecond)
  71. {
  72. foreach (var impl in _lateUpdate)
  73. {
  74. impl.LateUpdate(millisecond);
  75. }
  76. }
  77. void IDisposable.Dispose()
  78. {
  79. foreach(var item in _nodeGroupMap)
  80. {
  81. (item.Value as IDisposable)?.Dispose();
  82. }
  83. _nodeGroupMap.Clear();
  84. _updates.Clear();
  85. _lateUpdate.Clear();
  86. Root.Disable();
  87. (Root as IDisposable)?.Dispose();
  88. Root = null;
  89. (_context as IDisposable)?.Dispose();
  90. _context = null;
  91. }
  92. #endregion
  93. #region ÄÚ²¿·½·¨
  94. private INodeGroup GetGroup(uint id)
  95. {
  96. if (!_nodeGroupMap.TryGetValue(id, out var group))
  97. {
  98. group = new NodeGroup(_context.Clone());
  99. _nodeGroupMap.Add(id, group);
  100. _updates.Add(group);
  101. _lateUpdate.Add(group);
  102. }
  103. return group;
  104. }
  105. #endregion
  106. }
  107. }