我正在尝试使用简单的状态机针对他的不同行为构建一个老板敌人。我正在尝试建立一个攻击和等待状态,但我遇到了一些问题。
如何使状态自动切换?理想情况下,我希望老板在指定时间内进行攻击,然后等待(此时他很脆弱)再等一段时间。但等待是我遇到的问题。有什么好的方法可以解决这个问题吗?
编辑:我正在编写的状态机目前看起来像这样:
public class BossStateMachine : MonoBehaviour
{
/* TODO: Figure out WAITING for state changes */
private delegate void State(); //create delegate
private State stateMethod; //create holder for delegate
bool canBeAttacked = false;
public int bossHealth = 5; //takes 5 hits
/*
* States:
* Attacking - boss is attacking, is not vulnerable
* Waiting - boss is not attacking, is vulnerable
*/
// Use this for initialization
void Start ()
{
stateMethod = new State(EnterStateAttacking);
}
// Update is called once per frame
void Update ()
{
}
void FixedUpdate()
{
stateMethod();
}
private void EnterStateAttacking()
{
canBeAttacked=false;
stateMethod = new State(Attacking);
stateMethod(); //start in same frame (comment out to delay)
}
private void Attacking()
{
FireBlast(); //method of attack
}
private void EnterStateWaiting()
{
canBeAttacked=true;
stateMethod = new State(Waiting);
stateMethod();
}
private void Waiting()
{
//wait (open to attack from player)
//do laughing or something
}
}
我想让 Waiting 状态和 Attacking 状态都自己进入和退出。即使通过更新功能,我该怎么做?