CrossHair.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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)
  39. SetVisiable(onlyShow);
  40. else
  41. SetVisiable(false);
  42. }
  43. }
  44. void OnDestroy()
  45. {
  46. if (Instance == this) Instance = null;
  47. GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
  48. }
  49. void UpdateHideShow(bool mouseAwaked)
  50. {
  51. //transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
  52. SetOnlyShow(!mouseAwaked);
  53. }
  54. void SetVisiable(bool value)
  55. {
  56. if (this.visiable == value) return;
  57. this.visiable = value;
  58. this.image.enabled = this.visiable;
  59. }
  60. public void SetOpen(bool open)
  61. {
  62. this.open = open;
  63. if (!this.open)
  64. {
  65. SetVisiable(false);
  66. }
  67. }
  68. public bool GetOpen()
  69. {
  70. return this.open;
  71. }
  72. public void SetOnlyShow(bool onlyShow)
  73. {
  74. this.onlyShow = onlyShow;
  75. }
  76. public bool GetOnlyShow()
  77. {
  78. return this.onlyShow;
  79. }
  80. Vector2 mapPosition;
  81. float rotateRangeV;
  82. float rotateRangeH;
  83. void InitRotateRangeVH()
  84. {
  85. rotateRangeV = 25f;
  86. float halfScreenHeight = Screen.height * 0.5f;
  87. float halfScreenWidth = Screen.width * 0.5f;
  88. float halfRangeV = rotateRangeV * 0.5f;
  89. float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI);
  90. float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f;
  91. rotateRangeH = halfRangeH * 2f;
  92. }
  93. public void UpdatePositionByModuleRotation(Quaternion rotation)
  94. {
  95. Vector3 resEulerAngles = rotation.eulerAngles;
  96. resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2);
  97. resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2);
  98. mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width;
  99. mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height;
  100. transform.position = mapPosition;
  101. }
  102. public void Shoot()
  103. {
  104. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  105. {
  106. AudioManager.Instance.PlayGunShoot(null);
  107. }
  108. else {
  109. AudioManager.Instance.PlayShoot(null);
  110. }
  111. _PlayShootAnimation();
  112. }
  113. Sequence _shootSequence;
  114. void _PlayShootAnimation()
  115. {
  116. if (_shootSequence != null)
  117. {
  118. _shootSequence.Kill();
  119. _shootSequence = null;
  120. }
  121. _shootSequence = DOTween.Sequence();
  122. _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f));
  123. _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f));
  124. }
  125. }
  126. }