1 module Engine.Coroutine; 2 3 import core.thread; 4 import std.datetime; 5 import Engine.Entity; 6 7 Coroutine[] Coroutines; 8 9 void StartCoroutine(void delegate() dg) { 10 new Coroutine(null, dg); 11 } 12 13 void StartCoroutine(void function() fn) { 14 new Coroutine(null, fn); 15 } 16 17 package void RunCoroutines() { 18 for (int i=0;i<Coroutines.length;i++) { 19 auto co = Coroutines[i]; 20 if (co.state == Fiber.State.TERM) { 21 Coroutines[i] = Coroutines[Coroutines.length-1]; 22 Coroutines.length--; 23 i--; 24 continue; 25 } 26 co.call(); 27 } 28 } 29 30 class Coroutine : Fiber 31 { 32 Entity entity; 33 34 package this(Entity entity, void delegate() dg) { 35 this.entity = entity; 36 super(dg); 37 Coroutines ~= this; 38 } 39 40 package this(Entity entity, void function() fn) { 41 this.entity = entity; 42 super(fn); 43 Coroutines ~= this; 44 } 45 46 public static void wait(float sec) { 47 StopWatch sw; 48 sw.start(); 49 auto msecs = sec*1000; 50 while (sw.peek().msecs < msecs) { 51 Fiber.yield(); 52 } 53 } 54 55 public static void yield() { 56 Fiber.yield(); 57 } 58 } 59