123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using UnityEngine;
- using UnityEngine.UIElements;
- using ZXing;
- /* * WalkDogsScoreController.cs
- * This script is responsible for managing the score in the Walk Dogs game.
- * It will handle score updates, display, and any related functionality.
- */
- public class WalkDogsScoreController : MonoBehaviour
- {
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- public int score, maxCombo, perfect, good, poor, miss;
- private int coin;
- private Label scoreLabel, maxComboLabel, perfectLabel, goodLabel, poorLabel, missLabel, coinLabel;
- private Label scoreValueLabel, maxComboValueLabel, perfectValueLabel, goodValueLabel, poorValueLabel, missValueLabel, coinValueLabel;
- private Button confirmButton;
- void OnEnable()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- root.style.backgroundColor = new Color(255, 255, 255, 0.25f); // Set background color to white with 50% opacity
- scoreLabel = root.Q<Label>("scoreLabel");
- maxComboLabel = root.Q<Label>("maxComboLabel");
- perfectLabel = root.Q<Label>("perfectLabel");
- goodLabel = root.Q<Label>("goodLabel");
- poorLabel = root.Q<Label>("poorLabel");
- missLabel = root.Q<Label>("missLabel");
- coinLabel = root.Q<Label>("coinLabel");
- confirmButton = root.Q<Button>("confirm");
- scoreValueLabel = root.Q<Label>("scoreValue");
- maxComboValueLabel = root.Q<Label>("maxComboValue");
- perfectValueLabel = root.Q<Label>("perfectValue");
- goodValueLabel = root.Q<Label>("goodValue");
- poorValueLabel = root.Q<Label>("poorValue");
- missValueLabel = root.Q<Label>("missValue");
- coinValueLabel = root.Q<Label>("coinValue");
- confirmButton = root.Q<Button>("confirm");
- // confirmButton.clicked += ScoreSubmitRequest;
- confirmButton.clicked += ConfirmButtonClick;
- LabelLanguageLoading();
- }
- // Update is called once per frame
- void Update()
- {
- // ScoreDisplay();
- }
- // 读取显示正确的语言
- void LabelLanguageLoading()
- {
- string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "score", EnviromentSetting.languageCode });
- scoreLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "maxCombo", EnviromentSetting.languageCode });
- maxComboLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "perfect", EnviromentSetting.languageCode });
- perfectLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "good", EnviromentSetting.languageCode });
- goodLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "poor", EnviromentSetting.languageCode });
- poorLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "miss", EnviromentSetting.languageCode });
- missLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "coin", EnviromentSetting.languageCode });
- coinLabel.text = textValue;
- textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "button", "confirm", EnviromentSetting.languageCode });
- confirmButton.text = textValue;
- }
- public void ShowScore()
- {
- coin = Mathf.FloorToInt(score / 1000f);
- // Display the score and other statistics in the UI
- scoreValueLabel.text = score.ToString();
- maxComboValueLabel.text = maxCombo.ToString();
- perfectValueLabel.text = perfect.ToString();
- goodValueLabel.text = good.ToString();
- poorValueLabel.text = poor.ToString();
- missValueLabel.text = miss.ToString();
- coinValueLabel.text = coin.ToString();
- // Optionally, you can also log the score to the console for debugging
- Debug.Log($"Score: {score}, Max Combo: {maxCombo}, Perfect: {perfect}, Good: {good}, Poor: {poor}, Miss: {miss}, Coin: {coin}");
- }
- // This method should be called when the score is ready to be displayed
- private void ConfirmButtonClick()
- {
- // MaskTransitions.TransitionManager.Instance.LoadLevel("Home");
- ScoreSubmitRequest();
- }
- private void ScoreSubmitRequest()
- {
- Debug.Log("GetUserData request");
- // 提交POST
- string url = "/api/walkdogs/score/";
- WWWForm form = new();
- form.AddField("user_id", UserProperty.userId);
- form.AddField("score", score);
- form.AddField("max_combo", maxCombo);
- form.AddField("perfect", perfect);
- form.AddField("good", good);
- form.AddField("poor", poor);
- form.AddField("miss", miss);
- form.AddField("coin", coin);
- StartCoroutine(WebController.PostRequest(url, form, callback: ScoreSubmitCallback));
- }
- private void ScoreSubmitCallback(string response)
- {
- var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
- if (data != null && data["status"] == "success")
- {
- string user_info = data["user_info"].ToString();
- UserProperty.RefreshUserInfo(user_info);
- string dogs = data["dogs"].ToString();
- UserProperty.RefreshDogInfo(dogs);
- MaskTransitions.TransitionManager.Instance.LoadLevel("Home");
- }
- }
- }
|