Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / common / NodeExtend.cs
@小李xl 小李xl on 27 Oct 2022 3 KB 完成新版本投抛功能
  1. using Godot;
  2. using System;
  3.  
  4. /// <summary>
  5. /// 该类为 node 节点通用扩展函数类
  6. /// </summary>
  7. public static class NodeExtend
  8. {
  9.  
  10. public static ThrowNode StartThrow(this Node2D node, Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate)
  11. {
  12. return StartThrow<ThrowNode>(node, size, start, startHeight, direction, xSpeed, ySpeed, rotate);
  13. }
  14.  
  15. public static T StartThrow<T>(this Node2D node, Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate) where T : ThrowNode
  16. {
  17. return StartThrow<T>(node, size, start, startHeight, direction, xSpeed, ySpeed, rotate, null);
  18. }
  19.  
  20. public static ThrowNode StartThrow(this Node2D node, Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate, Sprite shadowTarget)
  21. {
  22. return StartThrow<ThrowNode>(node, size, start, startHeight, direction, xSpeed, ySpeed, rotate, shadowTarget);
  23. }
  24.  
  25. public static T StartThrow<T>(this Node2D node, Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate, Sprite shadowTarget) where T : ThrowNode
  26. {
  27. ThrowNode throwNode = node.GetParentOrNull<ThrowNode>();
  28. T inst;
  29. if (throwNode == null)
  30. {
  31. inst = Activator.CreateInstance<T>();
  32. }
  33. else if (throwNode is T)
  34. {
  35. inst = throwNode as T;
  36. }
  37. else
  38. {
  39. throwNode.StopThrow();
  40. inst = Activator.CreateInstance<T>();
  41. }
  42. inst.StartThrow(size, start, startHeight, direction, xSpeed, ySpeed, rotate, node, shadowTarget);
  43. return inst;
  44. }
  45.  
  46. /// <summary>
  47. /// 将一个节点扔到地上, 并设置显示的阴影, 函数返回根据该节点创建的 ThrowNode 节点
  48. /// </summary>
  49. public static ThrowNode PutDown(this ActivityObject node)
  50. {
  51. //return StartThrow(node, Vector2.Zero, node.Position, 0, 0, 0, 0, 0, shadowTarget);
  52. RoomManager.Current.ObjectRoot.AddChild(node);
  53. return null;
  54. }
  55.  
  56. /// <summary>
  57. /// 拾起一个 node 节点, 返回是否拾起成功
  58. /// </summary>
  59. public static bool Pickup(this Node2D node)
  60. {
  61. ThrowNode parent = node.GetParentOrNull<ThrowNode>();
  62. if (parent != null)
  63. {
  64. parent.StopThrow();
  65. return true;
  66. }
  67. return false;
  68. }
  69.  
  70. /// <summary>
  71. /// 尝试将一个node2d节点转换成一个 ActivityObject 类
  72. /// </summary>
  73. public static ActivityObject AsActivityObject(this Node2D node2d)
  74. {
  75. if (node2d is ActivityObject p)
  76. {
  77. return p;
  78. }
  79. var parent = node2d.GetParent();
  80. if (parent != null && parent is ActivityObject p2)
  81. {
  82. return p2;
  83. }
  84. return null;
  85. }
  86. public static T StartThrow<T>(this ActivityObject node, Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate) where T : ThrowComponent
  87. {
  88. T throwNode = node.GetComponent<T>();
  89. if (throwNode == null)
  90. {
  91. throwNode = Activator.CreateInstance<T>();
  92. node.AddComponent(throwNode);
  93. }
  94. else
  95. {
  96. throwNode.StopThrow();
  97. }
  98. throwNode.StartThrow(size, start, startHeight, direction, xSpeed, ySpeed, rotate);
  99. return throwNode;
  100. }
  101. }