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