com.wallstop-studios.unity-helpers 2.0.0-rc75.2 → 2.0.0-rc75.4

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.
@@ -1,7 +1,7 @@
1
1
  namespace WallstopStudios.UnityHelpers.Tags
2
2
  {
3
3
  using System;
4
- using Core.DataStructure.Adapters;
4
+ using System.Threading;
5
5
  using Core.Extension;
6
6
 
7
7
  [Serializable]
@@ -10,16 +10,18 @@
10
10
  IComparable<EffectHandle>,
11
11
  IComparable
12
12
  {
13
+ private static long Id;
14
+
13
15
  public readonly AttributeEffect effect;
14
16
 
15
- public readonly KGuid id;
17
+ public readonly long id;
16
18
 
17
19
  public static EffectHandle CreateInstance(AttributeEffect effect)
18
20
  {
19
- return new EffectHandle(Guid.NewGuid(), effect);
21
+ return new EffectHandle(Interlocked.Increment(ref Id), effect);
20
22
  }
21
23
 
22
- private EffectHandle(KGuid id, AttributeEffect effect)
24
+ private EffectHandle(long id, AttributeEffect effect)
23
25
  {
24
26
  this.id = id;
25
27
  this.effect = effect;
@@ -1,9 +1,9 @@
1
1
  namespace WallstopStudios.UnityHelpers.Tags
2
2
  {
3
+ using System;
3
4
  using System.Collections.Generic;
4
5
  using System.Linq;
5
6
  using Core.Attributes;
6
- using Core.DataStructure.Adapters;
7
7
  using Core.Extension;
8
8
  using Core.Helper;
9
9
  using UnityEngine;
@@ -13,7 +13,10 @@
13
13
  [RequireComponent(typeof(TagHandler))]
14
14
  public sealed class EffectHandler : MonoBehaviour
15
15
  {
16
- [SiblingComponent()]
16
+ public event Action<EffectHandle> OnEffectApplied;
17
+ public event Action<EffectHandle> OnEffectRemoved;
18
+
19
+ [SiblingComponent]
17
20
  private TagHandler _tagHandler;
18
21
 
19
22
  [SiblingComponent(optional = true)]
@@ -26,11 +29,11 @@
26
29
  > _instancedCosmeticEffects = new();
27
30
 
28
31
  // Stores expiration time of duration effects (We store by Id because it's much cheaper to iterate Guids than it is EffectHandles
29
- private readonly Dictionary<KGuid, float> _effectExpirations = new();
30
- private readonly Dictionary<KGuid, EffectHandle> _effectHandlesById = new();
32
+ private readonly Dictionary<long, float> _effectExpirations = new();
33
+ private readonly Dictionary<long, EffectHandle> _effectHandlesById = new();
31
34
 
32
35
  // Used only to save allocations in Update()
33
- private readonly List<KGuid> _expiredEffectIds = new();
36
+ private readonly List<long> _expiredEffectIds = new();
34
37
  private readonly List<EffectHandle> _appliedEffects = new();
35
38
 
36
39
  private bool _initialized;
@@ -77,7 +80,7 @@
77
80
  && (effect.resetDurationOnReapplication || !_appliedEffects.Contains(handle))
78
81
  )
79
82
  {
80
- KGuid handleId = handle.id;
83
+ long handleId = handle.id;
81
84
  _effectExpirations[handleId] = Time.time + effect.duration;
82
85
  _effectHandlesById[handleId] = handle;
83
86
  }
@@ -123,10 +126,11 @@
123
126
  _ = _tagHandler.ForceRemoveTags(handle);
124
127
  }
125
128
 
126
- KGuid handleId = handle.id;
129
+ long handleId = handle.id;
127
130
  _ = _effectExpirations.Remove(handleId);
128
131
  _ = _effectHandlesById.Remove(handleId);
129
132
  InternalRemoveCosmeticEffects(handle);
133
+ OnEffectRemoved?.Invoke(handle);
130
134
  }
131
135
 
132
136
  private void InternalApplyEffect(EffectHandle handle)
@@ -142,7 +146,7 @@
142
146
  {
143
147
  if (effect.resetDurationOnReapplication || !exists)
144
148
  {
145
- KGuid handleId = handle.id;
149
+ long handleId = handle.id;
146
150
  _effectExpirations[handleId] = Time.time + effect.duration;
147
151
  _effectHandlesById[handleId] = handle;
148
152
  }
@@ -170,6 +174,8 @@
170
174
  attributesComponent.ForceApplyAttributeModifications(handle);
171
175
  }
172
176
  }
177
+
178
+ OnEffectApplied?.Invoke(handle);
173
179
  }
