CrossHair.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. visiable = image.enabled;
  22. UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked);
  23. GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow;
  24. CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load<GameObject>("Prefabs/CrossHairOutBoundChecker1")).GetComponent<CrossHairOutBoundChecker1>();
  25. outBoundChecker.crossHair = transform as RectTransform;
  26. //读取准心值
  27. SetOnlyShow(InfraredDemo._ins.getCrosshairValue() == 1);
  28. }
  29. void Update()
  30. {
  31. if (open) SetVisiable(onlyShow);
  32. }
  33. void OnDestroy()
  34. {
  35. if (Instance == this) Instance = null;
  36. GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
  37. }
  38. void UpdateHideShow(bool mouseAwaked)
  39. {
  40. //transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
  41. SetOnlyShow(!mouseAwaked);
  42. }
  43. void SetVisiable(bool value)
  44. {
  45. if (this.visiable == value) return;
  46. this.visiable = value;
  47. this.image.enabled = this.visiable;
  48. }
  49. public void SetOpen(bool open)
  50. {
  51. this.open = open;
  52. if (!this.open)
  53. {
  54. SetVisiable(false);
  55. }
  56. }
  57. public bool GetOpen()
  58. {
  59. return this.open;
  60. }
  61. public void SetOnlyShow(bool onlyShow)
  62. {
  63. this.onlyShow = onlyShow;
  64. }
  65. public bool GetOnlyShow()
  66. {
  67. return this.onlyShow;
  68. }
  69. Vector2 mapPosition;
  70. float rotateRangeV;
  71. float rotateRangeH;
  72. void InitRotateRangeVH()
  73. {
  74. rotateRangeV = 25f;
  75. float halfScreenHeight = Screen.height * 0.5f;
  76. float halfScreenWidth = Screen.width * 0.5f;
  77. float halfRangeV = rotateRangeV * 0.5f;
  78. float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI);
  79. float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f;
  80. rotateRangeH = halfRangeH * 2f;
  81. }
  82. public void UpdatePositionByModuleRotation(Quaternion rotation)
  83. {
  84. Vector3 resEulerAngles = rotation.eulerAngles;
  85. resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2);
  86. resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2);
  87. mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width;
  88. mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height;
  89. transform.position = mapPosition;
  90. }
  91. public void Shoot()
  92. {
  93. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  94. {
  95. AudioManager.Instance.PlayGunShoot(null);
  96. }
  97. else {
  98. AudioManager.Instance.PlayShoot(null);
  99. }
  100. _PlayShootAnimation();
  101. }
  102. Sequence _shootSequence;
  103. void _PlayShootAnimation()
  104. {
  105. if (_shootSequence != null)
  106. {
  107. _shootSequence.Kill();
  108. _shootSequence = null;
  109. }
  110. _shootSequence = DOTween.Sequence();
  111. _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f));
  112. _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f));
  113. }
  114. }
  115. }