StatusController.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. /* 这个controller 是用于控制 Status UI菜单的
  7. */
  8. public class StatusController : MonoBehaviour
  9. {
  10. public Button backButton;
  11. public Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel, statusLabel, statusValueLabel;
  12. DogProperty puppy;
  13. public VisualElement selectElement;
  14. // Start is called before the first frame update
  15. void OnEnable()
  16. {
  17. var root = GetComponent<UIDocument>().rootVisualElement;
  18. backButton = root.Q<Button>("back");
  19. nameLabel = root.Q<Label>("name");
  20. genderLabel = root.Q<Label>("gender");
  21. breedLabel = root.Q<Label>("breed");
  22. hostLabel = root.Q<Label>("host");
  23. hostnameLabel = root.Q<Label>("hostname");
  24. ageLabel = root.Q<Label>("age");
  25. ageValueLabel = root.Q<Label>("ageValue");
  26. statusLabel = root.Q<Label>("status");
  27. statusValueLabel = root.Q<Label>("statusValue");
  28. selectElement = root.Q("selectElement");
  29. // 绑定事件
  30. backButton.clicked += BackBtnClick;
  31. // todo 目前只有一个狗,以后会添加这里要改
  32. puppy = UserProperty.dogs[0];
  33. StatusPageUpdate();
  34. LabelLanguageSetting();
  35. StatusSummary();
  36. }
  37. // Update is called once per frame
  38. //void Update()
  39. //{
  40. //}
  41. //void BackPressed()
  42. //{
  43. // if (backButton != null)
  44. // {
  45. // var root = GetComponent<UIDocument>();
  46. // Destroy(GameObject.Find(root.GetComponentInParent<Canvas>().name));
  47. // }
  48. //}
  49. void StatusPageUpdate()
  50. {
  51. //backButton.clicked += BackPressed;
  52. nameLabel.text = puppy.name;
  53. if (puppy.sex == 1) {
  54. genderLabel.text = "♂";
  55. }
  56. else
  57. {
  58. genderLabel.text = "♀";
  59. }
  60. breedLabel.text = puppy.breed;
  61. hostnameLabel.text = UserProperty.name;
  62. // 计算狗生日和现在时间差
  63. TimeSpan ts = System.DateTime.Now - puppy.brithday;
  64. ageValueLabel.text = ts.Days.ToString();
  65. //根据狗的数量添加选择球
  66. int puppyQty = UserProperty.dogs.Count;
  67. VisualTreeAsset selectBall = Resources.Load<VisualTreeAsset>("Status/SelectBall");
  68. // 这里将VisualTreeAsset转换成VisualElement类型
  69. VisualElement uiDocument = selectBall.CloneTree();
  70. VisualElement[] selectBalls = new VisualElement[puppyQty];
  71. for (int i = 0; i<puppyQty; i++)
  72. {
  73. selectBalls[i] = uiDocument;
  74. selectElement.Add(selectBalls[i]);
  75. }
  76. // todo 设置selectBall颜色对应选择
  77. }
  78. void LabelLanguageSetting()
  79. {
  80. // 设置status UI界面里面label和按键的语言显示
  81. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
  82. backButton.text = textValue;
  83. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
  84. hostLabel.text = textValue;
  85. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
  86. ageLabel.text = textValue;
  87. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
  88. statusLabel.text = textValue;
  89. }
  90. void StatusSummary()
  91. {
  92. // 先清空状态文字,应对切换多只狗
  93. statusValueLabel.text = null;
  94. // 汇总宠物状态
  95. string summary = "";
  96. if (puppy.satiety < 30)
  97. {
  98. if (puppy.satiety < 10)
  99. {
  100. // 小于10,达到L2警告
  101. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
  102. }
  103. else
  104. {
  105. // 不小于10,触发L1警告
  106. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
  107. }
  108. summary += "<br>";
  109. }
  110. if (puppy.stamina < 30)
  111. {
  112. if (puppy.stamina < 10)
  113. {
  114. // 小于10,达到L2警告
  115. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
  116. }
  117. else
  118. {
  119. // 不小于10,触发L1警告
  120. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
  121. }
  122. summary += "<br>";
  123. }
  124. if (puppy.thirsty < 30)
  125. {
  126. if (puppy.thirsty < 10)
  127. {
  128. // 小于10,达到L2警告
  129. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
  130. }
  131. else
  132. {
  133. // 不小于10,触发L1警告
  134. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
  135. }
  136. summary += "<br>";
  137. }
  138. if (puppy.healthy < 30)
  139. {
  140. if (puppy.healthy < 10)
  141. {
  142. // 小于10,达到L2警告
  143. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
  144. }
  145. else
  146. {
  147. // 不小于10,触发L1警告
  148. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
  149. }
  150. summary += "<br>";
  151. }
  152. if (puppy.clean < 30)
  153. {
  154. if (puppy.clean < 10)
  155. {
  156. // 小于10,达到L2警告
  157. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
  158. }
  159. else
  160. {
  161. // 不小于10,触发L1警告
  162. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
  163. }
  164. summary += "<br>";
  165. }
  166. if (puppy.obesity > 70)
  167. {
  168. if (puppy.obesity >90)
  169. {
  170. // 小于10,达到L2警告
  171. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
  172. }
  173. else
  174. {
  175. // 不小于10,触发L1警告
  176. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
  177. }
  178. summary += "<br>";
  179. }
  180. // 如果没有任何异常,返回正常的状态
  181. if (summary.Length == 0)
  182. {
  183. summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
  184. }
  185. statusValueLabel.text = summary;
  186. }
  187. void BackBtnClick()
  188. {
  189. var uiPlaceholder = GameObject.Find("UI Placeholder");
  190. if (uiPlaceholder != null)
  191. {
  192. var shoppingUI = uiPlaceholder.transform.Find("Status").gameObject;
  193. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  194. shoppingUI.SetActive(false);
  195. vamUI.SetActive(true);
  196. }
  197. }
  198. }