Advertisement
Freack100

CoroutineManager

Jul 16th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. --[[
  2. This API is meant to provide a simple but powerfull CoroutineManager which can be used as replacement of the
  3. ]]
  4.  
  5. local currentRoutine = 0;
  6.  
  7. local childEnv = {
  8.     __index = _G;
  9.  
  10.     isCoroutine = true;
  11.     requestCoroutineID = function()
  12.         return currentRoutine
  13.     end;
  14. }
  15.  
  16. local masterEnv = {
  17.    
  18. }
  19.  
  20. childEnv = setmetatable(childEnv,childEnv)
  21.  
  22. local CoroutineManagerTemplate = {
  23.    
  24.     currentRoutines = {};
  25.     activeRoutines = {};
  26.     currentFilter = {};
  27.     masterRoutine = nil;
  28.     isRunning = false;
  29.     firstRun = false;
  30.     lastEvent = {};
  31.  
  32.     addCoroutine = function(self,func)
  33.         self.currentRoutines[(#self.currentRoutines+1) or 1] = coroutine.create(func)
  34.         self.activeRoutines[#self.currentRoutines] = true
  35.         return #self.currentRoutines
  36.     end;
  37.  
  38.     removeRoutine = function(id)
  39.         currentRoutines[id] = nil
  40.     end;
  41.  
  42.     addActiveRoutine = function(id)
  43.         activeRoutines[id] = true
  44.     end;
  45.  
  46.     removeActiveRoutine = function(id)
  47.         activeRoutines[id] = false
  48.     end;
  49.  
  50.     run = function(self,obj)
  51.         self.isRunning = true
  52.         self.firstRun = true
  53.         while self.isRunning do
  54.            
  55.             local event = {}
  56.  
  57.             self.lastEvent = event
  58.  
  59.             if not self.firstRun then
  60.                 coroutine.resume(self.masterRoutine,unpack(event))
  61.             end
  62.  
  63.             if self.firstRun then
  64.                 local _,filter = coroutine.resume(self.masterRoutine, unpack(event))
  65.                 if(filter == "ManagerObject") then
  66.                     coroutine.resume(self.masterRoutine, self)
  67.                 end
  68.                 self.firstRun = false
  69.             end
  70.  
  71.             if coroutine.status(self.masterRoutine) == "dead" then
  72.                 self.isRunning = false
  73.                 error("Master routine died!",0)
  74.             end
  75.  
  76.             for k,v in pairs(self.currentRoutines) do
  77.                 if(self.activeRoutines[k]) then
  78.                     if self.currentFilter[k] == nil or self.currentFilter[k] == event[1] or event[1] == "terminated" then
  79.                         local _, filter = coroutine.resume(v,unpack(event))
  80.                         self.currentFilter[k] = filter
  81.                     end
  82.                 end
  83.             end
  84.             local event = {coroutine.yield()}
  85.         end
  86.     end;
  87.  
  88.     pause = function(self)
  89.         self.isRunning = false
  90.     end;
  91.  
  92.     setMasterRoutine = function(self,func)
  93.         func = func
  94.         self.masterRoutine = coroutine.create(func)
  95.         return self
  96.     end;
  97. }
  98.  
  99. function new(func)
  100.     obj = CoroutineManagerTemplate
  101.     if func then
  102.         obj.setMasterRoutine(func)
  103.     end
  104.     return obj
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement