0

我正在尝试使用简单的状态机针对他的不同行为构建一个老板敌人。我正在尝试建立一个攻击和等待状态,但我遇到了一些问题。

如何使状态自动切换?理想情况下,我希望老板在指定时间内进行攻击,然后等待(此时他很脆弱)再等一段时间。但等待是我遇到的问题。有什么好的方法可以解决这个问题吗?

编辑:我正在编写的状态机目前看起来像这样:

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 状态都自己进入和退出。即使通过更新功能,我该怎么做?

4

1 回答 1

1

以最简单的形式,您可以让它在状态之间来回切换.. 像这样:

private TimeSpan timeToWait = TimeSpan.FromMilliseconds(5000); // 5 seconds
private TimeSpan lastStateCheck;

public void Update(GameTime gameTime) {
    if (lastStateCheck + timeToWait < gameTime.TotalGameTime) {
        // switch states after 5 seconds..
        /* Switch state code here */
        lastStateCheck = gameTime.TotalGameTime;
    }
}

更高级的 AI 会执行更多检查,例如是否已经看到玩家,或者如果玩家直接在它面前,它是否应该继续攻击。

PS:我没有投票关闭(我想我应该提一下)

于 2013-04-03T05:17:21.637 回答