Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / common / NodeExtend.cs
@小李xl 小李xl on 2 Nov 2023 6 KB 完成对象池基础功能
  1. using System;
  2. using System.Collections;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 该类为 node 节点通用扩展函数类
  7. /// </summary>
  8. public static class NodeExtend
  9. {
  10. /// <summary>
  11. /// 尝试将一个 Node2d 节点转换成一个 ActivityObject 对象, 如果转换失败, 则返回 null
  12. /// </summary>
  13. public static ActivityObject AsActivityObject(this Node2D node2d)
  14. {
  15. if (node2d is ActivityObject p)
  16. {
  17. return p;
  18. }
  19. var parent = node2d.GetParent();
  20. if (parent != null && parent is ActivityObject p2)
  21. {
  22. return p2;
  23. }
  24. return null;
  25. }
  26. /// <summary>
  27. /// 尝试将一个 Node2d 节点转换成一个 ActivityObject 对象, 如果转换失败, 则返回 null
  28. /// </summary>
  29. public static T AsActivityObject<T>(this Node2D node2d) where T : ActivityObject
  30. {
  31. if (node2d is T p)
  32. {
  33. return p;
  34. }
  35. var parent = node2d.GetParent();
  36. if (parent != null && parent is T p2)
  37. {
  38. return p2;
  39. }
  40. return null;
  41. }
  42.  
  43. /// <summary>
  44. /// 将节点插入的房间物体根节点
  45. /// </summary>
  46. /// <param name="node">实例</param>
  47. /// <param name="layer">放入的层</param>
  48. public static void AddToActivityRoot(this Node2D node, RoomLayerEnum layer)
  49. {
  50. GameApplication.Instance.World.GetRoomLayer(layer).AddChild(node);
  51. }
  52. /// <summary>
  53. /// 将节点插入的房间物体根节点,延时调用
  54. /// </summary>
  55. /// <param name="node">实例</param>
  56. /// <param name="layer">放入的层</param>
  57. public static void AddToActivityRootDeferred(this Node2D node, RoomLayerEnum layer)
  58. {
  59. GameApplication.Instance.World.GetRoomLayer(layer).CallDeferred(Node.MethodName.AddChild, node);
  60. }
  61.  
  62. /// <summary>
  63. /// 设置Ui布局方式是否横向扩展, 如果为 true, 则 GridContainer 的宽度会撑满父物体
  64. /// </summary>
  65. public static void SetHorizontalExpand(this Control control, bool flag)
  66. {
  67. if (flag)
  68. {
  69. control.SizeFlagsHorizontal |= Control.SizeFlags.Expand;
  70. }
  71. else if ((control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0)
  72. {
  73. control.SizeFlagsHorizontal ^= Control.SizeFlags.Expand;
  74. }
  75. }
  76.  
  77. /// <summary>
  78. /// 获取Ui布局方式是否横向扩展
  79. /// </summary>
  80. public static bool GetHorizontalExpand(this Control control)
  81. {
  82. return (control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0;
  83. }
  84.  
  85. /// <summary>
  86. /// 设置是否启用节点
  87. /// </summary>
  88. public static void SetActive(this Node node, bool value)
  89. {
  90. if (node is CanvasItem canvasItem)
  91. {
  92. canvasItem.Visible = value;
  93. }
  94. node.SetProcess(value);
  95. node.SetPhysicsProcess(value);
  96. node.SetProcessInput(value);
  97. node.SetPhysicsProcessInternal(value);
  98. node.SetProcessInput(value);
  99. }
  100. /// <summary>
  101. /// 延时指定时间调用一个回调函数
  102. /// </summary>
  103. public static void CallDelay(this ICoroutine coroutine, float delayTime, Action cb)
  104. {
  105. coroutine.StartCoroutine(_CallDelay(delayTime, cb));
  106. }
  107. /// <summary>
  108. /// 延时指定时间调用一个回调函数
  109. /// </summary>
  110. public static void CallDelay<T1>(this ICoroutine coroutine, float delayTime, Action<T1> cb, T1 arg1)
  111. {
  112. coroutine.StartCoroutine(_CallDelay(delayTime, cb, arg1));
  113. }
  114. /// <summary>
  115. /// 延时指定时间调用一个回调函数
  116. /// </summary>
  117. public static void CallDelay<T1, T2>(this ICoroutine coroutine, float delayTime, Action<T1, T2> cb, T1 arg1, T2 arg2)
  118. {
  119. coroutine.StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2));
  120. }
  121. /// <summary>
  122. /// 延时指定时间调用一个回调函数
  123. /// </summary>
  124. public static void CallDelay<T1, T2, T3>(this ICoroutine coroutine, float delayTime, Action<T1, T2, T3> cb, T1 arg1, T2 arg2, T3 arg3)
  125. {
  126. coroutine.StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2, arg3));
  127. }
  128. //---------------------------
  129. /// <summary>
  130. /// 延时指定时间调用一个回调函数
  131. /// </summary>
  132. public static void CallDelayInNode(this Node node, float delayTime, Action cb)
  133. {
  134. GameApplication.Instance.StartCoroutine(_CallDelay(delayTime, cb));
  135. }
  136. /// <summary>
  137. /// 延时指定时间调用一个回调函数
  138. /// </summary>
  139. public static void CallDelayInNode<T1>(this Node node, float delayTime, Action<T1> cb, T1 arg1)
  140. {
  141. GameApplication.Instance.StartCoroutine(_CallDelay(delayTime, cb, arg1));
  142. }
  143. /// <summary>
  144. /// 延时指定时间调用一个回调函数
  145. /// </summary>
  146. public static void CallDelayInNode<T1, T2>(this Node node, float delayTime, Action<T1, T2> cb, T1 arg1, T2 arg2)
  147. {
  148. GameApplication.Instance.StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2));
  149. }
  150. /// <summary>
  151. /// 延时指定时间调用一个回调函数
  152. /// </summary>
  153. public static void CallDelayInNode<T1, T2, T3>(this Node node, float delayTime, Action<T1, T2, T3> cb, T1 arg1, T2 arg2, T3 arg3)
  154. {
  155. GameApplication.Instance.StartCoroutine(_CallDelay(delayTime, cb, arg1, arg2, arg3));
  156. }
  157. private static IEnumerator _CallDelay(float delayTime, Action cb)
  158. {
  159. yield return new WaitForSeconds(delayTime);
  160. cb();
  161. }
  162. private static IEnumerator _CallDelay<T1>(float delayTime, Action<T1> cb, T1 arg1)
  163. {
  164. yield return new WaitForSeconds(delayTime);
  165. cb(arg1);
  166. }
  167. private static IEnumerator _CallDelay<T1, T2>(float delayTime, Action<T1, T2> cb, T1 arg1, T2 arg2)
  168. {
  169. yield return new WaitForSeconds(delayTime);
  170. cb(arg1, arg2);
  171. }
  172. private static IEnumerator _CallDelay<T1, T2, T3>(float delayTime, Action<T1, T2, T3> cb, T1 arg1, T2 arg2, T3 arg3)
  173. {
  174. yield return new WaitForSeconds(delayTime);
  175. cb(arg1,arg2, arg3);
  176. }
  177. }