Skip to content

Commit c8c2879

Browse files
authored
Event Bus (#23)
* Changes Made + Persistent Inventory Items Saving + Bag Refactored from SO to Serializable Class + Custom Json Serializer for SOs using Addressable + Small Utils Edits and Bug Fixes * Changes Made + Lighting regenerated for game scene + Small modifications and added burst project settings files * Refactored Functions in to states (just renames) * Updated Unity version to v2022 LTS * checkpoint * checkpoint, refactored StateStatus enum to a bool IsEnabled * added refactored and tested new state transitions * pooling abstraction implemented, moved SerializableVector3.cs to Data package to fix core dependency compile error * implemented spawner to hit effect on weapons * Changes made - Added serialized dictionary as an attribute to Generic Dictionary - More improvements to Spawner * checkpoint * finished #16 * small DeSpawn fix for Spawner.cs * EventBus implemented * EventBus.cs implemented/refactored into Project * namespace and class renames * revert from merge
1 parent b464436 commit c8c2879

File tree

159 files changed

+586
-302
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+586
-302
lines changed

Packages/com.ekaka.character/Runtime/Damage/DefaultDeath.cs renamed to Packages/com.ekaka.character/Runtime/Damage/DefaultDeathHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Character.Damage
44
{
5-
public class DefaultDeath : Death
5+
public class DefaultDeathHandler : DeathHandler
66
{
77
public override void Apply(DamageData damage)
88
{
9-
Destroy(damage.Damagable.Obj);
9+
Destroy(damage.Damageable.Obj);
1010
}
1111
}
1212
}

Packages/com.ekaka.character/Runtime/Main/Actor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Core.Game;
5-
using Core.Utils;
5+
using Core.Common;
66
using Damage.Main;
77
using Sensors.Main;
88
using UnityEngine;
99

1010
namespace Character.Main
1111
{
12-
public abstract class Actor : MonoBehaviour, IDamagable
12+
public abstract class Actor : MonoBehaviour, IDamageable
1313
{
1414
#region Ready
1515

@@ -110,7 +110,7 @@ protected virtual void Start()
110110

111111
else
112112
{
113-
GameManager.Instance.OnReady += Initialize;
113+
EventBus<GameManagerReady>.Subscribe(Initialize);
114114
}
115115
}
116116

@@ -135,9 +135,9 @@ protected virtual void Initialize()
135135

136136
#region Damagable
137137

138-
IDamagable damagable = this;
138+
IDamageable damageable = this;
139139

140-
damagable.InitializeDamagable();
140+
damageable.InitializeDamagable();
141141

142142
#endregion
143143

Packages/com.ekaka.core/Editor/BaseEditor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Text.RegularExpressions;
7-
using Core.Utils;
7+
using Core.Common;
88
using UnityEditor;
99
using UnityEngine;
1010

11-
namespace Editor.Core
11+
namespace Core.Editor
1212
{
1313
public static class BaseEditor
1414
{

Packages/com.ekaka.core/Editor/ObjectTypePropertyDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using Core.Utils;
4+
using Core.Common;
55
using UnityEditor;
66
using UnityEngine;
77
using Object = UnityEngine.Object;
88

9-
namespace Editor.Core
9+
namespace Core.Editor
1010
{
1111
[CustomPropertyDrawer(typeof(ObjectTypeAttribute))]
1212
public class ObjectTypePropertyDrawer : PropertyDrawer

Packages/com.ekaka.core/Editor/SceneListPropertyDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Collections;
22
using System.Collections.Generic;
3-
using Core.Utils;
3+
using Core.Common;
44
using UnityEditor;
55
using UnityEngine;
66

7-
namespace Editor.Core
7+
namespace Core.Editor
88
{
99
[CustomPropertyDrawer(typeof(SceneListAttribute))]
1010
public class SceneListPropertyDrawer : PropertyDrawer

Packages/com.ekaka.core/Editor/SerializedDictionaryPropertyDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System.Dynamic;
2-
using Core.Utils;
2+
using Core.Common;
33
using UnityEditor;
44
using UnityEngine;
55

6-
namespace Editor.Core
6+
namespace Core.Editor
77
{
88
[CustomPropertyDrawer(typeof(SerializedDictionaryAttribute), true)]
99
public class SerializedDictionaryPropertyDrawer : PropertyDrawer

Packages/com.ekaka.core/Editor/ShowIfPropertyDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Threading.Tasks;
6-
using Core.Utils;
6+
using Core.Common;
77
using UnityEditor;
88
using UnityEngine;
99

10-
namespace Editor.Core
10+
namespace Core.Editor
1111
{
1212
[CustomPropertyDrawer(typeof(ShowIfAttribute))]
1313
public class ShowIfPropertyDrawer : PropertyDrawer

Packages/com.ekaka.core/Editor/TagMaskPropertyDrawer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using Core.Utils;
5-
using Editor.Core;
4+
using Core.Common;
65
using UnityEditor;
76
using UnityEngine;
87

9-
namespace Editor.Core
8+
namespace Core.Editor
109
{
1110
[CustomPropertyDrawer(typeof(TagMask))]
1211
public class TagMaskPropertyDrawer : PropertyDrawer

Packages/com.ekaka.core/Runtime/Utils/ClipOverride.cs renamed to Packages/com.ekaka.core/Runtime/Common/ClipOverride.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Linq;
55
using UnityEngine;
66

7-
namespace Core.Utils
7+
namespace Core.Common
88
{
99
[Serializable]
1010
public class ClipOverride
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using UnityEngine;
5+
6+
namespace Core.Common
7+
{
8+
public abstract class EventBase<TDelegate, TConcrete> where TDelegate : Delegate where TConcrete : EventBase<TDelegate, TConcrete>, new()
9+
{
10+
private readonly HashSet<TDelegate> _listeners = new HashSet<TDelegate>();
11+
12+
protected TDelegate eventHandler;
13+
14+
private readonly List<TDelegate> _oneTimeListeners = new List<TDelegate>();
15+
16+
protected static TConcrete Instance { get; private set; } = new TConcrete();
17+
18+
protected virtual void AddListener(TDelegate handler)
19+
{
20+
if (_listeners.Contains(handler))
21+
{
22+
throw new InvalidOperationException("Attempting to subscribe a method that is already subscribed.");
23+
}
24+
25+
_listeners.Add(handler);
26+
}
27+
28+
protected virtual void RemoveListener(TDelegate handler)
29+
{
30+
if (!_listeners.Contains(handler))
31+
{
32+
throw new InvalidOperationException("Attempting to unsubscribe a method that is not subscribed.");
33+
}
34+
35+
_listeners.Remove(handler);
36+
37+
if (_oneTimeListeners.Contains(handler))
38+
{
39+
_oneTimeListeners.Remove(handler);
40+
}
41+
}
42+
43+
private void ListenOneTime(TDelegate handler)
44+
{
45+
AddListener(handler);
46+
47+
_oneTimeListeners.Add(handler);
48+
}
49+
50+
protected virtual void ClearOneTimeListeners()
51+
{
52+
foreach (TDelegate listener in _oneTimeListeners.ToList())
53+
{
54+
RemoveListener(listener);
55+
}
56+
57+
_oneTimeListeners.Clear();
58+
}
59+
60+
/// <summary>
61+
/// Subscribe to event.
62+
/// </summary>
63+
/// <param name="handler">Listener method subscribing to event.</param>
64+
public static void Subscribe(TDelegate handler)
65+
{
66+
Instance.AddListener(handler);
67+
}
68+
69+
/// <summary>
70+
/// Subscribe to event for a single call.
71+
/// </summary>
72+
/// <param name="handler">Listener method subscribing to event.</param>
73+
public static void SubscribeOneTime(TDelegate handler)
74+
{
75+
Instance.ListenOneTime(handler);
76+
}
77+
78+
/// <summary>
79+
/// Unsubscribe from event.
80+
/// </summary>
81+
/// <param name="handler">Listener method unsubscribing to event.</param>
82+
public static void Unsubscribe(TDelegate handler)
83+
{
84+
Instance.RemoveListener(handler);
85+
}
86+
87+
/// <summary>
88+
/// Try to Unsubscribe from event.
89+
/// This won't throw an exception if the listener is not subscribed.
90+
/// </summary>
91+
/// <param name="handler">Listener method subscribed to event.</param>
92+
public static void TryUnsubscribe(TDelegate handler)
93+
{
94+
try
95+
{
96+
Instance.RemoveListener(handler);
97+
}
98+
catch (InvalidOperationException)
99+
{
100+
Debug.LogWarning("Attempting to unsubscribe a method that is not subscribed.");
101+
}
102+
}
103+
}
104+
105+
public class EventBus<T> : EventBase<Action<T>, EventBus<T>> where T : IEventParams
106+
{
107+
private readonly Dictionary<Action, Action<T>> _actionMap = new Dictionary<Action, Action<T>>();
108+
109+
private readonly List<Action> _oneTimeListeners = new List<Action>();
110+
111+
protected override void AddListener(Action<T> handler)
112+
{
113+
base.AddListener(handler);
114+
115+
eventHandler += handler;
116+
}
117+
118+
private void AddListener(Action handler)
119+
{
120+
if (_actionMap.ContainsKey(handler))
121+
{
122+
throw new InvalidOperationException("Attempting to subscribe a method that is already subscribed.");
123+
}
124+
125+
void Handler(T arg)
126+
{
127+
handler();
128+
}
129+
130+
eventHandler += Handler;
131+
132+
_actionMap.Add(handler, Handler);
133+
}
134+
135+
private void ListenOneTime(Action handler)
136+
{
137+
AddListener(handler);
138+
139+
_oneTimeListeners.Add(handler);
140+
}
141+
142+
/// <summary>
143+
/// Subscribe to event.
144+
/// </summary>
145+
/// <param name="handler">Listener method subscribing to event.</param>
146+
public static void Subscribe(Action handler)
147+
{
148+
Instance.AddListener(handler);
149+
}
150+
151+
/// <summary>
152+
/// Subscribe to event for a single call.
153+
/// </summary>
154+
/// <param name="handler">Listener method subscribing to event.</param>
155+
public static void SubscribeOneTime(Action handler)
156+
{
157+
Instance.ListenOneTime(handler);
158+
}
159+
160+
protected override void RemoveListener(Action<T> handler)
161+
{
162+
base.RemoveListener(handler);
163+
164+
eventHandler -= handler;
165+
}
166+
167+
private void RemoveListener(Action handler)
168+
{
169+
if (_actionMap.TryGetValue(handler, out Action<T> action))
170+
{
171+
_actionMap.Remove(handler);
172+
173+
if (_oneTimeListeners.Contains(handler))
174+
{
175+
_oneTimeListeners.Remove(handler);
176+
}
177+
178+
eventHandler -= action;
179+
}
180+
181+
else
182+
{
183+
throw new InvalidOperationException("Attempting to unsubscribe a method that is not subscribed.");
184+
}
185+
}
186+
187+
/// <summary>
188+
/// Unsubscribe from event.
189+
/// </summary>
190+
/// <param name="handler">Listener method subscribed to event.</param>
191+
public static void Unsubscribe(Action handler)
192+
{
193+
Instance.RemoveListener(handler);
194+
}
195+
196+
/// <summary>
197+
/// Try to Unsubscribe from event.
198+
/// This won't throw an exception if the listener is not subscribed.
199+
/// </summary>
200+
/// <param name="handler">Listener method subscribed to event.</param>
201+
public static void TryUnsubscribe(Action handler)
202+
{
203+
try
204+
{
205+
Instance.RemoveListener(handler);
206+
}
207+
catch (InvalidOperationException)
208+
{
209+
Debug.LogWarning("Attempting to unsubscribe a method that is not subscribed.");
210+
}
211+
}
212+
213+
private void Publish(T arg)
214+
{
215+
eventHandler?.Invoke(arg);
216+
217+
ClearOneTimeListeners();
218+
}
219+
220+
/// <summary>
221+
/// Invoke event.
222+
/// </summary>
223+
/// <param name="arg">Event argument.</param>
224+
public static void Invoke(T arg)
225+
{
226+
Instance.Publish(arg);
227+
}
228+
229+
/// <summary>
230+
/// Invoke event.
231+
/// </summary>
232+
public static void Invoke()
233+
{
234+
Instance.Publish(default);
235+
}
236+
237+
protected override void ClearOneTimeListeners()
238+
{
239+
base.ClearOneTimeListeners();
240+
241+
foreach (Action listener in _oneTimeListeners.ToList())
242+
{
243+
RemoveListener(listener);
244+
}
245+
246+
_oneTimeListeners.Clear();
247+
}
248+
}
249+
250+
public interface IEventParams
251+
{
252+
}
253+
}

0 commit comments

Comments
 (0)