三个脚本 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的大小