Forráskód Böngészése

Add coroutine.waitevent and coroutine.sendevent.

aster2013 11 éve
szülő
commit
ac63f37ead
1 módosított fájl, 36 hozzáadás és 20 törlés
  1. 36 20
      Source/Engine/LuaScript/pkgs/LuaScript/Coroutine.pkg

+ 36 - 20
Source/Engine/LuaScript/pkgs/LuaScript/Coroutine.pkg

@@ -1,12 +1,9 @@
 $[
 $[
 
 
--- Total time
-coroutine.totaltime = 0
+local totalTime_ = 0
+local sleepedCoroutines_ = {}
+local waitEventCoroutines_ = {}
 
 
--- Suspended coroutines
-coroutine.suspendedcoroutines = {}
-
--- Start a coroutine
 function coroutine.start(func)
 function coroutine.start(func)
     if func == nil then
     if func == nil then
         return nil
         return nil
@@ -16,38 +13,57 @@ function coroutine.start(func)
     return coroutine.resume(co)
     return coroutine.resume(co)
 end
 end
 
 
--- Sleep a coroutine
 function coroutine.sleep(time)
 function coroutine.sleep(time)
-    -- Return running coroutine
     local co = coroutine.running()
     local co = coroutine.running()
     if co == nil then
     if co == nil then
         return
         return
     end
     end
 
 
-    -- Calculate wake up time
-    local wakeuptime = coroutine.totaltime + time
-    coroutine.suspendedcoroutines[co] = wakeuptime
+    sleepedCoroutines_[co] = totalTime_ + time
 
 
-    -- Suspend running coroutine
     return coroutine.yield(co)
     return coroutine.yield(co)
 end
 end
 
 
--- Update suspended coroutines
 function coroutine.update(steptime)
 function coroutine.update(steptime)
-    -- Update total time
-    coroutine.totaltime = coroutine.totaltime + steptime
+    totalTime_ = totalTime_ + steptime
 
 
-    -- Find all coroutines
     local coroutines = {}
     local coroutines = {}
-    for co, wakeuptime in pairs(coroutine.suspendedcoroutines) do
-        if wakeuptime < coroutine.totaltime then
+    for co, wakeupTime in pairs(sleepedCoroutines_) do
+        if wakeupTime < totalTime_ then
             table.insert(coroutines, co)
             table.insert(coroutines, co)
         end
         end
     end
     end
 
 
-    -- Resume all coroutines
     for _, co in ipairs(coroutines) do
     for _, co in ipairs(coroutines) do
-        coroutine.suspendedcoroutines[co] = nil
+        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)
         coroutine.resume(co)
     end
     end
 end
 end