| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using DG.Tweening;
- namespace DuckHunter
- {
- public class CrossHair : MonoBehaviour
- {
- public static CrossHair Instance;
- void Start()
- {
- Instance = this;
- InitRotateRangeVH();
- UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked);
- GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow;
- CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load<GameObject>("Prefabs/CrossHairOutBoundChecker1")).GetComponent<CrossHairOutBoundChecker1>();
- outBoundChecker.crossHair = transform as RectTransform;
- }
- void OnDestroy()
- {
- if (Instance == this) Instance = null;
- GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
- }
- void UpdateHideShow(bool mouseAwaked)
- {
- transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
- }
- Vector2 mapPosition;
- float rotateRangeV;
- float rotateRangeH;
- void InitRotateRangeVH()
- {
- rotateRangeV = 25f;
- float halfScreenHeight = Screen.height * 0.5f;
- float halfScreenWidth = Screen.width * 0.5f;
- float halfRangeV = rotateRangeV * 0.5f;
- float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI);
- float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f;
- rotateRangeH = halfRangeH * 2f;
- }
- public void UpdatePositionByModuleRotation(Quaternion rotation)
- {
- Vector3 resEulerAngles = rotation.eulerAngles;
- resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2);
- resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2);
- mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width;
- mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height;
- transform.position = mapPosition;
- }
- public void Shoot()
- {
- AudioManager.Instance.PlayShoot(null);
- _PlayShootAnimation();
- }
- Sequence _shootSequence;
- void _PlayShootAnimation()
- {
- if (_shootSequence != null)
- {
- _shootSequence.Kill();
- _shootSequence = null;
- }
- _shootSequence = DOTween.Sequence();
- _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f));
- _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f));
- }
- }
- }
|