AimHandler.cs 11 KB

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