CrossHair.cs 2.9 KB

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