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