using System; using UnityEngine; using System.Collections; using System.Collections.Generic; public enum AimDeviceType { NONE = -1, HOUYI = 0, HOUYI2 = 1, ARTEMIS = 2, } [Serializable]//需要在转换为json格式的类的上方添加序列化 public class AimDeviceInfo { public int id; public int type = -1;//类型 AimDeviceType public int pos; //位置 public AimDeviceInfo(int _id) { this.id = _id; } } [Serializable] public class AimDeviceInfos { public List arry; } /* 瞄准处理器 */ public class AimHandler : MonoBehaviour { CameraToLook m_controlObj { get { return CameraToLook.ins; } } //记录一个设备 public AimDeviceInfo aimDeviceInfo = null; public AimDeviceInfos aimDeviceInfos = new AimDeviceInfos(); public Action aimDeviceInfoChangeEvent; public int DeviceType { get { return 0; } } //陀螺仪校准进度记录 [NonSerialized] public int gyrCalibrateCompleteCount = 0; [NonSerialized] public int gyrCalibrateTotalCount = 2000; public static AimHandler ins; public bool bInitOne = false; void Start() { ins = this; BluetoothDispatcher.aim = OnDataReceived; // StartCoroutine(TestRecord()); } public void OnGetAimDeviceInfos() { aimDeviceInfoChangeEvent?.Invoke(); } public void OnSaveAimDeviceInfos() { aimDeviceInfoChangeEvent?.Invoke(); } //是否存在AimDeviceInfo,存在更新,不存在创建; public void onCreateAimDeviceInfoById(int _id) { OnGetAimDeviceInfos(); //deviceIndex 区分 1p 还是 2p bool bCanAdd = true; foreach (AimDeviceInfo p in aimDeviceInfos.arry) { if (_id == p.id) { aimDeviceInfo = p; bCanAdd = false; } Debug.Log("bCanAdd:"+ bCanAdd+ " , id:" + p.id +" , type:"+ p.type); } if (bCanAdd) { aimDeviceInfo = new AimDeviceInfo(_id); aimDeviceInfos.arry.Add(aimDeviceInfo); } OnSaveAimDeviceInfos(); } public void SetAimDeviceType(AimDeviceType _aimDeviceType) { if (aimDeviceInfo == null) return; aimDeviceInfo.type = (int)_aimDeviceType; for (int i = 0; i < aimDeviceInfos.arry.Count; i++) { if (aimDeviceInfo.id == aimDeviceInfos.arry[i].id) { aimDeviceInfos.arry[i] = aimDeviceInfo; } } OnSaveAimDeviceInfos(); } public void ReinitAxisHandler() { Debug.Log("ReinitAxisHandler"); } // 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) { this.newRotation = quat; } public void SetNewRotation(o0.Geometry.Quaternion o0Quat) { Quaternion quat = o0.Bow.Extension.ToUnityQuaternion(o0Quat); if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w)) { Debug.LogError($"九轴Rotation存在Nan值,double:{o0Quat.ToString()},float:{quat.ToString()}"); return; } if (float.IsInfinity(quat.x) || float.IsInfinity(quat.y) || float.IsInfinity(quat.z) || float.IsInfinity(quat.w)) { Debug.LogError($"九轴Rotation存在Infinity值,double:{o0Quat.ToString()},float:{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 (Input.GetKeyDown(KeyCode.Space)) { AutoResetView.DoIdentity(); } } 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 (bInitOne) { bInitOne = false; AutoResetView.DoIdentity(); } else { 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]); //DeviceView.ins.RenderBattery(1, bytes[0]); } } else if (bytes[0] == 0x5b) { //红外射击检测 ShootCheck.ins.ShootByInfrared(bytes); } return; } 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() { } public void DoIdentity() { if (m_controlObj) m_controlObj.localRotation = newRotation; } }