2

我儿子正在编写一个简单的 RPG 游戏,其中包含许多非玩家角色(又名 NPC)。每个 NPC 都有一个关联的“脚本”来控制其行为。我们将使用一种迷你自定义脚本语言来编写这些行为,但我现在想知道在 C#5/Async 中这样做是否会更好。

举一个非常简单的例子,假设一个 NPC 只是在两点之间行走,我认为写这样的东西会很好:

while (true)
{
    await WalkTo(100,100);
    await WalkTo(200,200);
}

WalkTo 方法将是一个异步方法,它处理与在两点之间行走有关的所有事情,并在游戏循环中的多个帧上执行此操作。它不是可以卸载到后台线程的阻塞方法。

这就是我卡住的地方......我无法找到任何以这种方式使用 async/await 的示例,但它似乎非常适合它。

想法?


这是我想做的一些非常粗略的伪代码:

class NpcBase
{

    // Called from game loop
    public void onUpdate(double elapsedTime)
    {
        // Move the NPC
        .
        .
        .


        // Arrived at destination?
        if (Arrived)
        {
            // How do I trigger that the task is finished?
            _currentTask.MarkComplete();        
        }

    }


    // Async method called by NPC "script"
    public async Task WalkTo(int x, int y)
    {
        // Store new target location


        // return a task object that will be "triggered" when the walk is finished
        _currentTask = <something??>
        return _currentTask;
    }

    Task _currentTask;

}
4

2 回答 2

4

好的,听起来一个选择是TaskCompletionSource为游戏的每一帧设置一个。然后,您可以等待Taskfrom WalkTo,并将结果设置为OnUpdate

private TaskCompletionSource<double> currentFrameSource;

// Called from game loop
public void OnUpdate(double elapsedTime)
{
    ...
    var previousFrameSource = currentFrameSource;
    currentFrameSource = new TaskCompletionSource<double>();
    // This will trigger all the continuations...
    previousFrameSource.SetResult(elapsedTime);
}

// Async method called by NPC "script"
public async Task WalkTo(int x, int y)
{
    // Store new target location
    while (/* we're not there yet */)
    {
        double currentTime = await currentFrameSource.Task;
        // Move
    }
}

诚然,我不确定这会有多有效……但它应该可以工作。

于 2013-02-19T09:59:34.863 回答
0

我想我已经在一个简单的测试程序中弄清楚了

首先,我有一个 NPC 的基类,如下所示:

编辑:更新 NpcBase 以使用 TaskCompletionSource:

public class NpcBase
{
    // Derived classes to call this when starting an async operation
    public Task BeginTask()
    {
        // Task already running?
        if (_tcs!= null)
        {
            throw new InvalidOperationException("busy");
        }

        _tcs = new TaskCompletionSource<int>();

        return _tcs.Task;
    }

    TaskCompletionSource<int> _tcs;

    // Derived class calls this when async operation complete
    public void EndTask()
    {
        if (_tcs != null)
        {
            var temp = _tcs;
            _tcs = null;
            temp.SetResult(0);
        }
    }

    // Is this NPC currently busy?
    public bool IsBusy
    {
        get
        {
            return _tcs != null;
        }
    }

}

作为参考,这里是带有自定义 IAsyncResult 实现而不是 TaskCompletionSource 的旧版本 NpcBase:

// DONT USE THIS, OLD VERSION FOR REFERENCE ONLY
public class NpcBase
{
    // Derived classes to call this when starting an async operation
    public Task BeginTask()
    {
        // Task already running?
        if (_result != null)
        {
            throw new InvalidOperationException("busy");
        }

        // Create the async Task
        return Task.Factory.FromAsync(
            // begin method
            (ac, o) =>
            {
                return _result = new Result(ac, o);
            },

            // End method
            (r) =>
            {

            },

            // State object
            null

            );

    }

    // Derived class calls this when async operation complete
    public void EndTask()
    {
        if (_result != null)
        {
            var temp = _result;
            _result = null;
            temp.Finish();
        }
    }

    // Is this NPC currently busy?
    public bool IsBusy
    {
        get
        {
            return _result != null;
        }
    }

    // Result object for the current task
    private Result _result;

    // Simple AsyncResult class that stores the callback and the state object
    class Result : IAsyncResult
    {
        public Result(AsyncCallback callback, object AsyncState)
        {
            _callback = callback;
            _state = AsyncState;
        }


        private AsyncCallback _callback;
        private object _state;

        public object AsyncState
        {
            get { return _state; ; }
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return _finished; }
        }

        public void Finish()
        {
            _finished = true;
            if (_callback != null)
                _callback(this);
        }

        bool _finished;
    }
}

接下来,我有一个简单的“NPC”,它在一维中移动。当 moveTo 操作开始时,它会调用 NpcBase 中的 BeginTask。当到达目的地时,它调用 EndTask()。

public class NpcTest : NpcBase
{
    public NpcTest()
    {
        _position = 0;
        _target = 0;
    }

    // Async operation to count
    public Task MoveTo(int newPosition)
    {
        // Store new target
        _target = newPosition;
        return BeginTask();
    }

    public int Position
    {
        get
        {
            return _position;
        }
    }

    public void onFrame()
    {
        if (_position == _target)
        {
            EndTask();
        }
        else if (_position < _target)
        {
            _position++;
        }
        else
        {
            _position--;
        }
    }

    private int _position;
    private int _target;
}

最后,一个简单的 WinForms 应用程序来驱动它。它由一个按钮和两个标签组成。单击按钮会启动两个 NPC,并且它们的位置会显示在标签上。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void onButtonClick(object sender, EventArgs e)
    {
        RunNpc1();
        RunNpc2();
    }

    public async void RunNpc1()
    {
        while (true)
        {
            await _npc1.MoveTo(20);
            await _npc1.MoveTo(10);
        }
    }

    public async void RunNpc2()
    {
        while (true)
        {
            await _npc2.MoveTo(80);
            await _npc2.MoveTo(70);
        }
    }


    NpcTest _npc1 = new NpcTest();
    NpcTest _npc2 = new NpcTest();

    private void timer1_Tick(object sender, EventArgs e)
    {
        _npc1.onFrame();
        _npc2.onFrame();

        label1.Text = _npc1.Position.ToString();
        label2.Text = _npc2.Position.ToString();
    }

}

它有效,所有似乎都在主 UI 线程上运行......这就是我想要的。

当然,它需要修复以处理取消操作、异常等……但基本思想就在那里。

于 2013-02-19T13:01:04.647 回答