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