summaryrefslogtreecommitdiff
path: root/Assets/Scripts/UI/UIBinding.cs
blob: 9830b037aeb60f0b6eade230ccbc88ac2ddbeb8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class UIBinding : MonoBehaviour
    {
        public GameProgressManager progress;
        [Range(0, 1)] public float player1ScorePercent;
        [Range(0, 1)] public float player2ScorePercent;
        
        public TMP_Text timeText;
        public TMP_Text player1ScoreText;
        public TMP_Text player2ScoreText;
        public TMP_Text questionText;
        public Image player1ScoreDisplay;
        public Image player2ScoreDisplay;

        private void FixedUpdate()
        {
            // 更新时间文本
            timeText.text = progress.time.ToString();
            
            // 更新百分比
            player1ScorePercent = Mathf.Clamp(progress.player1Score, 0, Single.MaxValue) * 1.0f / progress.questionMaxScore * 1.0f;
            player2ScorePercent = Mathf.Clamp(progress.player2Score, 0, Single.MaxValue) * 1.0f / progress.questionMaxScore * 1.0f;
            var targetPlayer1Amount = Mathf.Lerp(0.25f, 0.5f, player1ScorePercent);
            var targetPlayer2Amount = Mathf.Lerp(0.25f, 0.5f, player2ScorePercent);
            
            // 更新进度条
            player1ScoreDisplay.fillAmount = targetPlayer1Amount;
            player2ScoreDisplay.fillAmount = targetPlayer2Amount;
            
            // 更新玩家分数显示
            player1ScoreText.text = progress.player1Score.ToString();
            player2ScoreText.text = progress.player2Score.ToString();
            
            // 更新问题显示
            questionText.text = progress.questionPlayer.currentQuestion.QuestionText;
        }
    }
}