Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / role / MountRotation.cs
@小李xl 小李xl on 7 Jun 2023 1 KB 加载武器
  1.  
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 用于限定 Marker2D 节点的旋转角度
  6. /// </summary>
  7. [Tool]
  8. public partial class MountRotation : Marker2D
  9. {
  10. /// <summary>
  11. /// 吸附角度
  12. /// </summary>
  13. private int _adsorption = 6;
  14. /// <summary>
  15. /// 所在的角色
  16. /// </summary>
  17. public Role Master { get; set; }
  18.  
  19. /// <summary>
  20. /// 当前节点真实的旋转角度, 角度制
  21. /// </summary>
  22. public float RealRotationDegrees { get; private set; }
  23. /// <summary>
  24. /// 当前节点真实的旋转角度, 弧度制
  25. /// </summary>
  26. public float RealRotation => Mathf.DegToRad(RealRotationDegrees);
  27.  
  28. /// <summary>
  29. /// 设置看向的目标点
  30. /// </summary>
  31. public void SetLookAt(Vector2 target)
  32. {
  33. var myPos = GlobalPosition;
  34. var angle = Mathf.RadToDeg((target - myPos).Angle());
  35.  
  36. if (Master.Face == FaceDirection.Left)
  37. {
  38. if (angle < 0 && angle > -80)
  39. {
  40. angle = -80;
  41. }
  42. else if (angle >= 0 && angle < 80)
  43. {
  44. angle = 80;
  45. }
  46. }
  47. else
  48. {
  49. angle = Mathf.Clamp(angle, -100, 100);
  50. }
  51.  
  52. RealRotationDegrees = angle;
  53.  
  54. // if (Master.GlobalPosition.X >= target.X)
  55. // {
  56. // angle = -angle;
  57. // }
  58. GlobalRotationDegrees = AdsorptionAngle(angle);
  59. }
  60.  
  61. private float AdsorptionAngle(float angle)
  62. {
  63. return Mathf.Round(angle / _adsorption) * _adsorption;
  64. }
  65. }