StatusController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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;
  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. DogProperty puppy;
  18. private VisualElement selectElement;
  19. // Start is called before the first frame update
  20. void OnEnable()
  21. {
  22. var root = GetComponent<UIDocument>().rootVisualElement;
  23. backButton = root.Q<Button>("back");
  24. nameLabel = root.Q<Label>("name");
  25. genderLabel = root.Q<Label>("gender");
  26. breedLabel = root.Q<Label>("breed");
  27. hostLabel = root.Q<Label>("host");
  28. hostnameLabel = root.Q<Label>("hostname");
  29. ageLabel = root.Q<Label>("age");
  30. ageValueLabel = root.Q<Label>("ageValue");
  31. statusLabel = root.Q<Label>("status");
  32. statusValueLabel = root.Q<Label>("statusValue");
  33. selectElement = root.Q("selectElement");
  34. leftArrow = root.Q<Label>("leftArrow");
  35. rightArrow = root.Q<Label>("rightArrow");
  36. addLabel = root.Q<Label>("add");
  37. transfer = root.Q<Label>("transfer");
  38. remove = root.Q<Label>("remove");
  39. callback = root.Q<Label>("callback");
  40. // 绑定事件
  41. backButton.clicked += BackBtnClick;
  42. leftArrow.RegisterCallback<ClickEvent>(e => LeftArrowClicked(e));
  43. rightArrow.RegisterCallback<ClickEvent>(e => RightArrowClicked(e));
  44. addLabel.RegisterCallback<ClickEvent>(e => AddClick(e));
  45. remove.RegisterCallback< ClickEvent >(e => RemoveClick(e));
  46. transfer.RegisterCallback<ClickEvent >(e => TransferClick(e));
  47. // 箭头是否显示
  48. if (UserProperty.dogs.Count > 1)
  49. {
  50. leftArrow.visible = true;
  51. rightArrow.visible = true;
  52. }
  53. }
  54. //private void Start()
  55. //{
  56. //}
  57. // Update is called once per frame
  58. void Update()
  59. {
  60. // 刷新狗的数据
  61. puppy = UserProperty.dogs[GameData.focusDog];
  62. StatusPageUpdate();
  63. LabelLanguageSetting();
  64. StatusSummary();
  65. }
  66. //void BackPressed()
  67. //{
  68. // if (backButton != null)
  69. // {
  70. // var root = GetComponent<UIDocument>();
  71. // Destroy(GameObject.Find(root.GetComponentInParent<Canvas>().name));
  72. // }
  73. //}
  74. void StatusPageUpdate()
  75. {
  76. //backButton.clicked += BackPressed;
  77. nameLabel.text = puppy.dog_name;
  78. if (puppy.sex == 1) {
  79. genderLabel.text = "♂";
  80. }
  81. else
  82. {
  83. genderLabel.text = "♀";
  84. }
  85. breedLabel.text = puppy.breed;
  86. hostnameLabel.text = UserProperty.name;
  87. // 计算狗生日和现在时间差
  88. TimeSpan ts = System.DateTime.Now - puppy.brithday;
  89. ageValueLabel.text = ts.Days.ToString();
  90. //根据狗的数量添加选择球
  91. //int puppyQty = UserProperty.dogs.Count;
  92. //VisualTreeAsset selectBall = Resources.Load<VisualTreeAsset>("Status/SelectBall");
  93. //// 这里将VisualTreeAsset转换成VisualElement类型
  94. //VisualElement uiDocument = selectBall.CloneTree();
  95. //VisualElement[] selectBalls = new VisualElement[puppyQty];
  96. //for (int i = 0; i<puppyQty; i++)
  97. //{
  98. // selectBalls[i] = uiDocument;
  99. // selectElement.Add(selectBalls[i]);
  100. //}
  101. // todo 设置selectBall颜色对应选择
  102. }
  103. void LabelLanguageSetting()
  104. {
  105. // 设置status UI界面里面label和按键的语言显示
  106. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
  107. backButton.text = textValue;
  108. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
  109. hostLabel.text = textValue;
  110. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
  111. ageLabel.text = textValue;
  112. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
  113. statusLabel.text = textValue;
  114. }
  115. void StatusSummary()
  116. {
  117. // 先清空状态文字,应对切换多只狗
  118. statusValueLabel.text = null;
  119. // 汇总宠物状态
  120. string summary = "";
  121. if (puppy.satiety < 30)
  122. {
  123. if (puppy.satiety < 10)
  124. {
  125. // 小于10,达到L2警告
  126. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
  127. }
  128. else
  129. {
  130. // 不小于10,触发L1警告
  131. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
  132. }
  133. summary += "<br>";
  134. }
  135. if (puppy.stamina < 30)
  136. {
  137. if (puppy.stamina < 10)
  138. {
  139. // 小于10,达到L2警告
  140. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
  141. }
  142. else
  143. {
  144. // 不小于10,触发L1警告
  145. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
  146. }
  147. summary += "<br>";
  148. }
  149. if (puppy.thirsty < 30)
  150. {
  151. if (puppy.thirsty < 10)
  152. {
  153. // 小于10,达到L2警告
  154. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
  155. }
  156. else
  157. {
  158. // 不小于10,触发L1警告
  159. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
  160. }
  161. summary += "<br>";
  162. }
  163. if (puppy.healthy < 30)
  164. {
  165. if (puppy.healthy < 10)
  166. {
  167. // 小于10,达到L2警告
  168. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
  169. }
  170. else
  171. {
  172. // 不小于10,触发L1警告
  173. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
  174. }
  175. summary += "<br>";
  176. }
  177. if (puppy.clean < 30)
  178. {
  179. if (puppy.clean < 10)
  180. {
  181. // 小于10,达到L2警告
  182. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
  183. }
  184. else
  185. {
  186. // 不小于10,触发L1警告
  187. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
  188. }
  189. summary += "<br>";
  190. }
  191. if (puppy.obesity > 70)
  192. {
  193. if (puppy.obesity >90)
  194. {
  195. // 小于10,达到L2警告
  196. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
  197. }
  198. else
  199. {
  200. // 不小于10,触发L1警告
  201. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
  202. }
  203. summary += "<br>";
  204. }
  205. // 如果没有任何异常,返回正常的状态
  206. if (summary.Length == 0)
  207. {
  208. summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
  209. }
  210. statusValueLabel.text = summary;
  211. }
  212. void RightArrowClicked(ClickEvent e)
  213. {
  214. GameData.focusDog++;
  215. if (GameData.focusDog == UserProperty.dogs.Count)
  216. {
  217. GameData.focusDog = 0;
  218. }
  219. }
  220. void LeftArrowClicked(ClickEvent e)
  221. {
  222. GameData.focusDog--;
  223. if (GameData.focusDog == -1)
  224. {
  225. GameData.focusDog = UserProperty.dogs.Count-1;
  226. }
  227. }
  228. void BackBtnClick()
  229. {
  230. var uiPlaceholder = GameObject.Find("UI Placeholder");
  231. if (uiPlaceholder != null)
  232. {
  233. var shoppingUI = uiPlaceholder.transform.Find("Status").gameObject;
  234. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  235. shoppingUI.SetActive(false);
  236. vamUI.SetActive(true);
  237. }
  238. }
  239. void AddClick(ClickEvent e)
  240. {
  241. if (UserProperty.level == "basic")
  242. {
  243. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_account_level", EnviromentSetting.languageCode });
  244. MessageBoxController.ShowMessage(msg);
  245. }
  246. else if (UserProperty.dogs.Count == EnviromentSetting.maxDogQty)
  247. {
  248. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_no_more", EnviromentSetting.languageCode });
  249. MessageBoxController.ShowMessage(msg);
  250. }
  251. else
  252. {
  253. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_prompt", EnviromentSetting.languageCode });
  254. MessageBoxController.YorN_Message(msg, SwitchAddNewDog);
  255. }
  256. }
  257. // 跳转login 场景添加新的狗
  258. void SwitchAddNewDog()
  259. {
  260. GameData.subScene = "Login_InitDog";
  261. SceneManager.LoadScene("Home");
  262. }
  263. void RemoveClick(ClickEvent e) {
  264. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "foster_dog_prompt", EnviromentSetting.languageCode });
  265. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  266. MessageBoxController.YorN_Message(msg, FosterDog);
  267. }
  268. // 点击确认remove后调用寄养的程序
  269. void FosterDog()
  270. {
  271. // 检测用户是否有200金币
  272. if (UserProperty.coin >= 200)
  273. {
  274. // todo 寄养某一只狗,并扣除金币
  275. }
  276. }
  277. // 找回寄养的狗
  278. void CallbackDogClick(ClickEvent e){
  279. }
  280. void TransferClick(ClickEvent e)
  281. {
  282. // TODO 添加代码增加面对面赠送狗的功能
  283. }
  284. }