AimHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public enum AimDeviceType {
  6. NONE = -1,
  7. HOUYI = 0,
  8. HOUYI2 = 1,
  9. ARTEMIS = 2,
  10. HOUYIPRO = 3,
  11. //枪械类
  12. Gun = 4,
  13. }
  14. [Serializable]//需要在转换为json格式的类的上方添加序列化
  15. public class AimDeviceInfo
  16. {
  17. public int id;
  18. public int type = -1;//类型 AimDeviceType
  19. public int pos; //位置
  20. public bool bInitMac = false; //是否初始化Mac
  21. public string mac; //记录当前
  22. public AimDeviceInfo(int _id) {
  23. this.id = _id;
  24. }
  25. public void setInitMac(string macTemp) {
  26. bInitMac = true;
  27. mac = macTemp;
  28. }
  29. public void resetInitMac() {
  30. bInitMac = false;
  31. mac = "";
  32. }
  33. }
  34. [Serializable]
  35. public class AimDeviceInfos
  36. {
  37. public List<AimDeviceInfo> arry;
  38. }
  39. /* 瞄准处理器 */
  40. public class AimHandler : MonoBehaviour
  41. {
  42. CameraToLook m_controlObj {
  43. get {
  44. return CameraToLook.ins;
  45. }
  46. }
  47. //记录一个设备
  48. public AimDeviceInfo aimDeviceInfo = null;
  49. public AimDeviceInfo tempAimDeviceInfo = null;//临时处理值。最后赋值给aimDeviceInfo
  50. public AimDeviceInfos aimDeviceInfos = new AimDeviceInfos();
  51. int deviceSelectIndex = 0;//默认选中第一个设备(1p)
  52. public Action aimDeviceInfoChangeEvent;
  53. public int DeviceType
  54. {
  55. get
  56. {
  57. if (m_axisHandler.GetType() == typeof(Axis9Handler)) return 9;
  58. if (m_axisHandler.GetType() == typeof(Axis663Handler)) return 663;
  59. return 0;
  60. }
  61. }
  62. private AxisBaseHandler m_axisHandler;
  63. //陀螺仪校准进度记录
  64. [NonSerialized] public int gyrCalibrateCompleteCount = 0;
  65. [NonSerialized] public int gyrCalibrateTotalCount = 2000;
  66. public static AimHandler ins;
  67. public bool bInitOne = false;
  68. void Start()
  69. {
  70. ins = this;
  71. BluetoothDispatcher.aim = OnDataReceived;
  72. // m_axisHandler = new Axis9NopackHandler(this); //九轴旧
  73. m_axisHandler = new Axis9Handler(this); //九轴新
  74. // m_axisHandler = new Axis663Handler(this); //双陀螺仪
  75. m_axisHandler.Init();
  76. // StartCoroutine(TestRecord());
  77. }
  78. public void onClearAimDeviceInfosNew()
  79. {
  80. PlayerPrefs.DeleteKey("aim-device-info-" + LoginMgr.myUserInfo.id);
  81. aimDeviceInfos.arry.Clear();
  82. }
  83. public void OnGetAimDeviceInfos() {
  84. string deviceInfo = PlayerPrefs.GetString("aim-device-info-" + LoginMgr.myUserInfo.id, "");
  85. Debug.Log("get deviceSelectIndex:" + deviceInfo);
  86. if (deviceInfo != "")
  87. {
  88. aimDeviceInfos = JsonUtility.FromJson<AimDeviceInfos>(deviceInfo);//这里的类是依据最外层{}决定的
  89. }
  90. aimDeviceInfoChangeEvent?.Invoke();
  91. }
  92. public void OnSaveAimDeviceInfos()
  93. {
  94. aimDeviceInfoChangeEvent?.Invoke();
  95. PlayerPrefs.SetString("aim-device-info-" + LoginMgr.myUserInfo.id, JsonUtility.ToJson(aimDeviceInfos));
  96. }
  97. //创建一个选择值
  98. public void onCreateTempAimDeviceInfo() {
  99. tempAimDeviceInfo = new AimDeviceInfo(deviceSelectIndex);
  100. Debug.Log("onCreateTempAimDeviceInfo deviceSelectIndex:" + deviceSelectIndex);
  101. }
  102. //是否存在AimDeviceInfo,存在更新,不存在创建;
  103. public void onCreateAimDeviceInfoById()
  104. {
  105. OnGetAimDeviceInfos();
  106. //deviceIndex 区分 1p 还是 2p
  107. bool bCanAdd = true;
  108. foreach (AimDeviceInfo p in aimDeviceInfos.arry)
  109. {
  110. if (deviceSelectIndex == p.id)
  111. {
  112. aimDeviceInfo = p;
  113. bCanAdd = false;
  114. }
  115. //Debug.Log("33deviceSelectIndex:" + deviceSelectIndex + ",bCanAdd:" + bCanAdd+ " , id:" + p.id +" , type:"+ p.type);
  116. }
  117. if (bCanAdd)
  118. {
  119. Debug.Log("add deviceSelectIndex:" + deviceSelectIndex);
  120. aimDeviceInfo = new AimDeviceInfo(deviceSelectIndex);
  121. aimDeviceInfos.arry.Add(aimDeviceInfo);
  122. }
  123. OnSaveAimDeviceInfos();
  124. }
  125. public void SetAimDeviceSelectIndex(int selectIndex) {
  126. if (selectIndex < 0) {
  127. Debug.LogWarning("selectIndex不能小于0:"+ selectIndex);
  128. return;
  129. }
  130. deviceSelectIndex = selectIndex;
  131. Debug.Log("SetAimDeviceSelectIndex :" + selectIndex);
  132. }
  133. public void SetTempAimDeviceType(AimDeviceType _aimDeviceType)
  134. {
  135. if (tempAimDeviceInfo == null) return;
  136. tempAimDeviceInfo.type = (int)_aimDeviceType;
  137. }
  138. public void SetAimDeviceType(AimDeviceType _aimDeviceType)
  139. {
  140. if (aimDeviceInfo == null) return;
  141. aimDeviceInfo.type = (int)_aimDeviceType;
  142. _AimDeviceInfosUpdate();
  143. OnSaveAimDeviceInfos();
  144. }
  145. public void SetAimDeviceType(int _aimDeviceType)
  146. {
  147. if (aimDeviceInfo == null) return;
  148. aimDeviceInfo.type = _aimDeviceType;
  149. _AimDeviceInfosUpdate();
  150. OnSaveAimDeviceInfos();
  151. }
  152. //APP连接需进行MAC地址的比对。
  153. //只有再引导初始化页面才保存连接使用的mac地址。
  154. public void SetAimDeviceMac(string _mac) {
  155. if (aimDeviceInfo == null) return;
  156. aimDeviceInfo.setInitMac(_mac);
  157. _AimDeviceInfosUpdate();
  158. OnSaveAimDeviceInfos();
  159. }
  160. //重置mac存储信息
  161. public void ResetAimDeviceMac() {
  162. if (aimDeviceInfo == null) return;
  163. aimDeviceInfo.resetInitMac();
  164. _AimDeviceInfosUpdate();
  165. OnSaveAimDeviceInfos();
  166. }
  167. void _AimDeviceInfosUpdate() {
  168. for (int i = 0; i < aimDeviceInfos.arry.Count; i++)
  169. {
  170. if (aimDeviceInfo.id == aimDeviceInfos.arry[i].id)
  171. {
  172. aimDeviceInfos.arry[i] = aimDeviceInfo;
  173. }
  174. }
  175. }
  176. #region 根据设备1p或者2p 获取HOUYIPRO的状态
  177. public bool isHOUYIPRO(BluetoothPlayer bluetoothPlayer)
  178. {
  179. bool _isHOUYIPRO = false;
  180. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  181. {
  182. if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.HOUYIPRO)
  183. {
  184. _isHOUYIPRO = true;
  185. }
  186. }
  187. return _isHOUYIPRO;
  188. }
  189. #endregion
  190. public void ReinitAxisHandler()
  191. {
  192. Debug.Log("ReinitAxisHandler");
  193. m_axisHandler.Init();
  194. }
  195. // System.Collections.IEnumerator TestRecord() {
  196. // while (LoginMgr.myUserInfo.id == 0) yield return null;
  197. // UserComp.Instance.saveMac();
  198. // }
  199. [NonSerialized] private Quaternion newRotation = Quaternion.identity;
  200. public void SetNewRotation(Quaternion quat) {
  201. this.newRotation = quat;
  202. }
  203. public void SetNewRotation(o0.Geometry.Quaternion o0Quat) {
  204. Quaternion quat = o0.Bow.Extension.ToUnityQuaternion(o0Quat);
  205. if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w)) {
  206. Debug.LogError($"九轴Rotation存在Nan值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  207. return;
  208. }
  209. if (float.IsInfinity(quat.x) || float.IsInfinity(quat.y) || float.IsInfinity(quat.z) || float.IsInfinity(quat.w)) {
  210. Debug.LogError($"九轴Rotation存在Infinity值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  211. return;
  212. }
  213. this.newRotation = quat;
  214. }
  215. [NonSerialized] public bool lerpForRotation = true;
  216. [NonSerialized] public float lerpTimeRate = 7;
  217. public void Update()
  218. {
  219. //&& !InfraredDemo.running
  220. if (m_controlObj && !m_ban9AxisCalculate && bRuning9Axis())
  221. {
  222. if (lerpForRotation)
  223. m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate);
  224. else
  225. m_controlObj.localRotation = newRotation;
  226. }
  227. if (IsMagCompleted())
  228. {
  229. if (!m_magCompleted)
  230. {
  231. StartCoroutine(SaveMag());
  232. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_success"));
  233. }
  234. m_magCompleted = true;
  235. } else {
  236. m_magCompleted = false;
  237. }
  238. if (Input.GetKeyDown(KeyCode.Space))
  239. {
  240. AutoResetView.DoIdentity();
  241. }
  242. }
  243. public void OnDataReceived(byte[] bytes)
  244. {
  245. // Debug.Log("瞄准模块数据长度" + bytes.Length);
  246. if (bytes.Length != 27 && bytes.Length != 39)
  247. {
  248. if (bytes.Length == 2) {
  249. if (bytes[0] == 0x66 && bytes[1] == 0x31) {
  250. if (bInitOne)
  251. {
  252. bInitOne = false;
  253. AutoResetView.DoIdentity();
  254. }
  255. else {
  256. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  257. {
  258. //视角回正
  259. DoIdentity();
  260. //鼠标居中自然会居中
  261. }
  262. else
  263. {
  264. AutoResetView.DoIdentity();
  265. }
  266. }
  267. } else if (bytes[0] == 0x66 && bytes[1] == 0x32) {
  268. if (SB_EventSystem.ins) {
  269. // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
  270. //唤起/隐藏虚拟鼠标
  271. SB_EventSystem.ins.AwakenSimulateMouse();
  272. }
  273. } else if (bytes[1] == 10) {
  274. //显示电量
  275. DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
  276. //DeviceView.ins.RenderBattery(1, bytes[0]);
  277. }
  278. } else if (bytes[0] == 0x5b) {
  279. //红外射击检测
  280. ShootCheck.ins.ShootByInfrared(bytes);
  281. }
  282. return;
  283. }
  284. //if (InfraredDemo.running)
  285. //{
  286. // BluetoothAim.ins.WriteData("4"); //关闭九轴
  287. // return;
  288. //}
  289. if (!bRuning9Axis())
  290. {
  291. return;
  292. }
  293. m_axisHandler.Update(bytes);
  294. if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles);
  295. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(newRotation);
  296. }
  297. private bool m_magCompleted;
  298. public void CorrectMagCompleted(bool value) { m_magCompleted = value; }
  299. private bool m_ban9AxisCalculate = false;
  300. public void Ban9AxisCalculate(bool ban)
  301. {
  302. // m_ban9AxisCalculate = ban;
  303. // if (!ban)
  304. // {
  305. // SetMsOldDefault();
  306. // if (m_controlObj) m_controlObj.localRotation = newRotation;
  307. // }
  308. }
  309. public void SetMsOldDefault()
  310. {
  311. if (m_axisHandler.GetType() == typeof(Axis9NopackHandler))
  312. (m_axisHandler as Axis9NopackHandler).msOld = default;
  313. }
  314. public void DoIdentity()
  315. {
  316. //if (InfraredDemo.running) return;
  317. m_axisHandler.DoIdentity();
  318. if (!bRuning9Axis()) return;
  319. if (m_controlObj) m_controlObj.localRotation = newRotation;
  320. }
  321. public void NotifyAxisOnShot() { m_axisHandler.NotifyAxisOnShot(); }
  322. public void CalibrateGyr(bool calibration) { m_axisHandler.CalibrateGyr(calibration); }
  323. public void InitGyr(string record) { m_axisHandler.InitGyr(record); }
  324. public void InitMag(string record) { m_axisHandler.InitMag(record); }
  325. public void ResetGyr() { m_axisHandler.ResetGyr(); }
  326. public void ResetMag() { m_axisHandler.ResetMag(); }
  327. public void ApplyImpreciseMag() { m_axisHandler.ApplyImpreciseMag(); }
  328. public bool IsGyrCompleted() { return m_axisHandler.IsGyrCompleted(); }
  329. public bool IsMagCompleted() { return m_axisHandler.IsMagCompleted(); }
  330. public IEnumerator SaveGyr() { return m_axisHandler.SaveGyr(); }
  331. public IEnumerator SaveMag() { return m_axisHandler.SaveMag(); }
  332. public void ResumeCalibrateRecord(string record) { m_axisHandler.ResumeCalibrateRecord(record); }
  333. #region 获取一个连接的设备下,对应的 Archery 是运行9轴的代码处理位置信息判断
  334. public bool bRuning9Axis() {
  335. bool bRuning = false;
  336. //连接的设备下,对应的 Archery 是运行9轴的代码处理位置信息判断
  337. bRuning = GlobalData.MyDeviceMode == DeviceMode.Archery;
  338. return bRuning;
  339. }
  340. #endregion
  341. }