MainPanel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace WildAttack
  7. {
  8. public class MainPanel : MonoBehaviour
  9. {
  10. #region Members
  11. [SerializeField] Button _btnConnect;
  12. [SerializeField] Text textMacAddress;
  13. [SerializeField] Text textBattery;
  14. [SerializeField] Button _btnCalibrateGyr;
  15. [SerializeField] Button _btnCalibrateMag;
  16. [SerializeField] Button _btnResetAim;
  17. [SerializeField] Button _btnCalibrationOffset;
  18. [SerializeField] Button _btnStartRotationSensor;
  19. [SerializeField] Button _btnStopRotationSensor;
  20. [SerializeField] Button _btnStartShootingSensor;
  21. [SerializeField] Button _btnStopShootingSensor;
  22. [SerializeField] Button _btnChangeShootType;
  23. [SerializeField] InputField _inputFieldGameID;
  24. [SerializeField] InputField _inputFieldChannelID;
  25. [SerializeField] InputField _inputFieldUsername;
  26. [SerializeField] InputField _inputFieldPassword;
  27. [SerializeField] Button _btnLogin;
  28. [SerializeField] Button _btnLogout;
  29. [SerializeField] Button _btnGetUserInfo;
  30. [SerializeField] Image _imgAvatar;
  31. [SerializeField] Text _textNickname;
  32. [SerializeField] Text _textGender;
  33. [SerializeField] Text btnConnectText;
  34. [SerializeField] GameObject _processObj;
  35. [SerializeField] Text _textProcessTitle;
  36. [SerializeField] Text _textProcess;
  37. [SerializeField] Text _textEnemy;
  38. [SerializeField] Text _textHighScore;
  39. [SerializeField] Text _textHpBarTitle;
  40. [SerializeField] Button _btnQuit;
  41. //用户信息
  42. [SerializeField] Image _imgUserAvatar;
  43. [SerializeField] Text _textUserNickname;
  44. [SerializeField] Text _textUserRecordScore;
  45. public Action onQuitCallback;
  46. #endregion
  47. #region Lifecycle
  48. // Start is called before the first frame update
  49. void Start()
  50. {
  51. btnConnectText.text = "<color=blue>未连接</color>(点击连接)";
  52. _textHighScore.text = StringModule.GetInstance().GetData("highScore") + ":";
  53. textBattery.color = new Color(1, 1, 1, 0);
  54. textMacAddress.color = new Color(1, 1, 1, 0);
  55. _textProcessTitle.text = StringModule.GetInstance().GetData("wave") + ":";
  56. Transform wall = GameObject.Find("Wall").transform;
  57. var wallScreenPos = Camera.main.WorldToScreenPoint(wall.position);
  58. RegistEvent();
  59. _btnResetAim.GetComponentInChildren<Text>().text = StringModule.GetInstance().GetData("resetAim");
  60. RenderUserInfo();
  61. }
  62. public void RenderUserInfo()
  63. {
  64. RoleMgr.SetAvatarToImage(
  65. _imgUserAvatar,
  66. LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl
  67. );
  68. _textUserNickname.text = LoginMgr.myUserInfo.nickname + " ";
  69. float recordScore = 0;
  70. if (LoginMgr.myUserInfo.timeLimitGameScores.ContainsKey("WildAttack"))
  71. {
  72. recordScore = LoginMgr.myUserInfo.timeLimitGameScores["WildAttack"];
  73. }
  74. _textUserRecordScore.text = recordScore.ToString();
  75. }
  76. // Update is called once per frame
  77. void Update()
  78. {
  79. UpdateProcess();
  80. ////更新显示陀螺仪校准文本
  81. //UpdateCalibrateGyrText();
  82. ////更新显示地磁计校准文本
  83. //UpdateCalibrateMagText();
  84. }
  85. #endregion
  86. #region ButtonClickEvent
  87. void RegistEvent()
  88. {
  89. // SDK
  90. //SmartBowHelper.GetInstance().OnBluetoothModuleInited += OnBluetoothModuleInited;
  91. //SmartBowHelper.GetInstance().OnBluetoothStatusChanged += OnBluetoothStatusChanged;
  92. // SDK相关btn
  93. //_btnConnect.onClick.AddListener(OnClick_Connect);
  94. //_btnCalibrateGyr.onClick.AddListener(OnClick_CalibrateGyr);
  95. //_btnCalibrateMag.onClick.AddListener(OnClick_CalibrateMag);
  96. if (BluetoothAim.ins && BluetoothAim.ins.isMainConnectToInfraredDevice())
  97. {
  98. _btnResetAim.gameObject.SetActive(false);
  99. //校准
  100. _btnCalibrationOffset.gameObject.SetActive(true);
  101. _btnCalibrationOffset.onClick.AddListener(delegate () {
  102. AudioMgr.ins.PlayBtn();
  103. OnClick_ResetAim();
  104. });
  105. }
  106. else {
  107. _btnResetAim.onClick.AddListener(() => {
  108. if (_btnResetAim.GetComponent<LongPressMonitor>().isLongPress) return;
  109. OnClick_ResetAim();
  110. });
  111. _btnResetAim.gameObject.AddComponent<LongPressMonitor>().onLongPress += () =>
  112. {
  113. AudioMgr.ins?.PlayBtn();
  114. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  115. };
  116. }
  117. //_btnStopRotationSensor.onClick.AddListener(OnClick_StopRotationSensor);
  118. //_btnStopShootingSensor.onClick.AddListener(OnClick_StopShootingSensor);
  119. //_btnLogin.onClick.AddListener(OnClick_Login);
  120. //_btnLogout.onClick.AddListener(OnClick_Logout);
  121. //_btnGetUserInfo.onClick.AddListener(OnClick_GetUserInfo);
  122. _btnQuit.onClick.AddListener(OnClick_Quit);
  123. }
  124. /// <summary>
  125. /// 退出游戏
  126. /// </summary>
  127. private void OnClick_Quit()
  128. {
  129. //#if UNITY_EDITOR
  130. // UnityEditor.EditorApplication.isPlaying = false;
  131. //#else
  132. // Application.Quit();
  133. //#endif
  134. AudioMgr.ins.PlayBtn();
  135. //UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  136. onQuitCallback?.Invoke();
  137. }
  138. //public void OnClick_Connect()
  139. //{
  140. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connecting) return;
  141. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None)
  142. // {
  143. // SmartBowHelper.GetInstance().Connect();
  144. // return;
  145. // }
  146. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connected)
  147. // {
  148. // if (SmartBowHelper.GetInstance().IsBluetoothModuleInited())
  149. // {
  150. // SmartBowHelper.GetInstance().Disconnect();
  151. // return;
  152. // }
  153. // else
  154. // {
  155. // return;
  156. // }
  157. // }
  158. //}
  159. //public void OnClick_CalibrateGyr()
  160. //{
  161. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() != BluetoothStatusEnum.Connected
  162. // || !SmartBowHelper.GetInstance().IsBluetoothModuleInited()) return;
  163. // if (SmartBowHelper.GetInstance().IsGyrCalibrating())
  164. // {
  165. // SmartBowHelper.GetInstance().StopGyrCalibration();
  166. // }
  167. // else
  168. // {
  169. // SmartBowHelper.GetInstance().StartGyrCalibration();
  170. // }
  171. //}
  172. ///// <summary>
  173. ///// 地磁计校准
  174. ///// </summary>
  175. //float _clickCalibrateMagTime = -100;
  176. //public void OnClick_CalibrateMag()
  177. //{
  178. // _clickCalibrateMagTime = Time.realtimeSinceStartup;
  179. // SmartBowHelper.GetInstance().StartMagCalibration();
  180. //}
  181. ////private bool isResetAim = false;
  182. public void OnClick_ResetAim()
  183. {
  184. // todo: 不连接时 归位正中间
  185. // UI改回1280 归位按钮放大
  186. AudioMgr.ins.PlayBtn();
  187. AutoResetView.DoIdentity();
  188. //TipText.Show("OnClick_ResetAim");
  189. //isResetAim = true;
  190. }
  191. //public void OnClick_StopRotationSensor()
  192. //{
  193. // bool success = SmartBowHelper.GetInstance().StopRotationSensor();
  194. // if (!success) TipText.Show("停止九轴传感失败\n原因\n模块未连接或未初始化完成");
  195. // else TipText.Show("停止九轴传感\n指令发送成功");
  196. // // 移除监听
  197. // SmartBowHelper.GetInstance().OnRotationUpdate -= GameMananger.GetInstance().OnRotationUpdate;
  198. //}
  199. //public void OnClick_StopShootingSensor()
  200. //{
  201. // bool success = SmartBowHelper.GetInstance().StopShootingSensor();
  202. // if (!success) TipText.Show("停止射箭传感失败\n原因\n模块未连接或未初始化完成");
  203. // else TipText.Show("停止射箭传感\n指令发送成功");
  204. // // 移除onshooting监听
  205. // SmartBowHelper.GetInstance().OnShooting -= GameMananger.GetInstance().OnShooting;
  206. //}
  207. /// <summary>
  208. /// 切换射击模式
  209. /// </summary>
  210. //private void OnClick_ChangeShootType()
  211. //{
  212. // ShootTypeEnum type = GameMananger.GetInstance().GetShootType();
  213. // if (type == ShootTypeEnum.FixedScreen)
  214. // {
  215. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.FixedCrossHair);
  216. // }
  217. // else if (type == ShootTypeEnum.FixedCrossHair)
  218. // {
  219. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.moreScreen);
  220. // }
  221. // else if (type == ShootTypeEnum.moreScreen)
  222. // {
  223. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.FixedScreen);
  224. // }
  225. // TipText.Show(GameMananger.GetInstance().GetShootType().ToString());
  226. //}
  227. //public void OnClick_Login()
  228. //{
  229. // if (string.IsNullOrWhiteSpace(_inputFieldUsername.text) || string.IsNullOrWhiteSpace(_inputFieldPassword.text))
  230. // {
  231. // TipText.Show("请输入账号密码");
  232. // return;
  233. // }
  234. // SmartBowHelper.GetInstance().Login(
  235. // _inputFieldGameID.text,
  236. // _inputFieldChannelID.text,
  237. // _inputFieldUsername.text,
  238. // _inputFieldPassword.text,
  239. // (res) =>
  240. // {
  241. // if (res.success)
  242. // {
  243. // Debug.Log("登录成功");
  244. // UpdateLoginUI(1);
  245. // }
  246. // TipText.Show(res.message);
  247. // });
  248. //}
  249. //public void OnClick_Logout()
  250. //{
  251. // SmartBowHelper.GetInstance().Logout((success) =>
  252. // {
  253. // if (success)
  254. // {
  255. // TipText.Show($"登出成功");
  256. // UpdateLoginUI(0);
  257. // }
  258. // else TipText.Show($"登出失败");
  259. // });
  260. //}
  261. ///// <summary>
  262. ///// 获取用户信息按钮点击事件
  263. ///// </summary>
  264. //public void OnClick_GetUserInfo()
  265. //{
  266. // SmartBowHelper.GetInstance().GetUserInfo((res) =>
  267. // {
  268. // if (res.success)
  269. // {
  270. // Debug.Log("获取用户信息成功");
  271. // UserInfo userInfo = res.userInfo;
  272. // //渲染UI
  273. // StartCoroutine(SmartBowNetwork.LoadSprite(userInfo.avatarUrl, (sprite) =>
  274. // {
  275. // _imgAvatar.sprite = sprite;
  276. // _imgAvatar.enabled = true;
  277. // }));
  278. // _textNickname.text = userInfo.nickname;
  279. // _textNickname.enabled = true;
  280. // _textGender.text = userInfo.gender == 1 ? "男" : "女";
  281. // _textGender.enabled = true;
  282. // // todo: 更新用户头像,保存最高分 GameOverPanel也用得到
  283. // GameMananger.GetInstance().SetHighScore(0);
  284. // }
  285. // TipText.Show(res.message);
  286. // });
  287. //}
  288. //#endregion
  289. //#region Functions
  290. ///// <summary>
  291. ///// 登录相关文字更新
  292. ///// </summary>
  293. ///// <param name="step"></param>
  294. //void UpdateLoginUI(int step)
  295. //{
  296. // _inputFieldGameID.gameObject.SetActive(step == 0);
  297. // _inputFieldChannelID.gameObject.SetActive(step == 0);
  298. // _inputFieldUsername.gameObject.SetActive(step == 0);
  299. // _inputFieldPassword.gameObject.SetActive(step == 0);
  300. // _btnLogin.gameObject.SetActive(step == 0);
  301. // _btnLogout.gameObject.SetActive(step == 1);
  302. // _btnGetUserInfo.gameObject.SetActive(step == 1);
  303. // _imgAvatar.gameObject.SetActive(step == 1);
  304. // _textNickname.gameObject.SetActive(step == 1);
  305. // _textGender.gameObject.SetActive(step == 1);
  306. // _imgAvatar.enabled = false;
  307. // _imgAvatar.sprite = null;
  308. // _textNickname.enabled = false;
  309. // _textGender.enabled = false;
  310. //}
  311. ///// <summary>
  312. ///// 陀螺仪文字更新
  313. ///// </summary>
  314. //void UpdateCalibrateGyrText()
  315. //{
  316. // Text text = _btnCalibrateGyr.GetComponentInChildren<Text>();
  317. // int progress = (int)(SmartBowHelper.GetInstance().GetGyrProgress() * 100);
  318. // string act = SmartBowHelper.GetInstance().IsGyrCalibrating() ? "停止" : "开始";
  319. // text.text = $"点击{act}陀螺仪校准({progress}%)";
  320. //}
  321. ///// <summary>
  322. ///// 地磁计文字更新
  323. ///// </summary>
  324. //void UpdateCalibrateMagText()
  325. //{
  326. // Text text = _btnCalibrateMag.GetComponentInChildren<Text>();
  327. // if (SmartBowHelper.GetInstance().IsMagCompleted())
  328. // {
  329. // text.text = $"<color=green>校准完成</color>(点击重置)";
  330. // }
  331. // else
  332. // {
  333. // string tip = Time.realtimeSinceStartup - _clickCalibrateMagTime < 2 ? "<color=#FF670D>已重置</color>" : "点击重置";
  334. // text.text = $"<color=blue>自动校准中</color>({tip})";
  335. // }
  336. //}
  337. /// <summary>
  338. /// 进度文字更新
  339. /// </summary>
  340. public void UpdateProcess()
  341. {
  342. if (ProcessManager.GetInstance().CurrLevel <= 0) return;
  343. _textProcess.text = $"{(ProcessManager.GetInstance().CurrLevel > ProcessModule.GetInstance().processDataDic.Count ? ProcessModule.GetInstance().processDataDic.Count : ProcessManager.GetInstance().CurrLevel)}/{ProcessModule.GetInstance().processDataDic.Count}";
  344. }
  345. public void SetProcessInfoActive(bool active)
  346. {
  347. _processObj.gameObject.SetActive(active);
  348. _textEnemy.gameObject.SetActive(active);
  349. }
  350. //private void OnBluetoothModuleInited()
  351. //{
  352. // textBattery.color = new Color(1, 1, 1, 1);
  353. // textMacAddress.color = new Color(1, 1, 1, 1);
  354. // // 初始化完成
  355. // // 真 连接成功 文本表现 见OnBluetoothStatusChanged注释
  356. // if (SmartBowHelper.GetInstance().IsBluetoothModuleInited()) btnConnectText.text = "<color=green>已连接</color>(点击断开)";
  357. // else btnConnectText.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  358. // // 开始get电量和macAdd (在update处理
  359. //}
  360. ///// <summary>
  361. ///// 蓝牙状态变化
  362. ///// </summary>
  363. ///// <param name="oldStatus"></param>
  364. ///// <param name="newStatus"></param>
  365. //void OnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
  366. //{
  367. // if (newStatus == BluetoothStatusEnum.None) btnConnectText.text = "<color=blue>未连接</color>(点击连接)";
  368. // if (newStatus == BluetoothStatusEnum.Connecting) btnConnectText.text = "<color=#FF670D>连接中</color>";
  369. // if (newStatus == BluetoothStatusEnum.Connected) btnConnectText.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  370. //}
  371. #endregion
  372. }
  373. }