StatusController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UIElements;
  7. /* 这个controller 是用于控制 Status UI菜单的
  8. * 包含显示狗的最新状态
  9. * 添加狗,把狗送给朋友,删除狗
  10. */
  11. public class StatusController : MonoBehaviour
  12. {
  13. private Button backButton, cancelButton;
  14. private Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel, statusLabel, statusValueLabel;
  15. private Label leftArrow, rightArrow;
  16. private Label addLabel, transfer, remove, callback;
  17. private Label transferMsgLabel;
  18. private string transferMsgBasic;
  19. private DateTime transferCountStartTime = new DateTime();
  20. DogProperty puppy;
  21. private VisualElement selectElement, statusElement, transferElement;
  22. private StausUIPage currentPage = StausUIPage.Status;
  23. // Start is called before the first frame update
  24. void OnEnable()
  25. {
  26. var root = GetComponent<UIDocument>().rootVisualElement;
  27. statusElement = root.Q("statusElement");
  28. transferElement = root.Q("transferElement");
  29. backButton = root.Q<Button>("back");
  30. cancelButton = root.Q<Button>("cancel");
  31. nameLabel = root.Q<Label>("name");
  32. genderLabel = root.Q<Label>("gender");
  33. breedLabel = root.Q<Label>("breed");
  34. hostLabel = root.Q<Label>("host");
  35. hostnameLabel = root.Q<Label>("hostname");
  36. ageLabel = root.Q<Label>("age");
  37. ageValueLabel = root.Q<Label>("ageValue");
  38. statusLabel = root.Q<Label>("status");
  39. statusValueLabel = root.Q<Label>("statusValue");
  40. selectElement = root.Q("selectElement");
  41. leftArrow = root.Q<Label>("leftArrow");
  42. rightArrow = root.Q<Label>("rightArrow");
  43. addLabel = root.Q<Label>("add");
  44. transfer = root.Q<Label>("transfer");
  45. remove = root.Q<Label>("remove");
  46. callback = root.Q<Label>("callback");
  47. transferMsgLabel = root.Q<Label>("transferMsg");
  48. // 绑定事件
  49. backButton.clicked += BackBtnClick;
  50. cancelButton.clicked += CancelBtnClick;
  51. leftArrow.RegisterCallback<ClickEvent>(e => LeftArrowClicked(e));
  52. rightArrow.RegisterCallback<ClickEvent>(e => RightArrowClicked(e));
  53. addLabel.RegisterCallback<ClickEvent>(e => AddClick(e));
  54. remove.RegisterCallback<ClickEvent>(e => RemoveClick(e));
  55. transfer.RegisterCallback<ClickEvent>(e => TransferClick(e));
  56. // 箭头是否显示
  57. if (UserProperty.dogs.Count > 1)
  58. {
  59. leftArrow.visible = true;
  60. rightArrow.visible = true;
  61. }
  62. else
  63. {
  64. leftArrow.visible = false;
  65. rightArrow.visible = false;
  66. }
  67. // 根据用户名下狗的数量判断是否显示transfer按键
  68. if (UserProperty.dogs.Count > 1)
  69. {
  70. transfer.style.display = DisplayStyle.Flex;
  71. }
  72. else
  73. {
  74. transfer.style.display = DisplayStyle.None;
  75. }
  76. // 如果用户狗的数量达到系统规定上限,则隐藏add按键
  77. if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
  78. {
  79. addLabel.style.display = DisplayStyle.None;
  80. }
  81. else
  82. {
  83. addLabel.style.display = DisplayStyle.Flex;
  84. }
  85. }
  86. //private void Start()
  87. //{
  88. //}
  89. // Update is called once per frame
  90. void Update()
  91. {
  92. // 刷新狗的数据
  93. // puppy = UserProperty.dogs[GameData.focusDog];
  94. // StatusPageUpdate();
  95. // LabelLanguageSetting();
  96. // StatusSummary();
  97. if (currentPage == StausUIPage.Transfer)
  98. {
  99. TimeSpan ts = DateTime.Now - transferCountStartTime;
  100. if (ts.TotalSeconds > 120)
  101. {
  102. // todo 超过120秒,关闭二维码
  103. transferElement.style.display = DisplayStyle.None;
  104. statusElement.style.display = DisplayStyle.Flex;
  105. currentPage = StausUIPage.Status;
  106. }
  107. }
  108. if (currentPage == StausUIPage.Status)
  109. {
  110. // 刷新狗的数据
  111. puppy = UserProperty.dogs[GameData.focusDog];
  112. StatusPageUpdate();
  113. LabelLanguageSetting();
  114. StatusSummary();
  115. }
  116. }
  117. //void BackPressed()
  118. //{
  119. // if (backButton != null)
  120. // {
  121. // var root = GetComponent<UIDocument>();
  122. // Destroy(GameObject.Find(root.GetComponentInParent<Canvas>().name));
  123. // }
  124. //}
  125. void StatusPageUpdate()
  126. {
  127. //backButton.clicked += BackPressed;
  128. nameLabel.text = puppy.dog_name;
  129. if (puppy.sex == 1)
  130. {
  131. genderLabel.text = "♂";
  132. }
  133. else
  134. {
  135. genderLabel.text = "♀";
  136. }
  137. breedLabel.text = puppy.breed;
  138. foreach (var breed in EnviromentSetting.dogBreeds)
  139. {
  140. if (breed.breed == puppy.breed)
  141. {
  142. breedLabel.text = breed.name[EnviromentSetting.languageCode];
  143. break;
  144. }
  145. }
  146. hostnameLabel.text = UserProperty.name;
  147. // 计算狗生日和现在时间差
  148. TimeSpan ts = System.DateTime.Now - puppy.brithday;
  149. ageValueLabel.text = ts.Days.ToString();
  150. //根据狗的数量添加选择球
  151. //int puppyQty = UserProperty.dogs.Count;
  152. //VisualTreeAsset selectBall = Resources.Load<VisualTreeAsset>("Status/SelectBall");
  153. //// 这里将VisualTreeAsset转换成VisualElement类型
  154. //VisualElement uiDocument = selectBall.CloneTree();
  155. //VisualElement[] selectBalls = new VisualElement[puppyQty];
  156. //for (int i = 0; i<puppyQty; i++)
  157. //{
  158. // selectBalls[i] = uiDocument;
  159. // selectElement.Add(selectBalls[i]);
  160. //}
  161. // todo 设置selectBall颜色对应选择
  162. }
  163. void LabelLanguageSetting()
  164. {
  165. // 设置status UI界面里面label和按键的语言显示
  166. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
  167. backButton.text = textValue;
  168. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "cancel", EnviromentSetting.languageCode });
  169. cancelButton.text = textValue;
  170. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
  171. hostLabel.text = textValue;
  172. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
  173. ageLabel.text = textValue;
  174. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
  175. statusLabel.text = textValue;
  176. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "message", "scan_QRcode", EnviromentSetting.languageCode });
  177. transferMsgBasic = textValue;
  178. }
  179. void StatusSummary()
  180. {
  181. // 先清空状态文字,应对切换多只狗
  182. statusValueLabel.text = null;
  183. // 汇总宠物状态
  184. string summary = "";
  185. if (puppy.satiety < 30)
  186. {
  187. if (puppy.satiety < 10)
  188. {
  189. // 小于10,达到L2警告
  190. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
  191. }
  192. else
  193. {
  194. // 不小于10,触发L1警告
  195. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
  196. }
  197. summary += "<br>";
  198. }
  199. if (puppy.stamina < 30)
  200. {
  201. if (puppy.stamina < 10)
  202. {
  203. // 小于10,达到L2警告
  204. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
  205. }
  206. else
  207. {
  208. // 不小于10,触发L1警告
  209. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
  210. }
  211. summary += "<br>";
  212. }
  213. if (puppy.thirsty < 30)
  214. {
  215. if (puppy.thirsty < 10)
  216. {
  217. // 小于10,达到L2警告
  218. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
  219. }
  220. else
  221. {
  222. // 不小于10,触发L1警告
  223. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
  224. }
  225. summary += "<br>";
  226. }
  227. if (puppy.healthy < 30)
  228. {
  229. if (puppy.healthy < 10)
  230. {
  231. // 小于10,达到L2警告
  232. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
  233. }
  234. else
  235. {
  236. // 不小于10,触发L1警告
  237. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
  238. }
  239. summary += "<br>";
  240. }
  241. if (puppy.clean < 30)
  242. {
  243. if (puppy.clean < 10)
  244. {
  245. // 小于10,达到L2警告
  246. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
  247. }
  248. else
  249. {
  250. // 不小于10,触发L1警告
  251. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
  252. }
  253. summary += "<br>";
  254. }
  255. if (puppy.obesity > 70)
  256. {
  257. if (puppy.obesity > 90)
  258. {
  259. // 小于10,达到L2警告
  260. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
  261. }
  262. else
  263. {
  264. // 不小于10,触发L1警告
  265. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
  266. }
  267. summary += "<br>";
  268. }
  269. // 如果没有任何异常,返回正常的状态
  270. if (summary.Length == 0)
  271. {
  272. summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
  273. }
  274. statusValueLabel.text = summary;
  275. }
  276. void RightArrowClicked(ClickEvent e)
  277. {
  278. GameData.focusDog++;
  279. if (GameData.focusDog == UserProperty.dogs.Count)
  280. {
  281. GameData.focusDog = 0;
  282. }
  283. }
  284. void LeftArrowClicked(ClickEvent e)
  285. {
  286. GameData.focusDog--;
  287. if (GameData.focusDog == -1)
  288. {
  289. GameData.focusDog = UserProperty.dogs.Count - 1;
  290. }
  291. }
  292. void BackBtnClick()
  293. {
  294. var uiPlaceholder = GameObject.Find("UI Placeholder");
  295. if (uiPlaceholder != null)
  296. {
  297. var shoppingUI = uiPlaceholder.transform.Find("Status").gameObject;
  298. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  299. shoppingUI.SetActive(false);
  300. vamUI.SetActive(true);
  301. }
  302. }
  303. void CancelBtnClick()
  304. {
  305. statusElement.style.display = DisplayStyle.Flex;
  306. transferElement.style.display = DisplayStyle.None;
  307. currentPage = StausUIPage.Status;
  308. }
  309. void AddClick(ClickEvent e)
  310. {
  311. // if (UserProperty.level == "basic")
  312. // {
  313. // string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_account_level", EnviromentSetting.languageCode });
  314. // MessageBoxController.ShowMessage(msg);
  315. // }
  316. if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
  317. {
  318. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_no_more", EnviromentSetting.languageCode });
  319. MessageBoxController.ShowMessage(msg);
  320. }
  321. else
  322. {
  323. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_prompt", EnviromentSetting.languageCode });
  324. MessageBoxController.YorN_Message(msg, SwitchAddNewDog);
  325. }
  326. }
  327. // 跳转login 场景添加新的狗
  328. void SwitchAddNewDog()
  329. {
  330. GameData.subScene = "Login_InitDog";
  331. MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
  332. }
  333. void RemoveClick(ClickEvent e)
  334. {
  335. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "foster_dog_prompt", EnviromentSetting.languageCode });
  336. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  337. MessageBoxController.YorN_Message(msg, FosterDog);
  338. }
  339. // 点击确认remove后调用寄养的程序
  340. void FosterDog()
  341. {
  342. // 检测用户是否有200金币
  343. if (UserProperty.coin >= 200)
  344. {
  345. // todo 寄养某一只狗,并扣除金币
  346. }
  347. }
  348. // 找回寄养的狗
  349. void CallbackDogClick(ClickEvent e)
  350. {
  351. }
  352. void TransferClick(ClickEvent e)
  353. {
  354. // TODO 添加代码增加面对面赠送狗的功能
  355. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "transfer_dog_prompt", EnviromentSetting.languageCode });
  356. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  357. MessageBoxController.YorN_Message(msg, TransferDogRequest);
  358. }
  359. void TransferDogRequest()
  360. {
  361. string url = "/api/transfer_dog/receive/";
  362. Dictionary<string, string> formData = new();
  363. WWWForm form = new();
  364. form.AddField("user_id", UserProperty.userId);
  365. form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
  366. StartCoroutine(WebController.PostRequest(url, form, callback: TransferDogCallback));
  367. }
  368. void TransferDogCallback(string json)
  369. {
  370. // TODO 生成二维码,和120秒倒计时
  371. transferCountStartTime = DateTime.Now;
  372. currentPage = StausUIPage.Transfer;
  373. }
  374. }
  375. enum StausUIPage
  376. {
  377. Status,
  378. Transfer,
  379. }