CrossHair.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using AppUI.Bluetooth;
  2. using DG.Tweening;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace DuckHunter
  8. {
  9. public class CrossHair : MonoBehaviour
  10. {
  11. public static CrossHair Instance;
  12. private bool open = false;
  13. private bool visiable = false;
  14. private bool onlyShow = true;
  15. private Image image = null;
  16. void Start()
  17. {
  18. Instance = this;
  19. InitRotateRangeVH();
  20. image = transform.Find("Image").GetComponent<Image>();
  21. //open = UserSettings.ins.openCrossHair;
  22. SetOpen(UserSettings.ins.openCrossHair);
  23. //visiable = image.enabled;
  24. visiable = open;
  25. image.enabled = open;
  26. UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked);
  27. GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow;
  28. CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load<GameObject>("Prefabs/CrossHairOutBoundChecker1")).GetComponent<CrossHairOutBoundChecker1>();
  29. outBoundChecker.crossHair = transform as RectTransform;
  30. //读取准心值
  31. if (SmartBowDeviceHub.ins?.Aim?.bRuning9Axis() == true && InfraredDemo._ins) SetOnlyShow(InfraredDemo._ins.getCrosshairValue() == 1);
  32. }
  33. void Update()
  34. {
  35. if (AutoResetView.ins != null) {
  36. //进入校准时候,一定显示准心
  37. SetVisiable(true);
  38. } else {
  39. if (open)
  40. SetVisiable(onlyShow);
  41. else
  42. SetVisiable(false);
  43. }
  44. }
  45. void OnDestroy()
  46. {
  47. if (Instance == this) Instance = null;
  48. GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
  49. }
  50. void UpdateHideShow(bool mouseAwaked)
  51. {
  52. //transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
  53. SetOnlyShow(!mouseAwaked);
  54. }
  55. void SetVisiable(bool value)
  56. {
  57. if (this.visiable == value) return;
  58. this.visiable = value;
  59. this.image.enabled = this.visiable;
  60. }
  61. public void SetOpen(bool open)
  62. {
  63. this.open = open;
  64. if (!this.open)
  65. {
  66. SetVisiable(false);
  67. }
  68. }
  69. public bool GetOpen()
  70. {
  71. return this.open;
  72. }
  73. public void SetOnlyShow(bool onlyShow)
  74. {
  75. this.onlyShow = onlyShow;
  76. }
  77. public bool GetOnlyShow()
  78. {
  79. return this.onlyShow;
  80. }
  81. Vector2 mapPosition;
  82. float rotateRangeV;
  83. float rotateRangeH;
  84. void InitRotateRangeVH()
  85. {
  86. rotateRangeV = 25f;
  87. float halfScreenHeight = Screen.height * 0.5f;
  88. float halfScreenWidth = Screen.width * 0.5f;
  89. float halfRangeV = rotateRangeV * 0.5f;
  90. float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI);
  91. float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f;
  92. rotateRangeH = halfRangeH * 2f;
  93. }
  94. public void UpdatePositionByModuleRotation(Quaternion rotation)
  95. {
  96. Vector3 resEulerAngles = rotation.eulerAngles;
  97. resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2);
  98. resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2);
  99. mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width;
  100. mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height;
  101. transform.position = mapPosition;
  102. }
  103. public void Shoot()
  104. {
  105. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  106. {
  107. AudioManager.Instance.PlayGunShoot(null);
  108. }
  109. else {
  110. AudioManager.Instance.PlayShoot(null);
  111. }
  112. _PlayShootAnimation();
  113. }
  114. Sequence _shootSequence;
  115. void _PlayShootAnimation()
  116. {
  117. if (_shootSequence != null)
  118. {
  119. _shootSequence.Kill();
  120. _shootSequence = null;
  121. }
  122. _shootSequence = DOTween.Sequence();
  123. _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f));
  124. _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f));
  125. }
  126. }
  127. }