基于Turn的游戏在Unity中统一C#

时间:2016-01-09 05:58:27

标签: c# unity3d

美好的一天!我有这个代码,但我有一个错误,例如(我设置了两个玩家,和一台电脑)。我进行第一次转弯,骰子重新生成值为4(仅举例),当我触摸屏幕时,游戏块从第1块移动到第4块,当计算机转动时,它也从第1块移动到第4块(因为我将结果设置为4只是一个例子)。现在轮到我了,骰子再也没有重生,它不等待触摸屏幕if (Input.GetMouseButtonDown(0))并再次移动4 ...

public class singlePlay : MonoBehaviour {
    //Player 
    public GameObject[] playerprefab;

    //Player Clone
    public GameObject[] playerprefabC;

    //Game Cards and Dice
    public GameObject[] situationCard;
    public GameObject dice;
    int diceresult;

    //Game Cards and Dice clone
    public GameObject diceclone;

    public int currentPlayer;
    public int compPlayer;
    public int playerTurn;
    public string compPlayerstring;
    public string playerTurnstring;

    //GUI Boolean
    bool play = false;

    //Game Boolean
    bool pieces = false;
    bool giveturn = false;
    bool myturn = false;
    bool diceSpawn = false;
    bool moving = false;
    bool routine = false;
    bool checking = false;  
    bool compturn = false;
    //icon1
    public GameObject[] icon;

    //population
    int[] population = new int[3];

    //Tile
    public GameObject[] Tile;
    int[] playerTile = new int[3]; //current location
    int[] playerTileUp = new int [3]; // updated location after dice roll

    bool endTurn = false;

    void Update () 
    {
        if (giveturn == true) {
            int h = 0;
            Giveturn(h);
            giveturn = false;
        }

        if (play == true) {

            if (pieces == true){
                SpawnPlayer();
                pieces = false;
            }

            if (myturn == true){
                compturn = false;
                if(diceSpawn == true) {
                    dice.transform.position = new Vector3(0,0,-1);
                    diceclone = Instantiate(dice, dice.transform.position, Quaternion.identity) as GameObject;
                    diceSpawn = false;
                }
                if (Input.GetMouseButtonDown(0))
                {
                    Debug.Log("click");
                    diceresult = 4;
                    Destroy(diceclone);
                    moving = true;
                    Updateposition(diceresult);
                }
            }
            else
            {
                Debug.Log("comp");
                myturn = false;
                diceresult = 4;
                moving = true;
                Updateposition(diceresult);
            }
        }
    }

    void Giveturn(int k) 
    {
        Debug.Log("" + k);
        currentPlayer = k;
        if (k == playerTurn) {
            Debug.Log("Yes");
            compturn = false;
            myturn = true;
            diceSpawn = true;
            moving = false;
        }
        else 
        {
            Debug.Log("No");
            compturn = true;
            myturn = false;
            moving = false;
        }
    }

    void Updateposition(int diceresult) 
    {
        if (moving == true) {
            playerTileUp[currentPlayer] = playerTile[currentPlayer] + diceresult;
            Debug.Log("" + playerTileUp[currentPlayer]+ " " +currentPlayer);
            routine = true;
            StartCoroutine(MyMethod()); 
        }
        moving = false;
    }

    IEnumerator MyMethod() 
    {
        if (routine == true) {
            if (myturn == true) {
                compturn = false;
            }
            else
            {
                myturn = false;
            }

            int f = playerTile[currentPlayer] + 1;
            Debug.Log(" " + currentPlayer );

            while (f <= playerTileUp[currentPlayer]) {
                Debug.Log("waiting");
                yield return new WaitForSeconds(1);
                Debug.Log(" " + Tile[f]);
                playerprefabC[currentPlayer].transform.position =  Tile[f].transform.position;
                Debug.Log(" " + currentPlayer);
                f++;
            }
            checking = true;
            TrapCheck();
        }
        routine = false;
    }

    void TrapCheck()
    {
        if (checking == true) {
            if (playerTileUp[currentPlayer] == 8) {
                Debug.Log("Trap spawning");
                Instantiate(situationCard[0], situationCard[0].transform.position, Quaternion.identity);
                population[currentPlayer] = population[currentPlayer] -1;
            }

            playerTile[currentPlayer] = playerTileUp[currentPlayer];
            Endturn();
            myturn = false;
            compturn = false;
            checking = false;
        }
    }

    void Endturn()
    {
        currentPlayer++;
        Debug.Log(" " + currentPlayer);
        if (currentPlayer > compPlayer) {
            currentPlayer = 0;
        }
        Giveturn(currentPlayer);
    }   
}

1 个答案:

答案 0 :(得分:0)

我已经看不到有什么问题了。首先,当协同程序正在运行时,似乎你没有阻止更新运行,因为游戏仍然是正确的。在TrapCheck中,你调用EndTurn调用GiveTurn并设置myTurn(true)和compTurn(false)布尔值。但是这两个在TrapCheck中被重置,myTurn被设置为false。你需要重新思考你班级的逻辑。

解决方案是使用委托。这将删除您设置和重置的许多布尔值。这是一个基本想法:

Action currentUpdate;
bool playerTurn = true;
void Start(){
    SetTurn();
}
void Update(){
    if(currentUpdate != null)currentUpdate();
}
void SetTurn(){
    // Prepare initial setting for moving
    if(playerTurn == true){ currentUpdate = PlayerTurn; } 
    else{ currentUpdate = CompTurn; } 
    playerTurn = !playerTurn;
}

void PlayerTurn(){
    // Check input
    // Get dice value
    currentUpdate = Move;
}

void CompTurn(){
    // Get dice value
    currentUpdate = Move;
}

void Move(){
    if(position != target){

    }else{
        SetTurn();
    }
}

这是相当简化的,但是一旦你得到关于委托的事情(也许你已经知道),这将使它变得更加灵活。