summaryrefslogtreecommitdiff
path: root/Assets/Scripts/GameProgressManager.cs
blob: f4d16452bbe4dc48ccfc2062c54c732857c352cb (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
using System.Collections;
using Tag;
using UnityEngine;

[RequireComponent(typeof(SceneStatus))]
public class GameProgressManager : MonoBehaviour
{
    public int time = 60;
    
    public MaskAnswers player1;
    public MaskAnswers player2;

    public int questionMaxScore;
    public int player1Score;
    public int player2Score;

    public QuestionPlayer questionPlayer;
    public SceneStatus sceneStatus;

    private void Awake()
    {
        sceneStatus = GetComponent<SceneStatus>();
    }

    public void Refresh()
    {
        if (player1.bindPlayer != null &&
            player2.bindPlayer != null &&
            SceneStatus.Instance.current == Status.Waiting)
        {
            // 开始游戏
            sceneStatus.current = Status.Playing;
            OnGameStart();
        }
    }

    private void OnGameStart()
    {
        Debug.Log("开始游戏!");
        player1.bindPlayer.GetComponent<PlayerMovement>().enabled = true;
        player2.bindPlayer.GetComponent<PlayerMovement>().enabled = true;

        // 记录最大分数
        questionMaxScore = questionPlayer.currentQuestion.Need.Count;
        
        StartCoroutine(GameTime());
    }

    private void OnGameEnd()
    {
        Debug.Log("游戏结束!");
    }

    IEnumerator GameTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.2f);
            UpdateScore();
            yield return new WaitForSeconds(0.4f);
            UpdateScore();
            yield return new WaitForSeconds(0.2f);
            time--;
            if (time <= 0)
                break;
        }
        OnGameEnd();
    }

    private void UpdateScore()
    {
        player1Score = QuestionVerifier.Verify(questionPlayer.currentQuestion, player1.CollectToAnswer());
        player2Score = QuestionVerifier.Verify(questionPlayer.currentQuestion, player2.CollectToAnswer());
    }
}