MainPanel.cs 16 KB

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