MainPanel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 (CommonConfig.bDisplayTwoPlayerGames && _btnCalibrationOffset_2P) {
  170. _btnCalibrationOffset_2P.gameObject.SetActive(true);
  171. _btnCalibrationOffset_2P.onClick.AddListener(delegate ()
  172. {
  173. AudioMgr.ins.PlayBtn();
  174. OnClick_ResetAim_2P();
  175. });
  176. }
  177. }
  178. else
  179. {
  180. _btnResetAim.onClick.AddListener(() =>
  181. {
  182. if (_btnResetAim.GetComponent<LongPressMonitor>().isLongPress) return;
  183. OnClick_ResetAim();
  184. });
  185. _btnResetAim.gameObject.AddComponent<LongPressMonitor>().onLongPress += () =>
  186. {
  187. AudioMgr.ins?.PlayBtn();
  188. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  189. };
  190. }
  191. }
  192. //_btnStopRotationSensor.onClick.AddListener(OnClick_StopRotationSensor);
  193. //_btnStopShootingSensor.onClick.AddListener(OnClick_StopShootingSensor);
  194. //_btnLogin.onClick.AddListener(OnClick_Login);
  195. //_btnLogout.onClick.AddListener(OnClick_Logout);
  196. //_btnGetUserInfo.onClick.AddListener(OnClick_GetUserInfo);
  197. _btnQuit.onClick.AddListener(OnClick_Quit);
  198. }
  199. /// <summary>
  200. /// 退出游戏
  201. /// </summary>
  202. private void OnClick_Quit()
  203. {
  204. //#if UNITY_EDITOR
  205. // UnityEditor.EditorApplication.isPlaying = false;
  206. //#else
  207. // Application.Quit();
  208. //#endif
  209. AudioMgr.ins.PlayBtn();
  210. //UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  211. onQuitCallback?.Invoke();
  212. }
  213. //public void OnClick_Connect()
  214. //{
  215. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connecting) return;
  216. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None)
  217. // {
  218. // SmartBowHelper.GetInstance().Connect();
  219. // return;
  220. // }
  221. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connected)
  222. // {
  223. // if (SmartBowHelper.GetInstance().IsBluetoothModuleInited())
  224. // {
  225. // SmartBowHelper.GetInstance().Disconnect();
  226. // return;
  227. // }
  228. // else
  229. // {
  230. // return;
  231. // }
  232. // }
  233. //}
  234. //public void OnClick_CalibrateGyr()
  235. //{
  236. // if (SmartBowHelper.GetInstance().GetBluetoothStatus() != BluetoothStatusEnum.Connected
  237. // || !SmartBowHelper.GetInstance().IsBluetoothModuleInited()) return;
  238. // if (SmartBowHelper.GetInstance().IsGyrCalibrating())
  239. // {
  240. // SmartBowHelper.GetInstance().StopGyrCalibration();
  241. // }
  242. // else
  243. // {
  244. // SmartBowHelper.GetInstance().StartGyrCalibration();
  245. // }
  246. //}
  247. ///// <summary>
  248. ///// 地磁计校准
  249. ///// </summary>
  250. //float _clickCalibrateMagTime = -100;
  251. //public void OnClick_CalibrateMag()
  252. //{
  253. // _clickCalibrateMagTime = Time.realtimeSinceStartup;
  254. // SmartBowHelper.GetInstance().StartMagCalibration();
  255. //}
  256. ////private bool isResetAim = false;
  257. public void OnClick_ResetAim()
  258. {
  259. // todo: 不连接时 归位正中间
  260. // UI改回1280 归位按钮放大
  261. AudioMgr.ins.PlayBtn();
  262. AutoResetView.DoIdentity();
  263. //TipText.Show("OnClick_ResetAim");
  264. //isResetAim = true;
  265. }
  266. public void OnClick_ResetAim_2P()
  267. {
  268. AudioMgr.ins.PlayBtn();
  269. //GameMananger.GetInstance().ResetAim(PlayerTypeEnum.Second);
  270. if (GameObject.Find("AutoResetViewNewRight")) return;
  271. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  272. resetView.name = "AutoResetViewNewRight";
  273. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  274. autoResetViewNewScript.setTextKey("new-user-guider_tip_视角归位-瞄准-infraredD");
  275. autoResetViewNewScript.setPosRight();
  276. autoResetViewNewScript.action_OnDestroy += () =>
  277. {
  278. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "WildAttackDouble")
  279. {
  280. GameMananger.GetInstance().ResetAim(PlayerTypeEnum.Second);
  281. }
  282. };
  283. }
  284. //public void OnClick_StopRotationSensor()
  285. //{
  286. // bool success = SmartBowHelper.GetInstance().StopRotationSensor();
  287. // if (!success) TipText.Show("停止九轴传感失败\n原因\n模块未连接或未初始化完成");
  288. // else TipText.Show("停止九轴传感\n指令发送成功");
  289. // // 移除监听
  290. // SmartBowHelper.GetInstance().OnRotationUpdate -= GameMananger.GetInstance().OnRotationUpdate;
  291. //}
  292. //public void OnClick_StopShootingSensor()
  293. //{
  294. // bool success = SmartBowHelper.GetInstance().StopShootingSensor();
  295. // if (!success) TipText.Show("停止射箭传感失败\n原因\n模块未连接或未初始化完成");
  296. // else TipText.Show("停止射箭传感\n指令发送成功");
  297. // // 移除onshooting监听
  298. // SmartBowHelper.GetInstance().OnShooting -= GameMananger.GetInstance().OnShooting;
  299. //}
  300. /// <summary>
  301. /// 切换射击模式
  302. /// </summary>
  303. //private void OnClick_ChangeShootType()
  304. //{
  305. // ShootTypeEnum type = GameMananger.GetInstance().GetShootType();
  306. // if (type == ShootTypeEnum.FixedScreen)
  307. // {
  308. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.FixedCrossHair);
  309. // }
  310. // else if (type == ShootTypeEnum.FixedCrossHair)
  311. // {
  312. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.moreScreen);
  313. // }
  314. // else if (type == ShootTypeEnum.moreScreen)
  315. // {
  316. // GameMananger.GetInstance().SetShootType(ShootTypeEnum.FixedScreen);
  317. // }
  318. // TipText.Show(GameMananger.GetInstance().GetShootType().ToString());
  319. //}
  320. //public void OnClick_Login()
  321. //{
  322. // if (string.IsNullOrWhiteSpace(_inputFieldUsername.text) || string.IsNullOrWhiteSpace(_inputFieldPassword.text))
  323. // {
  324. // TipText.Show("请输入账号密码");
  325. // return;
  326. // }
  327. // SmartBowHelper.GetInstance().Login(
  328. // _inputFieldGameID.text,
  329. // _inputFieldChannelID.text,
  330. // _inputFieldUsername.text,
  331. // _inputFieldPassword.text,
  332. // (res) =>
  333. // {
  334. // if (res.success)
  335. // {
  336. // Debug.Log("登录成功");
  337. // UpdateLoginUI(1);
  338. // }
  339. // TipText.Show(res.message);
  340. // });
  341. //}
  342. //public void OnClick_Logout()
  343. //{
  344. // SmartBowHelper.GetInstance().Logout((success) =>
  345. // {
  346. // if (success)
  347. // {
  348. // TipText.Show($"登出成功");
  349. // UpdateLoginUI(0);
  350. // }
  351. // else TipText.Show($"登出失败");
  352. // });
  353. //}
  354. ///// <summary>
  355. ///// 获取用户信息按钮点击事件
  356. ///// </summary>
  357. //public void OnClick_GetUserInfo()
  358. //{
  359. // SmartBowHelper.GetInstance().GetUserInfo((res) =>
  360. // {
  361. // if (res.success)
  362. // {
  363. // Debug.Log("获取用户信息成功");
  364. // UserInfo userInfo = res.userInfo;
  365. // //渲染UI
  366. // StartCoroutine(SmartBowNetwork.LoadSprite(userInfo.avatarUrl, (sprite) =>
  367. // {
  368. // _imgAvatar.sprite = sprite;
  369. // _imgAvatar.enabled = true;
  370. // }));
  371. // _textNickname.text = userInfo.nickname;
  372. // _textNickname.enabled = true;
  373. // _textGender.text = userInfo.gender == 1 ? "男" : "女";
  374. // _textGender.enabled = true;
  375. // // todo: 更新用户头像,保存最高分 GameOverPanel也用得到
  376. // GameMananger.GetInstance().SetHighScore(0);
  377. // }
  378. // TipText.Show(res.message);
  379. // });
  380. //}
  381. //#endregion
  382. //#region Functions
  383. ///// <summary>
  384. ///// 登录相关文字更新
  385. ///// </summary>
  386. ///// <param name="step"></param>
  387. //void UpdateLoginUI(int step)
  388. //{
  389. // _inputFieldGameID.gameObject.SetActive(step == 0);
  390. // _inputFieldChannelID.gameObject.SetActive(step == 0);
  391. // _inputFieldUsername.gameObject.SetActive(step == 0);
  392. // _inputFieldPassword.gameObject.SetActive(step == 0);
  393. // _btnLogin.gameObject.SetActive(step == 0);
  394. // _btnLogout.gameObject.SetActive(step == 1);
  395. // _btnGetUserInfo.gameObject.SetActive(step == 1);
  396. // _imgAvatar.gameObject.SetActive(step == 1);
  397. // _textNickname.gameObject.SetActive(step == 1);
  398. // _textGender.gameObject.SetActive(step == 1);
  399. // _imgAvatar.enabled = false;
  400. // _imgAvatar.sprite = null;
  401. // _textNickname.enabled = false;
  402. // _textGender.enabled = false;
  403. //}
  404. ///// <summary>
  405. ///// 陀螺仪文字更新
  406. ///// </summary>
  407. //void UpdateCalibrateGyrText()
  408. //{
  409. // Text text = _btnCalibrateGyr.GetComponentInChildren<Text>();
  410. // int progress = (int)(SmartBowHelper.GetInstance().GetGyrProgress() * 100);
  411. // string act = SmartBowHelper.GetInstance().IsGyrCalibrating() ? "停止" : "开始";
  412. // text.text = $"点击{act}陀螺仪校准({progress}%)";
  413. //}
  414. ///// <summary>
  415. ///// 地磁计文字更新
  416. ///// </summary>
  417. //void UpdateCalibrateMagText()
  418. //{
  419. // Text text = _btnCalibrateMag.GetComponentInChildren<Text>();
  420. // if (SmartBowHelper.GetInstance().IsMagCompleted())
  421. // {
  422. // text.text = $"<color=green>校准完成</color>(点击重置)";
  423. // }
  424. // else
  425. // {
  426. // string tip = Time.realtimeSinceStartup - _clickCalibrateMagTime < 2 ? "<color=#FF670D>已重置</color>" : "点击重置";
  427. // text.text = $"<color=blue>自动校准中</color>({tip})";
  428. // }
  429. //}
  430. /// <summary>
  431. /// 进度文字更新
  432. /// </summary>
  433. public void UpdateProcess()
  434. {
  435. if (ProcessManager.GetInstance().CurrLevel <= 0) return;
  436. _textProcess.text = $"{(ProcessManager.GetInstance().CurrLevel > ProcessModule.GetInstance().processDataDic.Count ? ProcessModule.GetInstance().processDataDic.Count : ProcessManager.GetInstance().CurrLevel)}/{ProcessModule.GetInstance().processDataDic.Count}";
  437. }
  438. public void SetProcessInfoActive(bool active)
  439. {
  440. _processObj.gameObject.SetActive(active);
  441. _textEnemy.gameObject.SetActive(active);
  442. }
  443. //private void OnBluetoothModuleInited()
  444. //{
  445. // textBattery.color = new Color(1, 1, 1, 1);
  446. // textMacAddress.color = new Color(1, 1, 1, 1);
  447. // // 初始化完成
  448. // // 真 连接成功 文本表现 见OnBluetoothStatusChanged注释
  449. // if (SmartBowHelper.GetInstance().IsBluetoothModuleInited()) btnConnectText.text = "<color=green>已连接</color>(点击断开)";
  450. // else btnConnectText.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  451. // // 开始get电量和macAdd (在update处理
  452. //}
  453. ///// <summary>
  454. ///// 蓝牙状态变化
  455. ///// </summary>
  456. ///// <param name="oldStatus"></param>
  457. ///// <param name="newStatus"></param>
  458. //void OnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
  459. //{
  460. // if (newStatus == BluetoothStatusEnum.None) btnConnectText.text = "<color=blue>未连接</color>(点击连接)";
  461. // if (newStatus == BluetoothStatusEnum.Connecting) btnConnectText.text = "<color=#FF670D>连接中</color>";
  462. // if (newStatus == BluetoothStatusEnum.Connected) btnConnectText.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
  463. //}
  464. #endregion
  465. }
  466. }