Last active
August 29, 2015 14:04
-
-
Save kuanyingchou/64b6c62d2a75a2cce6bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
class Time | |
{ | |
public static float deltaTime; | |
} | |
class MonoBehaviour | |
{ | |
public virtual void Start() {} | |
public virtual void Update() {} | |
public IEnumerator WaitForSeconds(float time) | |
{ | |
float elapsed = 0; | |
while(elapsed < time) | |
{ | |
elapsed += Time.deltaTime; | |
yield return null; | |
} | |
} | |
public Coroutine StartCoroutine(IEnumerator i, bool newThread = false) | |
{ | |
Coroutine c = new Coroutine(i); | |
if(newThread) | |
{ | |
CallStack s = new CallStack(); | |
s.Push(c); | |
Engine.threads.Add(s); | |
} | |
return c; | |
} | |
} | |
class MyBehaviour : MonoBehaviour | |
{ | |
public override void Start() | |
{ | |
StartCoroutine(TestCoroutine(), true); | |
} | |
public IEnumerator TestCoroutine() | |
{ | |
Console.WriteLine("I'm waiting for the coroutine..."); | |
yield return StartCoroutine(WaitForSeconds(2000)); | |
Console.WriteLine("finally! the coroutine is done!"); | |
yield return StartCoroutine(WaitForSeconds(2000)); | |
Console.WriteLine("bye bye!"); | |
} | |
} | |
public class Coroutine | |
{ | |
private IEnumerator i; | |
public Coroutine parent; | |
public Coroutine(IEnumerator i) | |
{ | |
this.i = i; | |
} | |
public bool MoveNext() | |
{ | |
return i.MoveNext(); | |
} | |
public object Current {get {return i.Current; }} | |
} | |
class CallStack : IEnumerator | |
{ | |
private List<Coroutine> stack = | |
new List<Coroutine>(); | |
public int Count { get {return stack.Count;} } | |
public Coroutine Peak() | |
{ | |
return stack[stack.Count-1]; | |
} | |
public void Push(Coroutine i) | |
{ | |
stack.Add(i); | |
} | |
public Coroutine Pop() | |
{ | |
Coroutine res = stack[stack.Count-1]; | |
stack.RemoveAt(stack.Count-1); | |
return res; | |
} | |
public bool MoveNext() | |
{ | |
while(Count > 0) | |
{ | |
Coroutine top = Peak(); | |
if(top.MoveNext()) | |
{ | |
if(top.Current is Coroutine) | |
{ | |
Coroutine c = top.Current as Coroutine; | |
c.parent = top; | |
Push(c); | |
} | |
return true; | |
} else | |
{ | |
Pop(); | |
} | |
} | |
return false; | |
} | |
public object Current {get { return null; }} | |
public void Reset() {} | |
} | |
class Engine | |
{ | |
public static List<CallStack> threads = | |
new List<CallStack>(); | |
public static void Main() | |
{ | |
var sw = new Stopwatch(); | |
sw.Start(); | |
float last = sw.ElapsedMilliseconds; | |
MonoBehaviour mb = new MyBehaviour(); | |
mb.Start(); | |
while(true) | |
{ | |
Time.deltaTime = sw.ElapsedMilliseconds - last; | |
//Console.WriteLine("delta: "+deltaTime +", current: "+current); | |
UpdateAll(); | |
last += Time.deltaTime; | |
Thread.Sleep(10); | |
} | |
} | |
public static void UpdateAll() | |
{ | |
for(int i=0; i<threads.Count; i++) | |
{ | |
threads[i].MoveNext(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment