using System; using UnityEngine; using System.Collections.Generic; namespace SmartBowSDK { /* 瞄准处理器 */ public class AimHandler_SDK : MonoBehaviour { public SmartBowHelper smartBowHelper; private Quaternion _gameRotation = Quaternion.identity; public Quaternion gameRotation { get => _gameRotation; set { _gameRotation = value; //去掉z轴影响 Vector3 normalVector = _gameRotation * Vector3.forward; Quaternion normalQuat = Quaternion.LookRotation(normalVector); //通知外部监听函数 smartBowHelper.InvokeOnRotationUpdate(normalQuat); } } private AxisBaseHandler_SDK m_axisHandler; void Start() { m_axisHandler = new Axis9Handler_SDK(this); m_axisHandler.Init(); } public void ReinitAxisHandler() { m_axisHandler.Init(); } Quaternion newRotation = Quaternion.identity; 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; } newRotation = quat; } public void Update() { gameRotation = Quaternion.Lerp(gameRotation, newRotation, Time.deltaTime * 7); } public void OnDataReceived(byte[] bytes) { if (bytes.Length != 27 && bytes.Length != 39) { if (bytes.Length == 2) { if (bytes[0] == 0x66 && bytes[1] == 0x31) { //短按功能键 smartBowHelper.InvokeOnFunctionKeyPress(); } else if (bytes[0] == 0x66 && bytes[1] == 0x32) { //长按功能键 smartBowHelper.InvokeOnFunctionKeyLongPress(); } else if (bytes[1] == 10) { //电量 smartBowHelper.bluetoothAim.battery = bytes[0]; } } else if (bytes[0] == 0x5b) { //红外线检测到射箭 smartBowHelper.shootChecker.OnInfraredDataReceived(bytes); } else if (bytes[0] == 0x5a) { //手枪应答 smartBowHelper.shootChecker.ReplyGun(bytes); } else if (bytes[0] == 0x5C) { //弹夹变化 smartBowHelper.shootChecker.UpdateTheMagazine(bytes); } else if (bytes[0] == 0x60) { //枪械是否上膛 smartBowHelper.shootChecker.UpdateChamberState(bytes); } else if (bytes[0] == 0x5E) { //接收到系统数据 smartBowHelper.shootChecker.UpdateDeviceAndSysInfo(bytes); } return; } m_axisHandler.Update(bytes); } public void DoIdentity() { try { m_axisHandler.DoIdentity(); } catch (Exception e) { Debug.LogWarning("DoIdentity:" + e.Message); } gameRotation = newRotation; } public void NotifyAxisOnShot() { m_axisHandler.NotifyAxisOnShot(); } public void StartGyrCalibration() { m_axisHandler.StartGyrCalibration(); } public void StopGyrCalibration() { m_axisHandler.StopGyrCalibration(); } public bool IsGyrCalibrating() { return m_axisHandler.IsGyrCalibrating(); } public float GetGyrProgress() { return m_axisHandler.GetGyrProgress(); } public bool IsGyrCompleted() { return m_axisHandler.IsGyrCompleted(); } public void StartMagCalibration() { m_axisHandler.StartMagCalibration(); } public bool IsMagCompleted() { return m_axisHandler.IsMagCompleted(); } public void ResumeCalibrateRecord(string record) { m_axisHandler.ResumeCalibrateRecord(record); } } }