StatusController.cs 9.0 KB

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