Newer
Older
DungeonShooting / DungeonShooting_Godot / src / effect / ThrowNode.cs
  1. using System;
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 模拟抛出的物体, 使用时将对象挂载到该节点上即可
  6. /// </summary>
  7. public class ThrowNode : KinematicBody2D
  8. {
  9. /// <summary>
  10. /// 是否已经结束
  11. /// </summary>
  12. /// <value></value>
  13. public bool IsOver { get; protected set; } = true;
  14. /// <summary>
  15. /// 物体大小
  16. /// </summary>
  17. public Vector2 Size { get; protected set; }
  18. /// <summary>
  19. /// 起始坐标
  20. /// </summary>
  21. public Vector2 StartPosition { get; protected set; }
  22. /// <summary>
  23. /// 移动方向, 0 - 360
  24. /// </summary>
  25. /// <value></value>
  26. public float Direction { get; protected set; }
  27. /// <summary>
  28. /// x速度, 也就是水平速度
  29. /// </summary>
  30. public float XSpeed { get; protected set; }
  31. /// <summary>
  32. /// y轴速度, 也就是竖直速度
  33. /// </summary>
  34. /// <value></value>
  35. public float YSpeed { get; protected set; }
  36. /// <summary>
  37. /// 初始x轴组队
  38. /// </summary>
  39. /// <value></value>
  40. public float StartXSpeed { get; protected set; }
  41. /// <summary>
  42. /// 初始y轴速度
  43. /// </summary>
  44. public float StartYSpeed { get; protected set; }
  45. /// <summary>
  46. /// 旋转速度
  47. /// </summary>
  48. public float RotateSpeed { get; protected set; }
  49. /// <summary>
  50. /// 挂载的对象
  51. /// </summary>
  52. public Node2D Mount { get; protected set; }
  53. /// <summary>
  54. /// 碰撞组件
  55. /// </summary>
  56. /// <value></value>
  57. public CollisionShape2D CollisionShape { get; protected set; }
  58. /// <summary>
  59. /// 绘制阴影的精灵
  60. /// </summary>
  61. public Sprite ShadowSprite { get; protected set; }
  62.  
  63. protected Sprite ShadowTarget { get; set; }
  64.  
  65. private bool inversionX = false;
  66.  
  67. public override void _Ready()
  68. {
  69. //只与墙壁碰撞
  70. CollisionMask = 1;
  71. CollisionLayer = 0;
  72. //创建碰撞器
  73. CollisionShape = new CollisionShape2D();
  74. var shape = new RectangleShape2D();
  75. shape.Extents = Size * 0.5f;
  76. CollisionShape.Shape = shape;
  77. AddChild(CollisionShape);
  78. }
  79.  
  80. /// <summary>
  81. /// 初始化该抛物线对象的基础数据
  82. /// </summary>
  83. /// <param name="size">抛射的物体所占大小, 用于碰撞检测</param>
  84. /// <param name="start">起始点</param>
  85. /// <param name="startHeight">起始高度</param>
  86. /// <param name="direction">角度, 0 - 360</param>
  87. /// <param name="xSpeed">横轴速度</param>
  88. /// <param name="ySpeed">纵轴速度</param>
  89. /// <param name="rotate">旋转速度</param>
  90. /// <param name="mount">需要挂载的节点</param>
  91. /// <param name="texutre">抛射的节点显示的纹理, 用于渲染阴影用</param>
  92. public void InitThrow(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate, Node2D mount)
  93. {
  94. if (CollisionShape != null)
  95. {
  96. CollisionShape.Disabled = false;
  97. }
  98.  
  99. IsOver = false;
  100. Size = size;
  101. GlobalPosition = StartPosition = start;
  102. Direction = direction;
  103. XSpeed = xSpeed;
  104. YSpeed = ySpeed;
  105. StartXSpeed = xSpeed;
  106. StartYSpeed = ySpeed;
  107. RotateSpeed = rotate;
  108.  
  109. if (mount != null)
  110. {
  111. Mount = mount;
  112. AddChild(mount);
  113. mount.Position = new Vector2(0, -startHeight);
  114. }
  115. }
  116.  
  117. /// <summary>
  118. /// 初始化该抛物线对象的基础数据, 并且渲染阴影
  119. /// </summary>
  120. /// <param name="size">抛射的物体所占大小, 用于碰撞检测</param>
  121. /// <param name="start">起始点</param>
  122. /// <param name="startHeight">起始高度</param>
  123. /// <param name="direction">角度, 0 - 360</param>
  124. /// <param name="xSpeed">横轴速度</param>
  125. /// <param name="ySpeed">纵轴速度</param>
  126. /// <param name="rotate">旋转速度</param>
  127. /// <param name="mount">需要挂载的节点</param>
  128. /// <param name="shadowTarget">抛射的节点显示的纹理, 用于渲染阴影用</param>
  129. public void InitThrow(Vector2 size, Vector2 start, float startHeight, float direction, float xSpeed, float ySpeed, float rotate, Node2D mount, Sprite shadowTarget)
  130. {
  131. InitThrow(size, start, startHeight, direction, xSpeed, ySpeed, rotate, mount);
  132. ShadowTarget = shadowTarget;
  133. if (shadowTarget != null)
  134. {
  135. if (ShadowSprite == null)
  136. {
  137. //阴影
  138. ShadowSprite = new Sprite();
  139. ShadowSprite.ZIndex = -5;
  140. ShadowSprite.Material = ResourceManager.ShadowMaterial;
  141. AddChild(ShadowSprite);
  142. }
  143. inversionX = mount.Scale.y < 0 ? true : false;
  144. if (inversionX)
  145. {
  146. ShadowSprite.Scale = shadowTarget.Scale * new Vector2(1, -1);
  147. }
  148. else
  149. {
  150. ShadowSprite.Scale = shadowTarget.Scale;
  151. }
  152. ShadowSprite.Texture = shadowTarget.Texture;
  153. }
  154. else if (ShadowSprite != null)
  155. {
  156. ShadowSprite.Texture = null;
  157. }
  158. }
  159.  
  160. /// <summary>
  161. /// 达到最高点时调用
  162. /// </summary>
  163. protected virtual void OnMaxHeight(float height)
  164. {
  165.  
  166. }
  167.  
  168. /// <summary>
  169. /// 结束的调用
  170. /// </summary>
  171. protected virtual void OnOver()
  172. {
  173.  
  174. }
  175.  
  176. public override void _Process(float delta)
  177. {
  178. if (!IsOver)
  179. {
  180. MoveAndSlide(new Vector2(XSpeed, 0).Rotated(Direction * Mathf.Pi / 180));
  181. Mount.Position = new Vector2(0, Mount.Position.y - YSpeed * delta);
  182. var rotate = Mount.GlobalRotationDegrees + RotateSpeed * delta;
  183. Mount.GlobalRotationDegrees = rotate;
  184. if (ShadowSprite != null)
  185. {
  186. ShadowSprite.GlobalRotationDegrees = rotate;
  187. // ShadowSprite.GlobalRotationDegrees = rotate + (inversionX ? 180 : 0);
  188. ShadowSprite.GlobalPosition = ShadowTarget.GlobalPosition + new Vector2(0, 1 - Mount.Position.y);
  189. }
  190. var ysp = YSpeed;
  191. YSpeed -= GameConfig.G * delta;
  192. //达到最高点
  193. if (ysp * YSpeed < 0)
  194. {
  195. OnMaxHeight(-Mount.Position.y);
  196. }
  197. //落地判断
  198. if (Mount.Position.y >= 0)
  199. {
  200. Mount.Position = new Vector2(0, 0);
  201. IsOver = true;
  202. CollisionShape.Disabled = true;
  203. OnOver();
  204. }
  205. }
  206. }
  207.  
  208. }