StatusController.cs 12 KB

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