飛行棋代碼

三個腳本 GameControl Dice 和 FollowThePath

首先是FollowThePath腳本

   public class FollowThePath : MonoBehaviour {
    public Transform[] wayPoints;

    [SerializeField]
    private float moveSpeed = 1f;

    [HideInInspector]
    public int waypointIndex = 0;

    public bool moveAllowed = false;
	// Use this for initialization
	void Start () {
        transform.position = wayPoints[waypointIndex].transform.position;
	}
	

	void Update () {
		if(moveAllowed)
        {
            Move();
        }
	}

    private void Move()
    {
        if(waypointIndex<=wayPoints.Length-1)
        {
            transform.position = Vector2.MoveTowards(transform.position, wayPoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
        }
        if(transform.position==wayPoints[waypointIndex].transform.position)
        {
            waypointIndex += 1;
        }
    }
}
這個腳本很有意思的一點在於他根本沒有管骰子到底擲到幾,他在移動上有兩個控制條件
第一個條件是如果index小於或者等於總點長度-1的話,意思是還沒到還沒到終點那麼就一直向前,
第二個條件是如果player的當前位置等於給定index當前點的位置,那麼就讓waypointindex+1
當然這個move條件的執行在於 moveAllowed這個bool值的變量

然後看GameControl腳本:

public class GameControl : MonoBehaviour {
    public static GameControl GameControlInstance;
    public   GameObject whoWinsTextShadow;
    public   GameObject player1MoveText;
    public   GameObject player2MoveText;

    public  GameObject player1;
    public  GameObject player2;
    
    //投擲點數和後面的Dice有關
    public  int diceSideThrown = 0;
    
    //起始點
    public  int player1StartWayPoint = 0;
    public  int player2StartWayPoint = 0;

    public   bool gameOver = false;

    void Awake()
    {
        GameControlInstance = this;
    }

    void Start () {

        player1.GetComponent<FollowThePath>().moveAllowed = false;
        player2.GetComponent<FollowThePath>().moveAllowed = false;
        whoWinsTextShadow.SetActive(false);
        player1MoveText.SetActive(false);
        player2MoveText.SetActive(false);         
    }
	

	void Update () {
		//規則
		//以下兩條if是控制是否移動的
        if(player1.GetComponent<FollowThePath>().waypointIndex>player1StartWayPoint+diceSideThrown)
        {
            player1.GetComponent<FollowThePath>().moveAllowed = false;
            player1MoveText.SetActive(false);
            player2MoveText.SetActive(true);
            player1StartWayPoint = player1.GetComponent<FollowThePath>().waypointIndex - 1;
        }
        if (player2.GetComponent<FollowThePath>().waypointIndex > player2StartWayPoint + diceSideThrown)
        {
            player2.GetComponent<FollowThePath>().moveAllowed = false;
            player2MoveText.SetActive(false);
            player1MoveText.SetActive(true);
            player2StartWayPoint = player2.GetComponent<FollowThePath>().waypointIndex - 1;
        }
        //以下兩條if是成功的條件
        if(player1.GetComponent<FollowThePath>().waypointIndex==player1.GetComponent<FollowThePath>().wayPoints.Length)
        {
            whoWinsTextShadow.SetActive(true);
            player1MoveText.SetActive(false);
            player2MoveText.SetActive(false);
            whoWinsTextShadow.GetComponent<Text>().text = "Player 1 Wins";
            gameOver = true;
        }
        if (player2.GetComponent<FollowThePath>().waypointIndex == player2.GetComponent<FollowThePath>().wayPoints.Length)
        {
            whoWinsTextShadow.SetActive(true);
            player1MoveText.SetActive(false);
            player2MoveText.SetActive(false);
            whoWinsTextShadow.GetComponent<Text>().text = "Player 2 Wins";
            gameOver = true;
        }
    }
    public  void MovePlayer(int playerToMove)
    {
        switch(playerToMove)
        {
            case 1:
                player1.GetComponent<FollowThePath>().moveAllowed = true;
                break;

            case 2:
                player2.GetComponent<FollowThePath>().moveAllowed = true;
                break;

        }
    }
}
移動的腳本是在這裏控制moveAllowed的變量

然後是最後一個腳本Dice腳本

public class Dice : MonoBehaviour {

    public Sprite[] diceSides;
    private SpriteRenderer rend;
    private int whosTurn = 1;
    private bool coroutineAllowed = true;

	void Start () {
        rend = GetComponent<SpriteRenderer>();
        diceSides = Resources.LoadAll<Sprite>("DiceSides/");
        rend.sprite = diceSides[5];
	}
	
	void Update () {
		
	}

    private void OnMouseDown()
    {

        if (!GameControl.GameControlInstance.gameOver && coroutineAllowed)
            StartCoroutine("RollTheDice");
    }
    private IEnumerator RollTheDice()
    {
        coroutineAllowed = false;
        int randomDiceSide = 0;
        for (int i = 0; i <=20; i++)
        {
            randomDiceSide = Random.Range(0, 6);
            rend.sprite = diceSides[randomDiceSide];
            yield return new WaitForSeconds(0.05f);
        }

        GameControl.GameControlInstance.diceSideThrown = randomDiceSide + 1;
        if(whosTurn==1)
        {
            GameControl.GameControlInstance.MovePlayer(1);
        }
        else if(whosTurn==-1)
        {
            GameControl.GameControlInstance.MovePlayer(2);
        }
        whosTurn *= -1;
        coroutineAllowed = true;
    }    
}
在這裏控制人物先後運動 然後控制index的大小
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章