using System; using UnityEngine; using System.Collections; /* 瞄准处理器 */ public class AimHandler : MonoBehaviour { Transform m_controlObj { get { if (CameraToLook.ins) { return CameraToLook.ins.transform; } return null; } } public int DeviceType { get { if (m_axisHandler.GetType() == typeof(Axis9Handler)) return 9; if (m_axisHandler.GetType() == typeof(Axis663Handler)) return 663; return 0; } } private AxisBaseHandler m_axisHandler; //陀螺仪校准进度记录 [NonSerialized] public int gyrCalibrateCompleteCount = 0; [NonSerialized] public int gyrCalibrateTotalCount = 2000; public static AimHandler ins; void Start() { ins = this; BluetoothDispatcher.aim = OnDataReceived; // m_axisHandler = new Axis9NopackHandler(this); //九轴旧 m_axisHandler = new Axis9Handler(this); //九轴新 // m_axisHandler = new Axis663Handler(this); //双陀螺仪 m_axisHandler.Init(); // StartCoroutine(TestRecord()); } // System.Collections.IEnumerator TestRecord() { // while (LoginMgr.myUserInfo.id == 0) yield return null; // UserComp.Instance.saveMac(); // } [NonSerialized] private Quaternion newRotation = Quaternion.identity; public void SetNewRotation(Quaternion quat) { if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w)) { Debug.LogWarning("九轴算法返回的Rotation存在Nan值:" + quat.ToString()); return; } this.newRotation = quat; } [NonSerialized] public bool lerpForRotation = true; [NonSerialized] public float lerpTimeRate = 7; public void Update() { if (m_controlObj && !m_ban9AxisCalculate) { if (lerpForRotation) m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate); else m_controlObj.localRotation = newRotation; } if (IsMagCompleted()) { if (!m_magCompleted) { StartCoroutine(SaveMag()); PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_success")); } m_magCompleted = true; } else { m_magCompleted = false; } } public void OnDataReceived(byte[] bytes) { // Debug.Log("瞄准模块数据长度" + bytes.Length); if (bytes.Length != 27 && bytes.Length != 39) { if (bytes.Length == 2) { if (bytes[0] == 0x66 && bytes[1] == 0x31) { if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) { //视角回正 DoIdentity(); //鼠标居中自然会居中 } else { AutoResetView.DoIdentity(); } } else if (bytes[0] == 0x66 && bytes[1] == 0x32) { if (SB_EventSystem.ins) { // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) { //唤起/隐藏虚拟鼠标 SB_EventSystem.ins.AwakenSimulateMouse(); } } else if (bytes[1] == 10) { //显示电量 DeviceBatteryView.ins.RenderBattery(1, bytes[0]); } } else if (bytes[0] == 0x5b) { //红外射击检测 ShootCheck.ins.ShootByInfrared(bytes); } return; } m_axisHandler.Update(bytes); if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles); if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(newRotation); } private bool m_magCompleted; public void CorrectMagCompleted(bool value) { m_magCompleted = value; } private bool m_ban9AxisCalculate = false; public void Ban9AxisCalculate(bool ban) { m_ban9AxisCalculate = ban; if (!ban) { SetMsOldDefault(); if (m_controlObj) m_controlObj.localRotation = newRotation; } } public void SetMsOldDefault() { if (m_axisHandler.GetType() == typeof(Axis9NopackHandler)) (m_axisHandler as Axis9NopackHandler).msOld = default; } public void DoIdentity() { m_axisHandler.DoIdentity(); if (m_controlObj) m_controlObj.localRotation = newRotation; } public void NotifyAxisOnShot() { m_axisHandler.NotifyAxisOnShot(); } public void CalibrateGyr(bool calibration) { m_axisHandler.CalibrateGyr(calibration); } public void InitGyr(string record) { m_axisHandler.InitGyr(record); } public void InitMag(string record) { m_axisHandler.InitMag(record); } public void ResetGyr() { m_axisHandler.ResetGyr(); } public void ResetMag() { m_axisHandler.ResetMag(); } public bool IsGyrCompleted() { return m_axisHandler.IsGyrCompleted(); } public bool IsMagCompleted() { return m_axisHandler.IsMagCompleted(); } public IEnumerator SaveGyr() { return m_axisHandler.SaveGyr(); } public IEnumerator SaveMag() { return m_axisHandler.SaveMag(); } public void ResumeCalibrateRecord(string record) { m_axisHandler.ResumeCalibrateRecord(record); } }