MainPanel.cs 16 KB

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