CrossHair.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }
  18. void OnDestroy()
  19. {
  20. if (Instance == this) Instance = null;
  21. GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
  22. }
  23. void UpdateHideShow(bool mouseAwaked)
  24. {
  25. transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
  26. }
  27. Vector2 mapPosition;
  28. float rotateRangeV;
  29. float rotateRangeH;
  30. void InitRotateRangeVH()
  31. {
  32. rotateRangeV = 25f;
  33. float halfScreenHeight = Screen.height * 0.5f;
  34. float halfScreenWidth = Screen.width * 0.5f;
  35. float halfRangeV = rotateRangeV * 0.5f;
  36. float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI);
  37. float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f;
  38. rotateRangeH = halfRangeH * 2f;
  39. }
  40. public void UpdatePositionByModuleRotation(Quaternion rotation)
  41. {
  42. Vector3 resEulerAngles = rotation.eulerAngles;
  43. resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2);
  44. resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2);
  45. mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width;
  46. mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height;
  47. transform.position = mapPosition;
  48. }
  49. public void Shoot()
  50. {
  51. AudioManager.Instance.PlayShoot(null);
  52. _PlayShootAnimation();
  53. }
  54. Sequence _shootSequence;
  55. void _PlayShootAnimation()
  56. {
  57. if (_shootSequence != null)
  58. {
  59. _shootSequence.Kill();
  60. _shootSequence = null;
  61. }
  62. _shootSequence = DOTween.Sequence();
  63. _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f));
  64. _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f));
  65. }
  66. }
  67. }