Guest User

Untitled

a guest
Jan 27th, 2015
11
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Statemanager by Averice
  2. class "CStateManager";
  3.  
  4. function CStateManager:Init()
  5.     self.States = {}
  6. end
  7.  
  8. function CStateManager:Push(state, init)
  9.     if( ctype(state) == "CState" ) then
  10.         self.States[#self.States+1] = state;
  11.         if( init ) then
  12.             if( self.States[#self.States-1] and self.States[#self.States-1].isinit ) then
  13.                 self.States[#self.States-1]:Shutdown();
  14.                 self.States[#self.States-1].isinit = false;
  15.             end
  16.             self.States[#self.States]:Init(App.context);
  17.             self.States[#self.States].isinit = true;
  18.         end
  19.     else
  20.         print("StateManager: CStateManager.Push expected CState got: "..ctype(state));
  21.     end
  22. end
  23.  
  24. function CStateManager:InitCurrentState()
  25.     if( self.States[1] and not self.States[#self.States].isinit ) then
  26.         self.States[#self.States]:Init(App.context);
  27.         self.States[#self.States].isinit = true;
  28.     end
  29. end
  30.  
  31. function CStateManager:Pop()
  32.     if( self.States[1] ) then
  33.         if( self.States[#self.States].isinit ) then
  34.             self.States[#self.States].isinit = false;
  35.             self.States[#self.States]:Shutdown();
  36.         end
  37.         local oldState = self.States[#self.States];
  38.         self.States[#self.States] = nil;
  39.         self:InitCurrentState();
  40.         return oldState;
  41.     end
  42.     print("StateManager: Called CStateManager.Pop with empty stack");
  43. end
  44.  
  45. function CStateManager:GetAll()
  46.     return self.States
  47. end
  48.  
  49. function CStateManager:GetActive()
  50.     if( self.States[1] and self.States[#self.States].isinit ) then
  51.         return self.States[#self.States];
  52.     end
  53.     print("StateManager: Called CStateManager.GetActive with no running states");
  54. end
  55.  
  56. function CStateManager:Pause(state)
  57.     if( ctype(state) == "CState" ) then
  58.         state.paused = true;
  59.     end
  60. end
  61.  
  62. function CStateManager:Resume(state)
  63.     if( ctype(state) == "CState" ) then
  64.         state.paused = false;
  65.     end
  66. end
  67.  
  68. function CStateManager:IsPaused(state)
  69.     if( ctype(state) == "CState" ) then
  70.         return state.paused;
  71.     end
  72. end
  73.  
  74. function CStateManager:Call(func, ...)
  75.     if( self.States[1] and self.States[#self.States].isinit and not self.States[#self.States].paused ) then
  76.         if( self.States[#self.States][func] ) then
  77.             self.States[#self.States][func](self.States[#self.States], ...);
  78.         end
  79.     end
  80. end
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×