| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Helpers |
| | 5 | | { |
| | 6 | | public class CompositeLock |
| | 7 | | { |
| | 8 | | public static bool VERBOSE = false; |
| | 9 | |
|
| | 10 | | public event System.Action OnAllLocksRemoved; |
| | 11 | |
|
| | 12 | | int lockCounter = 0; |
| 1 | 13 | | HashSet<object> lockIds = new HashSet<object>(); |
| | 14 | |
|
| 0 | 15 | | public bool isUnlocked => lockCounter == 0; |
| | 16 | |
|
| | 17 | | public void AddLock(object id) |
| | 18 | | { |
| 0 | 19 | | if (lockIds.Contains(id)) |
| 0 | 20 | | return; |
| | 21 | |
|
| 0 | 22 | | lockIds.Add(id); |
| 0 | 23 | | lockCounter++; |
| | 24 | |
|
| 0 | 25 | | if (VERBOSE) |
| 0 | 26 | | Debug.Log($"Lock added... {lockCounter}"); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public void RemoveLock(object id) |
| | 30 | | { |
| 0 | 31 | | if (!lockIds.Contains(id)) |
| 0 | 32 | | return; |
| | 33 | |
|
| 0 | 34 | | lockIds.Remove(id); |
| 0 | 35 | | lockCounter--; |
| | 36 | |
|
| 0 | 37 | | if (VERBOSE) |
| 0 | 38 | | Debug.Log($"Locking removed... {lockCounter}"); |
| | 39 | |
|
| 0 | 40 | | if (lockCounter == 0) |
| 0 | 41 | | OnAllLocksRemoved?.Invoke(); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void RemoveAllLocks() |
| | 45 | | { |
| 1 | 46 | | lockCounter = 0; |
| 1 | 47 | | lockIds.Clear(); |
| 1 | 48 | | OnAllLocksRemoved?.Invoke(); |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | public HashSet<object> GetLockIdsCopy() { return new HashSet<object>(lockIds); } |
| | 52 | | } |
| | 53 | | } |