StatusController.cs 11 KB

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