| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- $[
- local totalTime_ = 0
- local sleepedCoroutines_ = {}
- local waitEventCoroutines_ = {}
- function coroutine.start(func)
- if func == nil then
- return nil
- end
- local co = coroutine.create(func)
- return coroutine.resume(co)
- end
- function coroutine.sleep(time)
- local co = coroutine.running()
- if co == nil then
- return
- end
- sleepedCoroutines_[co] = totalTime_ + time
- return coroutine.yield(co)
- end
- function coroutine.update(steptime)
- totalTime_ = totalTime_ + steptime
- local coroutines = {}
- for co, wakeupTime in pairs(sleepedCoroutines_) do
- if wakeupTime < totalTime_ then
- table.insert(coroutines, co)
- end
- end
- for _, co in ipairs(coroutines) do
- sleepedCoroutines_[co] = nil
- coroutine.resume(co)
- end
- end
- function coroutine.waitevent(event)
- local co = coroutine.running()
- if co == nil then
- return
- end
- if waitEventCoroutines_[event] == nil then
- waitEventCoroutines_[event] = { co }
- else
- table.insert(waitEventCoroutines_[event], co)
- end
- return coroutine.yield(co)
- end
- function coroutine.sendevent(event)
- local coroutines = waitEventCoroutines_[event]
- if coroutines == nil then
- return
- end
- waitEventCoroutines_[event] = nil
-
- for _, co in ipairs(coroutines) do
- coroutine.resume(co)
- end
- end
- $]
|