敌人的团结回合基础脚本

本文关键字:脚本 敌人 | 更新日期: 2023-09-27 18:34:54

我有一个敌人列表。 所以我希望每个敌人都有轮到他们。首先:玩家回合 -->敌人回合("在这里每个敌人一个接一个地移动,直到最后,然后玩家再次移动"(。我如何在这里腾出一些等待时间,并在敌人回合时强迫库斯?任何帮助将不胜感激。

void Start()
{
     // find list enemy
    enemy = GameObject.FindGameObjectsWithTag("Enemy");
}
void Update()
{
    //enemy turn reference to player. after move all enemy we change it to false to change the player turn.
    if(StaticClass.enemyTurn == true )
    {
       for(int i=0;i<enemy.length;i++)
        {
           // how do i making some waiting time here and forcus on enemy turn?
           EnemyTurn(i);
        }
    }
}

 public void EnemyTurn(int id)
{
    ChessMoveMent chessMoveScript = enemy[id].GetComponent<ChessMoveMent>();
    chessMoveScript.ProcessMove();
    id++;
    if(id>=enemy.Length)
    {
        isMove = false;
    }
}

敌人的团结回合基础脚本

在这种情况下

,我通常使用StartCoroutine。请尝试以下代码:

public IEnumerator EnemyTurn(int id)
{
  yield return null;
  ChessMoveMent chessMoveScript = enemy[id].GetComponent<ChessMoveMent>();
  chessMoveScript.ProcessMove();
  id++;
  if(id>=enemy.Length)
  {
    isMove = false;
  }
}

当你想使用它时,请使用"StartCoroutine((">

StartCoroutine(EnemyTurn(i));

更多细节在这里

你可能有一个协调员,他告诉参与者什么时候轮到他们。

public class GameCoordinator : MonoBehaviour
{
    public List<Participant> participants;
    private int currentParticipantIdx = -1;
    private Participant CurrentParticipant
    {
        get { return participants[currentParticipantIdx]; }
    }
    private void Start()
    {
        PlayNextMove();
    }
    private void PlayNextMove()
    {
        ProceedToNextParticipant();
        CurrentParticipant.OnMoveCompleted += OnMoveCompleted;
        CurrentParticipant.BeginMove();
    }
    private void OnMoveCompleted()
    {
        CurrentParticipant.OnMoveCompleted -= OnMoveCompleted;
        StartCoroutine(PlayNextMoveIn(2.0f));
    }
    private IEnumerator PlayNextMoveIn(float countdown)
    {
        yield return new WaitForSeconds(countdown);
        PlayNextMove();
    }
    private void ProceedToNextParticipant()
    {
        ++currentParticipantIdx;
        if (currentParticipantIdx == participants.Count)
            currentParticipantIdx = 0;
    }
}
public class Participant : MonoBehaviour
{
    public event Action OnMoveCompleted;
    public void BeginMove()
    {
        //
    }
}