com.wallstop-studios.unity-helpers 2.0.0-rc65 → 2.0.0-rc67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
namespace WallstopStudios.UnityHelpers.Utils
|
|
2
|
+
{
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections.Generic;
|
|
5
|
+
using Core.Extension;
|
|
6
|
+
using UnityEngine;
|
|
7
|
+
|
|
8
|
+
[DisallowMultipleComponent]
|
|
9
|
+
public class TagHandler : MonoBehaviour
|
|
10
|
+
{
|
|
11
|
+
public IReadOnlyCollection<string> Tags => _tagCount.Keys;
|
|
12
|
+
|
|
13
|
+
[SerializeField]
|
|
14
|
+
protected List<string> _initialEffectTags = new();
|
|
15
|
+
|
|
16
|
+
protected readonly Dictionary<string, uint> _tagCount = new(StringComparer.Ordinal);
|
|
17
|
+
|
|
18
|
+
protected virtual void Awake()
|
|
19
|
+
{
|
|
20
|
+
if (_initialEffectTags is { Count: > 0 })
|
|
21
|
+
{
|
|
22
|
+
foreach (string effectTag in _initialEffectTags)
|
|
23
|
+
{
|
|
24
|
+
InternalApplyTag(effectTag);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public bool HasTag(string effectTag)
|
|
30
|
+
{
|
|
31
|
+
return _tagCount.ContainsKey(effectTag);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public bool HasAnyTag(IEnumerable<string> effectTags)
|
|
35
|
+
{
|
|
36
|
+
foreach (string effectTag in effectTags)
|
|
37
|
+
{
|
|
38
|
+
if (_tagCount.ContainsKey(effectTag))
|
|
39
|
+
{
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public bool HasAnyTag(IReadOnlyList<string> effectTags)
|
|
48
|
+
{
|
|
49
|
+
for (int i = 0; i < effectTags.Count; ++i)
|
|
50
|
+
{
|
|
51
|
+
string effectTag = effectTags[i];
|
|
52
|
+
if (_tagCount.ContainsKey(effectTag))
|
|
53
|
+
{
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public void ApplyTag(string effectTag)
|
|
62
|
+
{
|
|
63
|
+
InternalApplyTag(effectTag);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public void RemoveTag(string effectTag, bool allInstances)
|
|
67
|
+
{
|
|
68
|
+
if (allInstances)
|
|
69
|
+
{
|
|
70
|
+
_tagCount.Remove(effectTag);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
InternalRemoveTag(effectTag);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private void InternalApplyTag(string effectTag)
|
|
78
|
+
{
|
|
79
|
+
_ = _tagCount.AddOrUpdate(effectTag, _ => 1U, (_, existing) => existing + 1);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private void InternalRemoveTag(string effectTag)
|
|
83
|
+
{
|
|
84
|
+
if (!_tagCount.TryGetValue(effectTag, out uint count))
|
|
85
|
+
{
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (count != 0)
|
|
90
|
+
{
|
|
91
|
+
--count;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (count == 0)
|
|
95
|
+
{
|
|
96
|
+
_ = _tagCount.Remove(effectTag);
|
|
97
|
+
}
|
|
98
|
+
else
|
|
99
|
+
{
|
|
100
|
+
_tagCount[effectTag] = count;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|