MainPanel.cs 20 KB

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