174
180
 
175
181
  private void InternalApplyEffect(AttributeEffect effect)
@@ -360,7 +366,7 @@
360
366
 
361
367
  _expiredEffectIds.Clear();
362
368
  float currentTime = Time.time;
363
- foreach (KeyValuePair<KGuid, float> entry in _effectExpirations)
369
+ foreach (KeyValuePair<long, float> entry in _effectExpirations)
364
370
  {
365
371
  if (entry.Value < currentTime)
366
372
  {
@@ -368,7 +374,7 @@
368
374
  }
369
375
  }
370
376
 
371
- foreach (KGuid expiredHandleId in _expiredEffectIds)
377
+ foreach (long expiredHandleId in _expiredEffectIds)
372
378
  {
373
379
  if (_effectHandlesById.TryGetValue(expiredHandleId, out EffectHandle expiredHandle))
374
380
  {
@@ -2,20 +2,23 @@
2
2
  {
3
3
  using System;
4
4
  using System.Collections.Generic;
5
- using Core.DataStructure.Adapters;
6
5
  using Core.Extension;
7
6
  using UnityEngine;
8
7
 
9
8
  [DisallowMultipleComponent]
10
9
  public sealed class TagHandler : MonoBehaviour
11
10
  {
11
+ public event Action<string> OnTagAdded;
12
+ public event Action<string> OnTagRemoved;
13
+ public event Action<string, uint> OnTagCountChanged;
14
+
12
15
  public IReadOnlyCollection<string> Tags => _tagCount.Keys;
13
16
 
14
17
  [SerializeField]
15
18
  private List<string> _initialEffectTags = new();
16
19
 
17
20
  private readonly Dictionary<string, uint> _tagCount = new(StringComparer.Ordinal);
18
- private readonly Dictionary<KGuid, EffectHandle> _effectHandles = new();
21
+ private readonly Dictionary<long, EffectHandle> _effectHandles = new();
19
22
 
20
23
  private void Awake()
21
24
  {
@@ -78,7 +81,7 @@
78
81
 
79
82
  public void ForceApplyTags(EffectHandle handle)
80
83
  {
81
- KGuid id = handle.id;
84
+ long id = handle.id;
82
85
  if (!_effectHandles.TryAdd(id, handle))
83
86
  {
84
87
  return;
@@ -97,7 +100,7 @@
97
100
 
98
101
  public bool ForceRemoveTags(EffectHandle handle)
99
102
  {
100
- KGuid id = handle.id;
103
+ long id = handle.id;
101
104
  if (!_effectHandles.Remove(id, out EffectHandle appliedHandle))
102
105
  {
103
106
  return false;
@@ -113,7 +116,19 @@
113
116
 
114
117
  private void InternalApplyTag(string effectTag)
115
118
  {
116
- _ = _tagCount.AddOrUpdate(effectTag, _ => 1U, (_, existing) => existing + 1);
119
+ uint currentCount = _tagCount.AddOrUpdate(
120
+ effectTag,
121
+ _ => 1U,
122
+ (_, existing) => existing + 1
123
+ );
124
+ if (currentCount == 1)
125
+ {
126
+ OnTagAdded?.Invoke(effectTag);
127
+ }
128
+ else
129
+ {
130
+ OnTagCountChanged?.Invoke(effectTag, currentCount);
131
+ }
117
132
  }
118
133
 
119
134
  private void InternalRemoveTag(string effectTag)
@@ -131,10 +146,12 @@
131
146
  if (count == 0)
132
147
  {
133
148
  _ = _tagCount.Remove(effectTag);
149
+ OnTagRemoved?.Invoke(effectTag);
134
150
  }
135
151
  else
136
152
  {
137
153
  _tagCount[effectTag] = count;
154
+ OnTagCountChanged?.Invoke(effectTag, count);
138
155
  }
139
156
  }
140
157
  }
@@ -39,6 +39,12 @@
39
39
  return null;
40
40
  }
41
41
 
42
+ _instance = FindAnyObjectByType<T>(FindObjectsInactive.Exclude);
43
+ if (_instance != null)
44
+ {
45
+ return _instance;
46
+ }
47
+
42
48
  Type type = typeof(T);
43
49
  GameObject instance = new($"{type.Name}-Singleton", type);
44
50
  if (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc75.2",
3
+ "version": "2.0.0-rc75.4",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},
@@ -65,6 +65,8 @@
65
65
 
66
66
 
67
67
 
68
+
69
+
68
70
 
69
71
 
70
72