summaryrefslogtreecommitdiff
path: root/Assets/Scripts/UI/UIBinding.cs
blob: 2856393965e5cf4d8c971d53169b9cb94eab24b1 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;

        public GameObject noPlayerJoinedLayer;
        public GameObject player1JoinedLayer;
        public GameObject allPlayerJoinedLayer;

        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;
            
            // 更新玩家加入显示
            if (progress.sceneStatus.current == Status.Waiting)
            {
                var player1Joined = progress.player1.bindPlayer != null;
                var player2Joined = progress.player2.bindPlayer != null;

                if (!player1Joined && !player2Joined)
                {
                    noPlayerJoinedLayer.SetActive(true);
                    player1JoinedLayer.SetActive(false);
                    allPlayerJoinedLayer.SetActive(false);
                    return;
                }
                
                if (player1Joined && !player2Joined)
                {
                    noPlayerJoinedLayer.SetActive(false);
                    player1JoinedLayer.SetActive(true);
                    allPlayerJoinedLayer.SetActive(false);
                    return;
                }
                
                if (player1Joined && player2Joined)
                {
                    noPlayerJoinedLayer.SetActive(false);
                    player1JoinedLayer.SetActive(false);
                    allPlayerJoinedLayer.SetActive(true);
                }
            }
            else
            {
                noPlayerJoinedLayer.SetActive(false);
                player1JoinedLayer.SetActive(false);
                allPlayerJoinedLayer.SetActive(false);
            }
        }
    }
}