CrossHair.cs 4.6 KB

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