123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- 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;
- private Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel, statusLabel, statusValueLabel;
- private Label leftArrow, rightArrow;
- private Label addLabel, transfer, remove, callback;
- DogProperty puppy;
- private VisualElement selectElement;
- // Start is called before the first frame update
- void OnEnable()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- backButton = root.Q<Button>("back");
- 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");
- // 绑定事件
- backButton.clicked += BackBtnClick;
- 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;
- }
- }
- //private void Start()
- //{
-
- //}
- // Update is called once per frame
- void Update()
- {
- // 刷新狗的数据
- 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;
- 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", "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;
- }
- 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 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);
- }
- else 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";
- SceneManager.LoadScene("Home");
- }
- 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 添加代码增加面对面赠送狗的功能
- }
- }
|