Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / Cursor.cs
@小李xl 小李xl on 7 Jun 2023 3 KB 加载武器
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 鼠标指针
  5. /// </summary>
  6. public partial class Cursor : Node2D
  7. {
  8. /// <summary>
  9. /// 是否是GUI模式
  10. /// </summary>
  11. private bool _isGuiMode = true;
  12.  
  13. /// <summary>
  14. /// 非GUI模式下鼠标指针所挂载的角色
  15. /// </summary>
  16. private Role _mountRole;
  17.  
  18. private Sprite2D center;
  19. private Sprite2D lt;
  20. private Sprite2D lb;
  21. private Sprite2D rt;
  22. private Sprite2D rb;
  23. private Sprite2D finger;
  24. public override void _Ready()
  25. {
  26. center = GetNode<Sprite2D>("Center");
  27. lt = GetNode<Sprite2D>("LT");
  28. lb = GetNode<Sprite2D>("LB");
  29. rt = GetNode<Sprite2D>("RT");
  30. rb = GetNode<Sprite2D>("RB");
  31. finger = GetNode<Sprite2D>("Finger");
  32. SetGuiMode(true);
  33. }
  34.  
  35. public override void _Process(double delta)
  36. {
  37. if (!_isGuiMode)
  38. {
  39. var targetGun = _mountRole?.Holster.ActiveWeapon;
  40. if (targetGun != null)
  41. {
  42. SetScope(targetGun.CurrScatteringRange, targetGun);
  43. }
  44. else
  45. {
  46. SetScope(0, null);
  47. }
  48. }
  49.  
  50. SetCursorPos();
  51. }
  52.  
  53. /// <summary>
  54. /// 设置是否是Gui模式
  55. /// </summary>
  56. public void SetGuiMode(bool flag)
  57. {
  58. _isGuiMode = flag;
  59. if (flag) //手指
  60. {
  61. lt.Visible = false;
  62. lb.Visible = false;
  63. rt.Visible = false;
  64. rb.Visible = false;
  65. finger.Visible = true;
  66. }
  67. else //准心
  68. {
  69. lt.Visible = true;
  70. lb.Visible = true;
  71. rt.Visible = true;
  72. rb.Visible = true;
  73. finger.Visible = false;
  74. }
  75. }
  76. /// <summary>
  77. /// 获取是否是Gui模式
  78. /// </summary>
  79. public bool GetGuiMode()
  80. {
  81. return _isGuiMode;
  82. }
  83. /// <summary>
  84. /// 设置非GUI模式下鼠标指针所挂载的角色
  85. /// </summary>
  86. public void SetMountRole(Role role)
  87. {
  88. _mountRole = role;
  89. }
  90.  
  91. /// <summary>
  92. /// 获取非GUI模式下鼠标指针所挂载的角色
  93. /// </summary>
  94. public Role GetMountRole()
  95. {
  96. return _mountRole;
  97. }
  98.  
  99. /// <summary>
  100. /// 设置光标半径范围
  101. /// </summary>
  102. private void SetScope(float scope, Weapon targetGun)
  103. {
  104. if (targetGun != null)
  105. {
  106. var tunPos = GameApplication.Instance.ViewToGlobalPosition(targetGun.GlobalPosition);
  107. var len = GlobalPosition.DistanceTo(tunPos);
  108. if (targetGun.Attribute != null)
  109. {
  110. len = Mathf.Max(0, len - targetGun.FirePoint.Position.X);
  111. }
  112. scope = len / GameConfig.ScatteringDistance * scope;
  113. }
  114. scope = Mathf.Clamp(scope, 0, 192);
  115. center.Visible = scope > 64;
  116.  
  117. lt.Position = new Vector2(-scope, -scope);
  118. lb.Position = new Vector2(-scope, scope);
  119. rt.Position = new Vector2(scope, -scope);
  120. rb.Position = new Vector2(scope, scope);
  121. }
  122.  
  123. private void SetCursorPos()
  124. {
  125. GlobalPosition = GetGlobalMousePosition();
  126. }
  127. }