MainPanel.cs 16 KB

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