You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
	
	
		
			
				
					
						
							|  |  |  |  | using System; | 
					
						
							|  |  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Script.FrameWork.Time | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     public class TimerManager : Singleton<TimerManager> | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         private List<Timer> _timersToAdd = new(); | 
					
						
							|  |  |  |  |         private Dictionary<string, Timer> _timerDic = new(); | 
					
						
							|  |  |  |  |         private List<Timer> _timers = new(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public void AddTimer(float duration, Action completeAction, Action<float> updateAction = null) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             var timer = new Timer(duration, updateAction, completeAction); | 
					
						
							|  |  |  |  |             _timersToAdd.Add(timer); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public void AddTimer(string key, float duration, Action completeAction, Action<float> updateAction = null) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             RemoveTimer(key); | 
					
						
							|  |  |  |  |             var timer = new Timer(duration, updateAction, completeAction); | 
					
						
							|  |  |  |  |             _timersToAdd.Add(timer); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public void RemoveTimer(string key) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             if (_timerDic.TryGetValue(key, out var timer)) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 timer.Cancel(); | 
					
						
							|  |  |  |  |                 _timerDic.Remove(key); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public void RemoveAllTimers() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             foreach (var timer in _timers) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 timer.Cancel(true); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             _timers.Clear(); | 
					
						
							|  |  |  |  |             _timerDic.Clear(); | 
					
						
							|  |  |  |  |             _timersToAdd.Clear(); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public void Update(float dt) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             if (_timersToAdd.Count > 0) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 _timers.AddRange(_timersToAdd); | 
					
						
							|  |  |  |  |                 _timersToAdd.Clear(); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             for (var i = _timers.Count - 1; i >= 0; i--) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 var timer = _timers[i]; | 
					
						
							|  |  |  |  |                 if (timer.Finished) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     timer.Destroy(); | 
					
						
							|  |  |  |  |                     _timers.Remove(timer); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 else | 
					
						
							|  |  |  |  |                     timer.Update(dt); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } |