Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / Cursor.cs
  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 lt;
  19. private Sprite2D lb;
  20. private Sprite2D rt;
  21. private Sprite2D rb;
  22. public override void _Ready()
  23. {
  24. lt = GetNode<Sprite2D>("LT");
  25. lb = GetNode<Sprite2D>("LB");
  26. rt = GetNode<Sprite2D>("RT");
  27. rb = GetNode<Sprite2D>("RB");
  28. }
  29.  
  30. public override void _Process(double delta)
  31. {
  32. if (_isGuiMode)
  33. {
  34. SetScope(0, null);
  35. }
  36. else
  37. {
  38. var targetGun = _mountRole?.Holster.ActiveWeapon;
  39. if (targetGun != null)
  40. {
  41. SetScope(targetGun.CurrScatteringRange, targetGun);
  42. }
  43. else
  44. {
  45. SetScope(0, null);
  46. }
  47. }
  48.  
  49. SetCursorPos();
  50. }
  51.  
  52. /// <summary>
  53. /// 设置是否是Gui模式
  54. /// </summary>
  55. public void SetGuiMode(bool flag)
  56. {
  57. _isGuiMode = flag;
  58. }
  59. /// <summary>
  60. /// 获取是否是Gui模式
  61. /// </summary>
  62. public bool GetGuiMode()
  63. {
  64. return _isGuiMode;
  65. }
  66. /// <summary>
  67. /// 设置非GUI模式下鼠标指针所挂载的角色
  68. /// </summary>
  69. public void SetMountRole(Role role)
  70. {
  71. _mountRole = role;
  72. }
  73.  
  74. /// <summary>
  75. /// 获取非GUI模式下鼠标指针所挂载的角色
  76. /// </summary>
  77. public Role GetMountRole()
  78. {
  79. return _mountRole;
  80. }
  81.  
  82. /// <summary>
  83. /// 设置光标半径范围
  84. /// </summary>
  85. private void SetScope(float scope, Weapon targetGun)
  86. {
  87. if (targetGun != null)
  88. {
  89. var tunPos = GameApplication.Instance.ViewToGlobalPosition(targetGun.GlobalPosition);
  90. var len = GlobalPosition.DistanceTo(tunPos);
  91. if (targetGun.Attribute != null)
  92. {
  93. len = Mathf.Max(0, len - targetGun.Attribute.FirePosition.X);
  94. }
  95. scope = len / GameConfig.ScatteringDistance * scope;
  96. }
  97. lt.Position = new Vector2(-scope, -scope);
  98. lb.Position = new Vector2(-scope, scope);
  99. rt.Position = new Vector2(scope, -scope);
  100. rb.Position = new Vector2(scope, scope);
  101. }
  102.  
  103. private void SetCursorPos()
  104. {
  105. GlobalPosition = GetGlobalMousePosition();
  106. }
  107. }