I'm not sure if async/await are good for this, but it is definitely possible. I've created a small (well, at least I've tried to make it as small as possible) testing environment to illustrate possible approach. Let's start with a concept:
/// <summary>A simple frame-based game engine.</summary>
interface IGameEngine
{
/// <summary>Proceed to next frame.</summary>
void NextFrame();
/// <summary>Await this to schedule action.</summary>
/// <param name="framesToWait">Number of frames to wait.</param>
/// <returns>Awaitable task.</returns>
Task Wait(int framesToWait);
}
This should allow us to write complex game scripts this way:
static class Scripts
{
public static async void AttackPlayer(IGameEngine g)
{
await g.Wait(60);
while(true)
{
await DoSomeLogic(g);
await g.Wait(30);
}
}
private static async Task DoSomeLogic(IGameEngine g)
{
SomethingHappening();
await g.Wait(10);
SomethingElseHappens();
}
private static void ShootAtPlayer()
{
Console.WriteLine("Pew Pew!");
}
private static void SomethingHappening()
{
Console.WriteLine("Something happening!");
}
private static void SomethingElseHappens()
{
Console.WriteLine("SomethingElseHappens!");
}
}
I'm going to use engine this way:
static void Main(string[] args)
{
IGameEngine engine = new GameEngine();
Scripts.AttackPlayer(engine);
while(true)
{
engine.NextFrame();
Thread.Sleep(100);
}
}
Now we can get to implementation part. Of course you can implement a custom awaitable object, but I'll just rely on Task
and TaskCompletionSource<T>
(unfortunately, no non-generic version, so I'll just use TaskCompletionSource<object>
):
class GameEngine : IGameEngine
{
private int _frameCounter;
private Dictionary<int, TaskCompletionSource<object>> _scheduledActions;
public GameEngine()
{
_scheduledActions = new Dictionary<int, TaskCompletionSource<object>>();
}
public void NextFrame()
{
if(_frameCounter == int.MaxValue)
{
_frameCounter = 0;
}
else
{
++_frameCounter;
}
TaskCompletionSource<object> completionSource;
if(_scheduledActions.TryGetValue(_frameCounter, out completionSource))
{
Console.WriteLine("{0}: Current frame: {1}",
Thread.CurrentThread.ManagedThreadId, _frameCounter);
_scheduledActions.Remove(_frameCounter);
completionSource.SetResult(null);
}
else
{
Console.WriteLine("{0}: Current frame: {1}, no events.",
Thread.CurrentThread.ManagedThreadId, _frameCounter);
}
}
public Task Wait(int framesToWait)
{
if(framesToWait < 0)
{
throw new ArgumentOutOfRangeException("framesToWait", "Should be non-negative.");
}
if(framesToWait == 0)
{
return Task.FromResult<object>(null);
}
long scheduledFrame = (long)_frameCounter + (long)framesToWait;
if(scheduledFrame > int.MaxValue)
{
scheduledFrame -= int.MaxValue;
}
TaskCompletionSource<object> completionSource;
if(!_scheduledActions.TryGetValue((int)scheduledFrame, out completionSource))
{
completionSource = new TaskCompletionSource<object>();
_scheduledActions.Add((int)scheduledFrame, completionSource);
}
return completionSource.Task;
}
}
Main ideas:
Wait
method creates a task, which completes when the specified frame is reached.- I keep a dictionary of scheduled task and complete them as soon as required frame is reached.
Update: simplified code by removing unnecessary List<>
.