StatusController.cs 18 KB

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