CrossHair.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. void Start()
  12. {
  13. Instance = this;
  14. InitRotateRangeVH();
  15. UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked);
  16. GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow;
  17. CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load<GameObject>("Prefabs/CrossHairOutBoundChecker1")).GetComponent<CrossHairOutBoundChecker1>();
  18. outBoundChecker.crossHair = transform as RectTransform;
  19. if (InfraredManager.ConnetDevicesSingle.ins)
  20. InfraredManager.ConnetDevicesSingle.ins.posAction += UpdatePosition;
  21. }
  22. void OnDestroy()
  23. {
  24. if (Instance == this) Instance = null;
  25. GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
  26. if (InfraredManager.ConnetDevicesSingle.ins)
  27. InfraredManager.ConnetDevicesSingle.ins.posAction -= UpdatePosition;
  28. }
  29. void UpdatePosition(Vector3 pos) {
  30. transform.position = pos;
  31. }
  32. void UpdateShow(bool show)
  33. {
  34. transform.Find("Image").GetComponent<Image>().enabled = show;
  35. }
  36. void UpdateHideShow(bool mouseAwaked)
  37. {
  38. transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
  39. }
  40. Vector2 mapPosition;
  41. float rotateRangeV;
  42. float rotateRangeH;
  43. void InitRotateRangeVH()
  44. {
  45. rotateRangeV = 25f;
  46. float halfScreenHeight = Screen.height * 0.5f;
  47. float halfScreenWidth = Screen.width * 0.5f;
  48. float halfRangeV = rotateRangeV * 0.5f;
  49. float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI);
  50. float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f;
  51. rotateRangeH = halfRangeH * 2f;
  52. }
  53. public void UpdatePositionByModuleRotation(Quaternion rotation)
  54. {
  55. Vector3 resEulerAngles = rotation.eulerAngles;
  56. resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2);
  57. resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2);
  58. mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width;
  59. mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height;
  60. transform.position = mapPosition;
  61. }
  62. public void Shoot()
  63. {
  64. AudioManager.Instance.PlayShoot(null);
  65. _PlayShootAnimation();
  66. }
  67. Sequence _shootSequence;
  68. void _PlayShootAnimation()
  69. {
  70. if (_shootSequence != null)
  71. {
  72. _shootSequence.Kill();
  73. _shootSequence = null;
  74. }
  75. _shootSequence = DOTween.Sequence();
  76. _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f));
  77. _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f));
  78. }
  79. }
  80. }