StatusController.cs 7.8 KB

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