Newer
Older
DungeonShooting / src / weapon / ThrowNode.cs
@小李xl 小李xl on 9 Jun 2022 1 KB 实现投抛物体
  1. using System;
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 模拟抛出的物体, 使用时将对象挂载到该节点上即可
  6. /// </summary>
  7. public class ThrowNode : Node2D
  8. {
  9. public float MaxHeight = 30;
  10. public float TargetHeight = 0;
  11. public float StartHeight = 0;
  12.  
  13. public Vector2 StartPos;
  14. public Vector2 TargetPos = new Vector2(120, 100);
  15. public Vector2 RealPosition = Vector2.Zero;
  16.  
  17.  
  18. public override void _Ready()
  19. {
  20. StartPos = GlobalPosition;
  21. RealPosition = StartPos;
  22. }
  23.  
  24. public override void _Process(float delta)
  25. {
  26. if (RealPosition.DistanceSquaredTo(TargetPos) > 1)
  27. {
  28. float v = (StartPos.DistanceTo(RealPosition)) / (StartPos.DistanceTo(TargetPos));
  29. float progress = Mathf.Sin(v * Mathf.Pi);
  30.  
  31. float y = 0;
  32. if (v <= 0.5f)
  33. {
  34. y = Mathf.Lerp(StartHeight, MaxHeight, progress);
  35. }
  36. else
  37. {
  38. y = Mathf.Lerp(TargetHeight, MaxHeight, progress);
  39. }
  40.  
  41. RealPosition = RealPosition.MoveToward(TargetPos, 100 * delta);
  42. GlobalPosition = RealPosition - new Vector2(0, y);
  43. }
  44.  
  45. }
  46.  
  47. }