HomeView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. using System;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. /* 主界面 */
  10. public class HomeView : MonoBehaviour
  11. {
  12. [SerializeField] Image myAvatarSprite;
  13. [SerializeField] Text nickNameText;
  14. [SerializeField] GameObject[] genders;
  15. [SerializeField] GameObject btnConnectBow;
  16. [SerializeField] GameObject btnConnectArrow;
  17. [SerializeField] GameObject friendTip;
  18. public static HomeView ins;
  19. void Awake() {
  20. if (CommonConfig.needToExamine) {
  21. transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false);
  22. transform.Find("RightPanel/Item/Text").GetComponent<TextAutoLanguage>().SetText(1234562);
  23. transform.Find("RightPanel/Item (1)/Text").GetComponent<TextAutoLanguage>().SetText(1234563);
  24. transform.Find("RightPanel/Item (3)").gameObject.SetActive(false);
  25. }
  26. }
  27. void Start()
  28. {
  29. ins = this;
  30. BluetoothHolder.Init();
  31. AudioMgr.Init();
  32. TopBarView.NeedShowIt(this);
  33. if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
  34. if (LoginMgr.myUserInfo.id > 0) {
  35. RenderNameOrGender();
  36. RenderMyAvatarSprite();
  37. RenderDeviceNames();
  38. }
  39. StartCoroutine(RefreshFriendBar());
  40. }
  41. void OnDestroy()
  42. {
  43. if (ins == this) ins = null;
  44. TopBarView.DontNeedShowIt(this);
  45. }
  46. void Update() {
  47. if (UserPlayer.ins != null) {
  48. if (UserPlayer.ins.tempData.hasFriendRequest && !friendTip.activeSelf) {
  49. friendTip.SetActive(true);
  50. }
  51. else if (!UserPlayer.ins.tempData.hasFriendRequest && friendTip.activeSelf) {
  52. friendTip.SetActive(false);
  53. }
  54. }
  55. }
  56. public void RenderNameOrGender() {
  57. nickNameText.text = LoginMgr.myUserInfo.nickname;
  58. genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
  59. genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
  60. LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
  61. }
  62. public void RenderMyAvatarSprite() {
  63. myAvatarSprite.sprite = RoleMgr.GetAvatar(LoginMgr.myUserInfo.avatarID);
  64. }
  65. public void RenderDeviceNames()
  66. {
  67. try {
  68. (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
  69. // this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
  70. this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(200000);
  71. this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
  72. } catch (System.Exception) {}
  73. }
  74. public Action action_OnClickStartGame;
  75. public void GoTo(string target) {
  76. AudioMgr.ins.PlayBtn();
  77. switch (target)
  78. {
  79. case "开始游戏":
  80. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  81. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  82. return;
  83. }
  84. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("GameStartView"));
  85. action_OnClickStartGame?.Invoke();
  86. break;
  87. case "联机游戏":
  88. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  89. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  90. return;
  91. }
  92. GlobalDataTemp.pkMatchType = PKMatchType.OnlinePK;
  93. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("PKGameOptionView"));
  94. break;
  95. case "好友":
  96. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/FriendView"));
  97. break;
  98. case "排行磅":
  99. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RankView"));
  100. break;
  101. case "对战":
  102. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RoleSelectView"));
  103. break;
  104. case "教程":
  105. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/CourseView"), Vector3.zero, new Quaternion());
  106. break;
  107. case "我的":
  108. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/MeView"), Vector3.zero, new Quaternion());
  109. break;
  110. case "设备":
  111. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/DeviceView"), Vector3.zero, new Quaternion());
  112. break;
  113. case "商城":
  114. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/ShopView"), Vector3.zero, new Quaternion());
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. /* FriendBar */
  121. GameObject friendItemPrefab = null;
  122. Transform friendBarContent = null;
  123. IEnumerator RefreshFriendBar() {
  124. if (friendItemPrefab == null) {
  125. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  126. Destroy(friendItemPrefab.GetComponent<Image>());
  127. friendItemPrefab.SetActive(false);
  128. }
  129. if (friendBarContent == null) {
  130. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  131. }
  132. while (LoginMgr.myUserInfo.id < 1) {
  133. yield return null;
  134. }
  135. Action<JArray> cb = RenderFriendList;
  136. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  137. }
  138. public void RenderFriendList(JArray list) {
  139. if (flag_SwapFriendAndRank != 0) return;
  140. for (int i = 1; i < friendBarContent.childCount; i++) {
  141. Destroy(friendBarContent.GetChild(i).gameObject);
  142. }
  143. if (list.Count > 0) {
  144. Color outColor;
  145. foreach (var itemInfo in list) {
  146. int friendID = itemInfo.Value<int>("friendID");
  147. int avatarID = itemInfo.Value<int>("avatarID");
  148. string nickname = itemInfo.Value<string>("nickname");
  149. bool online = itemInfo.Value<bool>("online");
  150. long offlineTime = itemInfo.Value<long>("offlineTime");
  151. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  152. o.SetActive(true);
  153. o.name = friendID.ToString();
  154. o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  155. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  156. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  157. TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
  158. if (online) {
  159. ColorUtility.TryParseHtmlString("#12B525", out outColor);
  160. } else {
  161. ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
  162. }
  163. timeTip.GetComponent<Text>().color = outColor;
  164. }
  165. }
  166. }
  167. IEnumerator RefreshFriendRankBar() {
  168. if (friendItemPrefab == null) {
  169. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  170. Destroy(friendItemPrefab.GetComponent<Image>());
  171. friendItemPrefab.SetActive(false);
  172. }
  173. if (friendBarContent == null) {
  174. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  175. }
  176. while (LoginMgr.myUserInfo.id < 1) {
  177. yield return null;
  178. }
  179. Action<JArray> cb = RenderFriendRankList;
  180. UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
  181. }
  182. void RenderFriendRankList(JArray list) {
  183. if (flag_SwapFriendAndRank != 1) return;
  184. for (int i = 1; i < friendBarContent.childCount; i++) {
  185. Destroy(friendBarContent.GetChild(i).gameObject);
  186. }
  187. if (list.Count > 0) {
  188. foreach (var itemInfo in list) {
  189. int userID = itemInfo.Value<int>("id");
  190. int avatarID = itemInfo.Value<int>("avatarID");
  191. string nickname = itemInfo.Value<string>("nickname");
  192. bool online = itemInfo.Value<bool>("online");
  193. long offlineTime = itemInfo.Value<long>("offlineTime");
  194. int rankNum = itemInfo.Value<int>("rankNum");
  195. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  196. o.SetActive(true);
  197. o.name = userID.ToString();
  198. o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  199. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  200. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  201. Action<Text> onApplyToNext = (t) => {
  202. if (TextAutoLanguage2.GetLanguage() == 0) {
  203. t.text = $"第{rankNum}名";
  204. } else {
  205. string str = rankNum.ToString();
  206. if (str.EndsWith("1")) t.text = $"{rankNum}st";
  207. else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
  208. else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
  209. else t.text = $"{rankNum}th";
  210. }
  211. Color outColor;
  212. if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
  213. else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
  214. else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
  215. else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
  216. t.color = outColor;
  217. };
  218. timeTip.onApplyToNext += onApplyToNext;
  219. }
  220. }
  221. }
  222. int flag_SwapFriendAndRank = 0;
  223. public void OnClick_SwapFriendAndRank() {
  224. AudioMgr.ins.PlayBtn();
  225. if (flag_SwapFriendAndRank == 0) {
  226. flag_SwapFriendAndRank = 1;
  227. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
  228. StartCoroutine(RefreshFriendRankBar());
  229. } else if (flag_SwapFriendAndRank == 1) {
  230. flag_SwapFriendAndRank = 0;
  231. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
  232. StartCoroutine(RefreshFriendBar());
  233. }
  234. }
  235. public void OnClick_ShowDetail() {
  236. AudioMgr.ins.PlayBtn();
  237. if (flag_SwapFriendAndRank == 0) {
  238. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/FriendView"));
  239. } else if (flag_SwapFriendAndRank == 1) {
  240. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RankView"));
  241. }
  242. }
  243. }