Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Timer : ITick
- {
- public object id;
- public float timer;
- public Action callBackAction;
- public float finishTime;
- public float timeScale = 1;
- public bool IsRunning { get; set; }
- public static List<Timer> FindAllTimers(object possibleHolder)
- {
- return Toolbox.Get<ProcessingTimer>().allWorkingTimers.FindAll(t => t.id != null && t.id == possibleHolder);
- }
- public static Timer Add(float finishTime, Action actionComplete, bool isRunning = true)
- {
- if (finishTime == 0)
- {
- actionComplete();
- return null;
- }
- if (Toolbox.applicationIsQuitting) return null;
- var mng = Toolbox.Get<ProcessingTimer>();
- if (mng == null) return null;
- var freeTimer = mng.poolTimers.Find(t => t.IsRunning == false);
- if (freeTimer == null)
- {
- var timer = new Timer(actionComplete, finishTime);
- mng.poolTimers.Add(timer);
- timer.IsRunning = isRunning;
- timer.Restart();
- return timer;
- }
- var _ft = freeTimer;
- _ft.id = null;
- _ft.timer = 0;
- _ft.callBackAction = actionComplete;
- _ft.IsRunning = isRunning;
- _ft.Restart(actionComplete, finishTime);
- return _ft;
- }
- public Timer(Action callBackAction, float finishTime)
- {
- this.finishTime = finishTime;
- this.callBackAction = callBackAction;
- timer = 0.0f;
- }
- public Timer AddID(object id)
- {
- this.id = id;
- var mono = id as MonoCached;
- if (mono)
- timeScale = mono.timeScale;
- return this;
- }
- public static void Stop(Timer obj)
- {
- if (obj == null) return;
- obj.Stop();
- }
- public static void Kill(Timer obj)
- {
- if (obj == null) return;
- obj.Stop();
- obj.Dispose();
- }
- public void Stop()
- {
- Toolbox.Get<ProcessingUpdate>().Remove(this);
- IsRunning = false;
- timer = 0.0f;
- }
- public Timer SetTimeScale(float timeScale)
- {
- this.timeScale = timeScale;
- return this;
- }
- public Timer Restart(float finishTime = 0.0f)
- {
- if (finishTime != 0.0f)
- this.finishTime = finishTime;
- if (timer > 0)
- {
- timer = 0.0f;
- IsRunning = true;
- return this;
- }
- timer = 0.0f;
- IsRunning = true;
- Toolbox.Get<ProcessingTimer>().allWorkingTimers.Add(this);
- Toolbox.Get<ProcessingUpdate>().Add(this);
- return this;
- }
- public Timer Restart(Action callBackAction, float finishTime = 0.0f)
- {
- if (callBackAction != null)
- this.callBackAction = callBackAction;
- if (finishTime != 0.0f)
- this.finishTime = finishTime;
- if (timer > 0)
- {
- timer = 0.0f;
- IsRunning = true;
- return this;
- }
- timer = 0.0f;
- IsRunning = true;
- Toolbox.Get<ProcessingTimer>().allWorkingTimers.Add(this);
- Toolbox.Get<ProcessingUpdate>().Add(this);
- return this;
- }
- public void Finish()
- {
- timer += finishTime;
- }
- public void Tick()
- {
- if (timer == 0 && !IsRunning)
- {
- Toolbox.Get<ProcessingUpdate>().Remove(this);
- Toolbox.Get<ProcessingTimer>().allWorkingTimers.Remove(this);
- }
- if (!IsRunning) return;
- Execute();
- }
- public void Execute()
- {
- timer += Time.deltaTime * timeScale;
- if (timer < finishTime) return;
- timer = 0.0f;
- IsRunning = false;
- Toolbox.Get<ProcessingUpdate>().Remove(this);
- Toolbox.Get<ProcessingTimer>().allWorkingTimers.Remove(this);
- callBackAction();
- }
- public void Dispose()
- {
- timer = 0.0f;
- IsRunning = false;
- id = null;
- Toolbox.Get<ProcessingUpdate>().Remove(this);
- Toolbox.Get<ProcessingTimer>().allWorkingTimers.Remove(this);
- callBackAction = delegate { };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement