HomeView.cs 11 KB

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