StatusController.cs 12 KB

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