123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UIElements;
- /* 这个controller 是用于控制 Status UI菜单的
- * 包含显示狗的最新状态
- * 添加狗,把狗送给朋友,删除狗
- */
- public class StatusController : MonoBehaviour
- {
- private Button backButton, cancelButton;
- private Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel, statusLabel, statusValueLabel;
- private Label leftArrow, rightArrow;
- private Label addLabel, transfer, remove, callback;
- private Label transferMsgLabel;
- private string transferMsgBasic;
- private DateTime transferCountStartTime = new DateTime();
- DogProperty puppy;
- private VisualElement selectElement, statusElement, transferElement;
- private StausUIPage currentPage = StausUIPage.Status;
- // Start is called before the first frame update
- void OnEnable()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- statusElement = root.Q("statusElement");
- transferElement = root.Q("transferElement");
- backButton = root.Q<Button>("back");
- cancelButton = root.Q<Button>("cancel");
- nameLabel = root.Q<Label>("name");
- genderLabel = root.Q<Label>("gender");
- breedLabel = root.Q<Label>("breed");
- hostLabel = root.Q<Label>("host");
- hostnameLabel = root.Q<Label>("hostname");
- ageLabel = root.Q<Label>("age");
- ageValueLabel = root.Q<Label>("ageValue");
- statusLabel = root.Q<Label>("status");
- statusValueLabel = root.Q<Label>("statusValue");
- selectElement = root.Q("selectElement");
- leftArrow = root.Q<Label>("leftArrow");
- rightArrow = root.Q<Label>("rightArrow");
- addLabel = root.Q<Label>("add");
- transfer = root.Q<Label>("transfer");
- remove = root.Q<Label>("remove");
- callback = root.Q<Label>("callback");
- transferMsgLabel = root.Q<Label>("transferMsg");
- // 绑定事件
- backButton.clicked += BackBtnClick;
- cancelButton.clicked += CancelBtnClick;
- leftArrow.RegisterCallback<ClickEvent>(e => LeftArrowClicked(e));
- rightArrow.RegisterCallback<ClickEvent>(e => RightArrowClicked(e));
- addLabel.RegisterCallback<ClickEvent>(e => AddClick(e));
- remove.RegisterCallback<ClickEvent>(e => RemoveClick(e));
- transfer.RegisterCallback<ClickEvent>(e => TransferClick(e));
- // 箭头是否显示
- if (UserProperty.dogs.Count > 1)
- {
- leftArrow.visible = true;
- rightArrow.visible = true;
- }
- else
- {
- leftArrow.visible = false;
- rightArrow.visible = false;
- }
- // 根据用户名下狗的数量判断是否显示transfer按键
- if (UserProperty.dogs.Count > 1)
- {
- transfer.style.display = DisplayStyle.Flex;
- }
- else
- {
- transfer.style.display = DisplayStyle.None;
- }
- // 如果用户狗的数量达到系统规定上限,则隐藏add按键
- if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
- {
- addLabel.style.display = DisplayStyle.None;
- }
- else
- {
- addLabel.style.display = DisplayStyle.Flex;
- }
- }
- //private void Start()
- //{
- //}
- // Update is called once per frame
- void Update()
- {
- // 刷新狗的数据
- // puppy = UserProperty.dogs[GameData.focusDog];
- // StatusPageUpdate();
- // LabelLanguageSetting();
- // StatusSummary();
- if (currentPage == StausUIPage.Transfer)
- {
- TimeSpan ts = DateTime.Now - transferCountStartTime;
- if (ts.TotalSeconds > 120)
- {
- // todo 超过120秒,关闭二维码
- transferElement.style.display = DisplayStyle.None;
- statusElement.style.display = DisplayStyle.Flex;
- currentPage = StausUIPage.Status;
- }
- }
- if (currentPage == StausUIPage.Status)
- {
- // 刷新狗的数据
- puppy = UserProperty.dogs[GameData.focusDog];
- StatusPageUpdate();
- LabelLanguageSetting();
- StatusSummary();
- }
- }
- //void BackPressed()
- //{
- // if (backButton != null)
- // {
- // var root = GetComponent<UIDocument>();
- // Destroy(GameObject.Find(root.GetComponentInParent<Canvas>().name));
- // }
- //}
- void StatusPageUpdate()
- {
- //backButton.clicked += BackPressed;
- nameLabel.text = puppy.dog_name;
- if (puppy.sex == 1)
- {
- genderLabel.text = "♂";
- }
- else
- {
- genderLabel.text = "♀";
- }
- breedLabel.text = puppy.breed;
- foreach (var breed in EnviromentSetting.dogBreeds)
- {
- if (breed.breed == puppy.breed)
- {
- breedLabel.text = breed.name[EnviromentSetting.languageCode];
- break;
- }
- }
- hostnameLabel.text = UserProperty.name;
- // 计算狗生日和现在时间差
- TimeSpan ts = System.DateTime.Now - puppy.brithday;
- ageValueLabel.text = ts.Days.ToString();
- //根据狗的数量添加选择球
- //int puppyQty = UserProperty.dogs.Count;
- //VisualTreeAsset selectBall = Resources.Load<VisualTreeAsset>("Status/SelectBall");
- //// 这里将VisualTreeAsset转换成VisualElement类型
- //VisualElement uiDocument = selectBall.CloneTree();
- //VisualElement[] selectBalls = new VisualElement[puppyQty];
- //for (int i = 0; i<puppyQty; i++)
- //{
- // selectBalls[i] = uiDocument;
- // selectElement.Add(selectBalls[i]);
- //}
- // todo 设置selectBall颜色对应选择
- }
- void LabelLanguageSetting()
- {
- // 设置status UI界面里面label和按键的语言显示
- string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
- backButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "cancel", EnviromentSetting.languageCode });
- cancelButton.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
- hostLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
- ageLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
- statusLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "message", "scan_QRcode", EnviromentSetting.languageCode });
- transferMsgBasic = textValue;
- }
- void StatusSummary()
- {
- // 先清空状态文字,应对切换多只狗
- statusValueLabel.text = null;
- // 汇总宠物状态
- string summary = "";
- if (puppy.satiety < 30)
- {
- if (puppy.satiety < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.stamina < 30)
- {
- if (puppy.stamina < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.thirsty < 30)
- {
- if (puppy.thirsty < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.healthy < 30)
- {
- if (puppy.healthy < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.clean < 30)
- {
- if (puppy.clean < 10)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- if (puppy.obesity > 70)
- {
- if (puppy.obesity > 90)
- {
- // 小于10,达到L2警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
- }
- else
- {
- // 不小于10,触发L1警告
- summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
- }
- summary += "<br>";
- }
- // 如果没有任何异常,返回正常的状态
- if (summary.Length == 0)
- {
- summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
- }
- statusValueLabel.text = summary;
- }
- void RightArrowClicked(ClickEvent e)
- {
- GameData.focusDog++;
- if (GameData.focusDog == UserProperty.dogs.Count)
- {
- GameData.focusDog = 0;
- }
- }
- void LeftArrowClicked(ClickEvent e)
- {
- GameData.focusDog--;
- if (GameData.focusDog == -1)
- {
- GameData.focusDog = UserProperty.dogs.Count - 1;
- }
- }
- void BackBtnClick()
- {
- var uiPlaceholder = GameObject.Find("UI Placeholder");
- if (uiPlaceholder != null)
- {
- var shoppingUI = uiPlaceholder.transform.Find("Status").gameObject;
- var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
- shoppingUI.SetActive(false);
- vamUI.SetActive(true);
- }
- }
- void CancelBtnClick()
- {
- statusElement.style.display = DisplayStyle.Flex;
- transferElement.style.display = DisplayStyle.None;
- currentPage = StausUIPage.Status;
- }
- void AddClick(ClickEvent e)
- {
- // if (UserProperty.level == "basic")
- // {
- // string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_account_level", EnviromentSetting.languageCode });
- // MessageBoxController.ShowMessage(msg);
- // }
- if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
- {
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_no_more", EnviromentSetting.languageCode });
- MessageBoxController.ShowMessage(msg);
- }
- else
- {
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_prompt", EnviromentSetting.languageCode });
- MessageBoxController.YorN_Message(msg, SwitchAddNewDog);
- }
- }
- // 跳转login 场景添加新的狗
- void SwitchAddNewDog()
- {
- GameData.subScene = "Login_InitDog";
- MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
- }
- void RemoveClick(ClickEvent e)
- {
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "foster_dog_prompt", EnviromentSetting.languageCode });
- msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
- MessageBoxController.YorN_Message(msg, FosterDog);
- }
- // 点击确认remove后调用寄养的程序
- void FosterDog()
- {
- // 检测用户是否有200金币
- if (UserProperty.coin >= 200)
- {
- // todo 寄养某一只狗,并扣除金币
- }
- }
- // 找回寄养的狗
- void CallbackDogClick(ClickEvent e)
- {
- }
- void TransferClick(ClickEvent e)
- {
- // TODO 添加代码增加面对面赠送狗的功能
- string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "transfer_dog_prompt", EnviromentSetting.languageCode });
- msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
- MessageBoxController.YorN_Message(msg, TransferDogRequest);
- }
- void TransferDogRequest()
- {
- string url = "/api/transfer_dog/receive/";
- Dictionary<string, string> formData = new();
- WWWForm form = new();
- form.AddField("user_id", UserProperty.userId);
- form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
- StartCoroutine(WebController.PostRequest(url, form, callback: TransferDogCallback));
- }
- void TransferDogCallback(string json)
- {
- // TODO 生成二维码,和120秒倒计时
- transferCountStartTime = DateTime.Now;
- currentPage = StausUIPage.Transfer;
- }
- }
- enum StausUIPage
- {
- Status,
- Transfer,
- }
|