| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- 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;
- private bool open = false;
- private bool visiable = false;
- private bool onlyShow = true;
- private Image image = null;
- void Start()
- {
- Instance = this;
- InitRotateRangeVH();
- image = transform.Find("Image").GetComponent<Image>();
- //open = UserSettings.ins.openCrossHair;
- SetOpen(UserSettings.ins.openCrossHair);
- //visiable = image.enabled;
- visiable = open;
- image.enabled = open;
- 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;
- //读取准心值
- if (AimHandler.ins && !AimHandler.ins.bRuning9Axis() && InfraredDemo._ins) SetOnlyShow(InfraredDemo._ins.getCrosshairValue() == 1);
- }
- void Update()
- {
- if (AutoResetView.ins != null) {
- //进入校准时候,一定显示准心
- SetVisiable(true);
- } else {
- if (open) SetVisiable(onlyShow);
- }
-
- }
- void OnDestroy()
- {
- if (Instance == this) Instance = null;
- GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;
- }
- void UpdateHideShow(bool mouseAwaked)
- {
- //transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
- SetOnlyShow(!mouseAwaked);
- }
- void SetVisiable(bool value)
- {
- if (this.visiable == value) return;
- this.visiable = value;
- this.image.enabled = this.visiable;
- }
- public void SetOpen(bool open)
- {
- this.open = open;
- if (!this.open)
- {
- SetVisiable(false);
- }
- }
- public bool GetOpen()
- {
- return this.open;
- }
- public void SetOnlyShow(bool onlyShow)
- {
- this.onlyShow = onlyShow;
- }
- public bool GetOnlyShow()
- {
- return this.onlyShow;
- }
- 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()
- {
- if (GlobalData.MyDeviceMode == DeviceMode.Gun)
- {
- AudioManager.Instance.PlayGunShoot(null);
- }
- else {
- 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));
- }
- }
- }
|