MainPanel.cs 16 KB

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