StatusController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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, cancelButton;
  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. private Label transferMsgLabel;
  18. private string transferMsgBasic;
  19. private DateTime transferCountStartTime = new DateTime();
  20. DogProperty puppy;
  21. private VisualElement selectElement, statusElement, transferElement;
  22. private StausUIPage currentPage = StausUIPage.Status;
  23. // Start is called before the first frame update
  24. void OnEnable()
  25. {
  26. var root = GetComponent<UIDocument>().rootVisualElement;
  27. statusElement = root.Q("statusElement");
  28. transferElement = root.Q("transferElement");
  29. backButton = root.Q<Button>("back");
  30. cancelButton = root.Q<Button>("cancel");
  31. nameLabel = root.Q<Label>("name");
  32. genderLabel = root.Q<Label>("gender");
  33. breedLabel = root.Q<Label>("breed");
  34. hostLabel = root.Q<Label>("host");
  35. hostnameLabel = root.Q<Label>("hostname");
  36. ageLabel = root.Q<Label>("age");
  37. ageValueLabel = root.Q<Label>("ageValue");
  38. statusLabel = root.Q<Label>("status");
  39. statusValueLabel = root.Q<Label>("statusValue");
  40. selectElement = root.Q("selectElement");
  41. leftArrow = root.Q<Label>("leftArrow");
  42. rightArrow = root.Q<Label>("rightArrow");
  43. addLabel = root.Q<Label>("add");
  44. transfer = root.Q<Label>("transfer");
  45. remove = root.Q<Label>("remove");
  46. callback = root.Q<Label>("callback");
  47. transferMsgLabel = root.Q<Label>("transferMsg");
  48. // 绑定事件
  49. backButton.clicked += BackBtnClick;
  50. cancelButton.clicked += CancelBtnClick;
  51. leftArrow.RegisterCallback<ClickEvent>(e => LeftArrowClicked(e));
  52. rightArrow.RegisterCallback<ClickEvent>(e => RightArrowClicked(e));
  53. addLabel.RegisterCallback<ClickEvent>(e => AddClick(e));
  54. remove.RegisterCallback< ClickEvent >(e => RemoveClick(e));
  55. transfer.RegisterCallback<ClickEvent >(e => TransferClick(e));
  56. // 箭头是否显示
  57. if (UserProperty.dogs.Count > 1)
  58. {
  59. leftArrow.visible = true;
  60. rightArrow.visible = true;
  61. }
  62. else
  63. {
  64. leftArrow.visible = false;
  65. rightArrow.visible = false;
  66. }
  67. // 根据用户名下狗的数量判断是否显示transfer按键
  68. if (UserProperty.dogs.Count > 1)
  69. {
  70. transfer.style.display = DisplayStyle.Flex;
  71. }
  72. else
  73. {
  74. transfer.style.display = DisplayStyle.None;
  75. }
  76. // 如果用户狗的数量达到系统规定上限,则隐藏add按键
  77. if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
  78. {
  79. addLabel.style.display = DisplayStyle.None;
  80. }
  81. else
  82. {
  83. addLabel.style.display = DisplayStyle.Flex;
  84. }
  85. // 刷新狗的数据
  86. puppy = UserProperty.dogs[GameData.focusDog];
  87. StatusPageUpdate();
  88. LabelLanguageSetting();
  89. StatusSummary();
  90. }
  91. //private void Start()
  92. //{
  93. //}
  94. // Update is called once per frame
  95. void Update()
  96. {
  97. // 刷新狗的数据
  98. // puppy = UserProperty.dogs[GameData.focusDog];
  99. // StatusPageUpdate();
  100. // LabelLanguageSetting();
  101. // StatusSummary();
  102. if (currentPage == StausUIPage.Transfer)
  103. {
  104. TimeSpan ts = DateTime.Now - transferCountStartTime;
  105. if (ts.TotalSeconds > 120)
  106. {
  107. // todo 超过120秒,关闭二维码
  108. transferElement.style.display = DisplayStyle.None;
  109. statusElement.style.display = DisplayStyle.Flex;
  110. currentPage = StausUIPage.Status;
  111. }
  112. }
  113. }
  114. //void BackPressed()
  115. //{
  116. // if (backButton != null)
  117. // {
  118. // var root = GetComponent<UIDocument>();
  119. // Destroy(GameObject.Find(root.GetComponentInParent<Canvas>().name));
  120. // }
  121. //}
  122. void StatusPageUpdate()
  123. {
  124. //backButton.clicked += BackPressed;
  125. nameLabel.text = puppy.dog_name;
  126. if (puppy.sex == 1) {
  127. genderLabel.text = "♂";
  128. }
  129. else
  130. {
  131. genderLabel.text = "♀";
  132. }
  133. breedLabel.text = puppy.breed;
  134. foreach (var breed in EnviromentSetting.dogBreeds)
  135. {
  136. if (breed.breed == puppy.breed)
  137. {
  138. breedLabel.text = breed.name[EnviromentSetting.languageCode];
  139. break;
  140. }
  141. }
  142. hostnameLabel.text = UserProperty.name;
  143. // 计算狗生日和现在时间差
  144. TimeSpan ts = System.DateTime.Now - puppy.brithday;
  145. ageValueLabel.text = ts.Days.ToString();
  146. //根据狗的数量添加选择球
  147. //int puppyQty = UserProperty.dogs.Count;
  148. //VisualTreeAsset selectBall = Resources.Load<VisualTreeAsset>("Status/SelectBall");
  149. //// 这里将VisualTreeAsset转换成VisualElement类型
  150. //VisualElement uiDocument = selectBall.CloneTree();
  151. //VisualElement[] selectBalls = new VisualElement[puppyQty];
  152. //for (int i = 0; i<puppyQty; i++)
  153. //{
  154. // selectBalls[i] = uiDocument;
  155. // selectElement.Add(selectBalls[i]);
  156. //}
  157. // todo 设置selectBall颜色对应选择
  158. }
  159. void LabelLanguageSetting()
  160. {
  161. // 设置status UI界面里面label和按键的语言显示
  162. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
  163. backButton.text = textValue;
  164. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "cancel", EnviromentSetting.languageCode });
  165. cancelButton.text = textValue;
  166. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
  167. hostLabel.text = textValue;
  168. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
  169. ageLabel.text = textValue;
  170. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
  171. statusLabel.text = textValue;
  172. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "message", "scan_QRcode", EnviromentSetting.languageCode });
  173. transferMsgBasic = textValue;
  174. }
  175. void StatusSummary()
  176. {
  177. // 先清空状态文字,应对切换多只狗
  178. statusValueLabel.text = null;
  179. // 汇总宠物状态
  180. string summary = "";
  181. if (puppy.satiety < 30)
  182. {
  183. if (puppy.satiety < 10)
  184. {
  185. // 小于10,达到L2警告
  186. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
  187. }
  188. else
  189. {
  190. // 不小于10,触发L1警告
  191. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
  192. }
  193. summary += "<br>";
  194. }
  195. if (puppy.stamina < 30)
  196. {
  197. if (puppy.stamina < 10)
  198. {
  199. // 小于10,达到L2警告
  200. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
  201. }
  202. else
  203. {
  204. // 不小于10,触发L1警告
  205. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
  206. }
  207. summary += "<br>";
  208. }
  209. if (puppy.thirsty < 30)
  210. {
  211. if (puppy.thirsty < 10)
  212. {
  213. // 小于10,达到L2警告
  214. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
  215. }
  216. else
  217. {
  218. // 不小于10,触发L1警告
  219. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
  220. }
  221. summary += "<br>";
  222. }
  223. if (puppy.healthy < 30)
  224. {
  225. if (puppy.healthy < 10)
  226. {
  227. // 小于10,达到L2警告
  228. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
  229. }
  230. else
  231. {
  232. // 不小于10,触发L1警告
  233. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
  234. }
  235. summary += "<br>";
  236. }
  237. if (puppy.clean < 30)
  238. {
  239. if (puppy.clean < 10)
  240. {
  241. // 小于10,达到L2警告
  242. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
  243. }
  244. else
  245. {
  246. // 不小于10,触发L1警告
  247. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
  248. }
  249. summary += "<br>";
  250. }
  251. if (puppy.obesity > 70)
  252. {
  253. if (puppy.obesity >90)
  254. {
  255. // 小于10,达到L2警告
  256. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
  257. }
  258. else
  259. {
  260. // 不小于10,触发L1警告
  261. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
  262. }
  263. summary += "<br>";
  264. }
  265. // 如果没有任何异常,返回正常的状态
  266. if (summary.Length == 0)
  267. {
  268. summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
  269. }
  270. statusValueLabel.text = summary;
  271. }
  272. void RightArrowClicked(ClickEvent e)
  273. {
  274. GameData.focusDog++;
  275. if (GameData.focusDog == UserProperty.dogs.Count)
  276. {
  277. GameData.focusDog = 0;
  278. }
  279. }
  280. void LeftArrowClicked(ClickEvent e)
  281. {
  282. GameData.focusDog--;
  283. if (GameData.focusDog == -1)
  284. {
  285. GameData.focusDog = UserProperty.dogs.Count-1;
  286. }
  287. }
  288. void BackBtnClick()
  289. {
  290. var uiPlaceholder = GameObject.Find("UI Placeholder");
  291. if (uiPlaceholder != null)
  292. {
  293. var shoppingUI = uiPlaceholder.transform.Find("Status").gameObject;
  294. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  295. shoppingUI.SetActive(false);
  296. vamUI.SetActive(true);
  297. }
  298. }
  299. void CancelBtnClick()
  300. {
  301. statusElement.style.display = DisplayStyle.Flex;
  302. transferElement.style.display = DisplayStyle.None;
  303. currentPage = StausUIPage.Status;
  304. }
  305. void AddClick(ClickEvent e)
  306. {
  307. // if (UserProperty.level == "basic")
  308. // {
  309. // string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_account_level", EnviromentSetting.languageCode });
  310. // MessageBoxController.ShowMessage(msg);
  311. // }
  312. if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
  313. {
  314. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_no_more", EnviromentSetting.languageCode });
  315. MessageBoxController.ShowMessage(msg);
  316. }
  317. else
  318. {
  319. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_prompt", EnviromentSetting.languageCode });
  320. MessageBoxController.YorN_Message(msg, SwitchAddNewDog);
  321. }
  322. }
  323. // 跳转login 场景添加新的狗
  324. void SwitchAddNewDog()
  325. {
  326. GameData.subScene = "Login_InitDog";
  327. MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
  328. }
  329. void RemoveClick(ClickEvent e) {
  330. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "foster_dog_prompt", EnviromentSetting.languageCode });
  331. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  332. MessageBoxController.YorN_Message(msg, FosterDog);
  333. }
  334. // 点击确认remove后调用寄养的程序
  335. void FosterDog()
  336. {
  337. // 检测用户是否有200金币
  338. if (UserProperty.coin >= 200)
  339. {
  340. // todo 寄养某一只狗,并扣除金币
  341. }
  342. }
  343. // 找回寄养的狗
  344. void CallbackDogClick(ClickEvent e){
  345. }
  346. void TransferClick(ClickEvent e)
  347. {
  348. // TODO 添加代码增加面对面赠送狗的功能
  349. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "transfer_dog_prompt", EnviromentSetting.languageCode });
  350. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  351. MessageBoxController.YorN_Message(msg, TransferDogRequest);
  352. }
  353. void TransferDogRequest()
  354. {
  355. string url = "/api/transfer_dog/receive/";
  356. Dictionary<string, string> formData = new();
  357. WWWForm form = new();
  358. form.AddField("user_id", UserProperty.userId);
  359. form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
  360. StartCoroutine(WebController.PostRequest(url, form, callback: TransferDogCallback));
  361. }
  362. void TransferDogCallback(string json){
  363. // TODO 生成二维码,和120秒倒计时
  364. transferCountStartTime = DateTime.Now;
  365. currentPage = StausUIPage.Transfer;
  366. }
  367. }
  368. enum StausUIPage
  369. {
  370. Status,
  371. Transfer,
  372. }