Skip to content

Commit 9f2d965

Browse files
committed
cleanup
1 parent 56dc989 commit 9f2d965

18 files changed

+200
-130
lines changed

Runtime/Events/GameEvent.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using UnityEngine;
43

54
namespace Gameframe.ScriptableObjects.Events
6-
{
7-
[CreateAssetMenu(menuName ="GameJam/Events/GameEvent")]
5+
{
6+
[CreateAssetMenu(menuName=MenuNames.EventMenu+"GameEvent")]
87
public class GameEvent : ScriptableObject
98
{
10-
private List<IGameEventListener> listeners = new List<IGameEventListener>();
9+
private readonly List<IGameEventListener> _listeners = new List<IGameEventListener>();
1110

1211
public void Raise()
1312
{
14-
for ( int i = listeners.Count-1; i >= 0; i-- )
13+
for ( int i = _listeners.Count-1; i >= 0; i-- )
1514
{
16-
listeners[i].OnEventRaised(this);
15+
_listeners[i].OnEventRaised(this);
1716
}
1817
}
1918

2019
public void AddListener(IGameEventListener listener)
2120
{
22-
listeners.Add(listener);
21+
_listeners.Add(listener);
2322
}
2423

2524
public void RemoveListener(IGameEventListener listener)
2625
{
27-
listeners.Remove(listener);
26+
_listeners.Remove(listener);
2827
}
2928
}
3029
}

Runtime/Events/GameEventManager.cs

Lines changed: 107 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using UnityEngine;
5-
using UnityEngine.Events;
64

75
namespace Gameframe.ScriptableObjects.Events
86
{
9-
10-
[CreateAssetMenu(menuName = "GameJam/Events/EventManager")]
7+
[CreateAssetMenu(menuName = MenuNames.EventMenu+"EventManager")]
118
public class GameEventManager : ScriptableObject
129
{
1310

14-
//Classes to wrap callbacks so they can be called generically
1511
#region Invokable Callback Wrappers
16-
private abstract class BaseInvokable
12+
/// <summary>
13+
/// Serves as a wrapper for an invokable callback
14+
/// </summary>
15+
private interface IInvokable
1716
{
18-
public virtual void Invoke(params object[] args)
19-
{
20-
}
17+
void Invoke(params object[] args);
2118
}
2219

23-
private class Invokable : BaseInvokable
20+
/// <summary>
21+
/// Invokable wrapper for a simple action callback
22+
/// </summary>
23+
private class Invokable : IInvokable
2424
{
2525
public Invokable(Action callback)
2626
{
@@ -29,13 +29,17 @@ public Invokable(Action callback)
2929

3030
public Action Callback { get; }
3131

32-
public override void Invoke(params object[] args)
32+
public void Invoke(params object[] args)
3333
{
3434
Callback.Invoke();
3535
}
3636
}
3737

38-
private class Invokable<T> : BaseInvokable
38+
/// <summary>
39+
/// Invokable wrapper for a callback that takes a single arg
40+
/// </summary>
41+
/// <typeparam name="T"></typeparam>
42+
private class Invokable<T> : IInvokable
3943
{
4044
public Invokable(Action<T> callback)
4145
{
@@ -44,15 +48,54 @@ public Invokable(Action<T> callback)
4448

4549
public Action<T> Callback { get; }
4650

47-
public override void Invoke(params object[] args)
51+
public void Invoke(params object[] args)
4852
{
4953
Callback.Invoke((T)args[0]);
5054
}
5155
}
5256

57+
/// <summary>
58+
/// Invokable wrapper for a callback that takes a single arg
59+
/// </summary>
60+
/// <typeparam name="T"></typeparam>
61+
private class Invokable<T0,T1> : IInvokable
62+
{
63+
public Invokable(Action<T0,T1> callback)
64+
{
65+
Callback = callback;
66+
}
67+
68+
public Action<T0,T1> Callback { get; }
69+
70+
public void Invoke(params object[] args)
71+
{
72+
if ( args.Length < 2 )
73+
{
74+
return;
75+
}
76+
77+
if ( !(args[0] is T0) )
78+
{
79+
return;
80+
}
81+
82+
if ( !(args[1] is T1) )
83+
{
84+
return;
85+
}
86+
87+
var arg0 = (T0)args[0];
88+
var arg1 = (T1)args[1];
89+
Callback.Invoke(arg0,arg1);
90+
}
91+
}
92+
93+
/// <summary>
94+
/// Internal representation of a single invokable event instance
95+
/// </summary>
5396
private class InternalEvent
5497
{
55-
private readonly List<BaseInvokable> _invokables = new List<BaseInvokable>();
98+
private readonly List<IInvokable> _invokables = new List<IInvokable>();
5699

57100
public void Raise(params object[] args)
58101
{
@@ -71,7 +114,24 @@ public void AddListener<T>(Action<T> callback)
71114
{
72115
_invokables.Add(new Invokable<T>(callback));
73116
}
117+
118+
public void AddListener<T0,T1>(Action<T0,T1> callback)
119+
{
120+
_invokables.Add(new Invokable<T0,T1>(callback));
121+
}
74122

123+
public void RemoveListener(Action callback)
124+
{
125+
for (var i = 0; i < _invokables.Count; i++)
126+
{
127+
if (_invokables[i] is Invokable invokable && invokable.Callback == callback)
128+
{
129+
_invokables.RemoveAt(i);
130+
return;
131+
}
132+
}
133+
}
134+
75135
public void RemoveListener<T>(Action<T> callback)
76136
{
77137
for (var i = 0; i < _invokables.Count; i++)
@@ -84,17 +144,18 @@ public void RemoveListener<T>(Action<T> callback)
84144
}
85145
}
86146

87-
public void RemoveListener(Action callback)
147+
public void RemoveListener<T0,T1>(Action<T0,T1> callback)
88148
{
89149
for (var i = 0; i < _invokables.Count; i++)
90150
{
91-
if (_invokables[i] is Invokable invokable && invokable.Callback == callback)
151+
if (_invokables[i] is Invokable<T0,T1> invokable && invokable.Callback == callback)
92152
{
93153
_invokables.RemoveAt(i);
94154
return;
95155
}
96156
}
97157
}
158+
98159
}
99160
#endregion
100161

@@ -114,7 +175,6 @@ public void AddListener(string eventName, Action callback)
114175
_eventDictionary.Add(eventName,@event);
115176
}
116177

117-
//Now how can I unsubscribe? -_-
118178
@event.AddListener(callback);
119179
}
120180

@@ -125,11 +185,28 @@ public void AddListener<T>(string eventName, Action<T> callback)
125185
@event = new InternalEvent();
126186
_eventDictionary.Add(eventName,@event);
127187
}
128-
129-
//Now how can I unsubscribe? -_-
130188
@event.AddListener(callback);
131189
}
132-
190+
191+
public void AddListener<T0,T1>(string eventName, Action<T0,T1> callback)
192+
{
193+
if (!_eventDictionary.TryGetValue(eventName, out var @event))
194+
{
195+
@event = new InternalEvent();
196+
_eventDictionary.Add(eventName,@event);
197+
}
198+
@event.AddListener(callback);
199+
}
200+
201+
public void RemoveListener(string eventName, Action callback)
202+
{
203+
if (!_eventDictionary.TryGetValue(eventName, out var @event))
204+
{
205+
return;
206+
}
207+
@event.RemoveListener(callback);
208+
}
209+
133210
public void RemoveListener<T>(string eventName, Action<T> callback)
134211
{
135212
if (!_eventDictionary.TryGetValue(eventName, out var @event))
@@ -139,7 +216,7 @@ public void RemoveListener<T>(string eventName, Action<T> callback)
139216
@event.RemoveListener(callback);
140217
}
141218

142-
public void RemoveListener(string eventName, Action callback)
219+
public void RemoveListener<T0,T1>(string eventName, Action<T0,T1> callback)
143220
{
144221
if (!_eventDictionary.TryGetValue(eventName, out var @event))
145222
{
@@ -166,6 +243,15 @@ public void Raise<T>(string eventName, T payload)
166243
@event.Raise(payload);
167244
}
168245

246+
public void Raise<T0,T1>(string eventName, T0 payload0, T1 payload1)
247+
{
248+
if (!_eventDictionary.TryGetValue(eventName, out var @event))
249+
{
250+
return;
251+
}
252+
@event.Raise(payload0,payload1);
253+
}
254+
169255
}
170256

171257
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using UnityEngine;
4-
5-
namespace Gameframe.ScriptableObjects.Events
1+
namespace Gameframe.ScriptableObjects.Events
62
{
7-
83
public interface IGameEventListener
94
{
105
void OnEventRaised(GameEvent gameEvent);
116
}
12-
137
}

Runtime/Events/StringGameEvent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
namespace Gameframe.ScriptableObjects.Events
66
{
7-
[CreateAssetMenu(menuName = "GameJam/Events/StringEvent")]
7+
[CreateAssetMenu(menuName = MenuNames.EventMenu+"StringEvent")]
88
public class StringGameEvent : BaseGameEvent<string>
99
{
1010
[SerializeField]
11-
string _value;
11+
private string _value;
1212
public override string Value
1313
{
14-
get { return _value; }
15-
set { _value = value; }
14+
get => _value;
15+
set => _value = value;
1616
}
1717
}
1818

Runtime/MenuNames.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Gameframe.ScriptableObjects
2+
{
3+
public static class MenuNames
4+
{
5+
public const string MenuBase = "Gameframe/";
6+
public const string EventMenu = MenuBase + "Events/";
7+
public const string RuntimeSetMenu = MenuBase + "RuntimeSets/";
8+
public const string Variables = MenuBase + "Variables/";
9+
}
10+
}

Runtime/MenuNames.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/RuntimeSets/GameObjectRuntimeSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Gameframe.ScriptableObjects.RuntimeSets
66
{
7-
[CreateAssetMenu(menuName ="GameJam/Sets/GameObjectSet")]
7+
[CreateAssetMenu(menuName =MenuNames.RuntimeSetMenu+"GameObjectSet")]
88
public class GameObjectRuntimeSet : RuntimeSet<GameObject>
99
{
1010

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using UnityEngine;
1+
using UnityEngine;
42

53
namespace Gameframe.ScriptableObjects.RuntimeSets
64
{
7-
85
public class RuntimeSetInstance : MonoBehaviour
96
{
10-
117
[SerializeField]
12-
GameObjectRuntimeSet set;
8+
private GameObjectRuntimeSet set;
139

14-
void Awake()
10+
private void Awake()
1511
{
16-
set.Add(this.gameObject);
12+
set.Add(gameObject);
1713
}
1814

19-
void OnDestroy()
15+
private void OnDestroy()
2016
{
21-
set.Remove(this.gameObject);
17+
set.Remove(gameObject);
2218
}
23-
2419
}
25-
2620
}

0 commit comments

Comments
 (0)