-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimerManager.cs
More file actions
58 lines (47 loc) · 1.2 KB
/
TimerManager.cs
File metadata and controls
58 lines (47 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections.Generic;
using Assets;
using JetBrains.Annotations;
using UnityEngine;
public class TimerManager : Singleton<TimerManager>
{
private readonly List<Timer> _timers = new List<Timer>();
private readonly ObjectPool<Timer> _objectPool = new ObjectPool<Timer>(10);
public Timer Get()
{
var tiemr = _objectPool.Get();
_timers.Add(tiemr);
return tiemr;
}
public void OnUpdate(float deltaTime)
{
RemoveTimer();
for (var i = _timers.Count - 1; i >= 0; i--)
{
_timers[i].OnUpdate(deltaTime);
}
RemoveTimer();
}
public void RemoveTimer()
{
for (var i = _timers.Count - 1; i >= 0; i--)
{
var timer = _timers[i];
if (timer.IsCancelled)
{
timer.Reset();
_timers.RemoveAt(i);
_objectPool.Release(timer);
}
}
}
public bool TryGet(long useId, ref Timer timer)
{
for (var i = _timers.Count - 1; i >= 0; i--)
{
if (_timers[i].UseId != useId) continue;
timer = _timers[i];
return true;
}
return false;
}
}