Skip to content

Commit a941d29

Browse files
Rewrote event system to use scriptable objects instead. Allows easier adding of future events
1 parent face9d7 commit a941d29

22 files changed

Lines changed: 215 additions & 104 deletions

Attributes/Health.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Health : MonoBehaviour, ISaveable {
2727
public class TakeDamageEvent : UnityEvent<float> { }
2828

2929
[System.Serializable]
30-
public class OnDieEvent : UnityEvent<Health> { }
30+
public class OnDieEvent : UnityEvent<String> { }
3131

3232
public bool IsDead { get; private set; } = false;
3333
public float Percentage => Fraction * 100;
@@ -69,7 +69,7 @@ public void TakeDamage(GameObject instigator, float damage) {
6969
takeDamage?.Invoke(damage);
7070
onHealthUpdate?.Invoke();
7171
if (healthPoints.value == 0) {
72-
onDie?.Invoke(this);
72+
onDie?.Invoke(gameObject.name);
7373
Die();
7474
AwardExperience(instigator);
7575
}

Combat/PickupBase.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace RPG.Combat {
88
public class PickupBase : MonoBehaviour, IRaycastable {
99
private Collider objectCollider;
1010

11-
[SerializeField] private OnPickupEvent onPickup;
11+
[SerializeField] public OnPickupEvent onPickUp;
1212

1313
[System.Serializable]
14-
public class OnPickupEvent : UnityEvent<PickupBase> { }
14+
public class OnPickupEvent : UnityEvent<string> { }
1515

1616
public CursorType Cursor => CursorType.Pickup;
1717

@@ -38,7 +38,8 @@ protected void Pickup(bool respawnable, float respawnTime) {
3838
} else {
3939
Destroy(gameObject);
4040
}
41-
onPickup?.Invoke(this);
41+
42+
onPickUp?.Invoke(gameObject.name);
4243
}
4344

4445
public bool HandleRaycast(GameObject callingObject) {

Events/Editor.meta

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

Events/Editor/EventEditor.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace RPG.Events {
5+
[CustomEditor(typeof(GameEvent))]
6+
public class EventEditor : Editor {
7+
public override void OnInspectorGUI() {
8+
base.OnInspectorGUI();
9+
10+
GUI.enabled = Application.isPlaying;
11+
12+
GameEvent e = target as GameEvent;
13+
if (GUILayout.Button("Raise")) {
14+
e.Raise("Raise");
15+
}
16+
17+
}
18+
}
19+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Events/EventSystem.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

Events/GameEvent.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace RPG.Events {
5+
[CreateAssetMenu(fileName = "GameEvent", menuName = "Events/New Game Event", order = 0)]
6+
public class GameEvent : ScriptableObject {
7+
/// <summary>
8+
/// The list of listeners that this event will notify if it is raised.
9+
/// </summary>
10+
private readonly List<GameEventListener> eventListeners =
11+
new List<GameEventListener>();
12+
13+
public void Raise(string something) {
14+
for (int i = eventListeners.Count - 1; i >= 0; i--) {
15+
eventListeners[i].OnEventRaised(something);
16+
}
17+
18+
}
19+
20+
public void RegisterListener(GameEventListener listener) {
21+
if (!eventListeners.Contains(listener)) {
22+
eventListeners.Add(listener);
23+
}
24+
25+
}
26+
27+
public void UnregisterListener(GameEventListener listener) {
28+
if (eventListeners.Contains(listener)) {
29+
eventListeners.Remove(listener);
30+
}
31+
}
32+
}
33+
}

Events/GameEvent.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.

Events/GameEventListener.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using UnityEngine;
2+
using UnityEngine.Events;
3+
4+
namespace RPG.Events {
5+
public class GameEventListener : MonoBehaviour {
6+
[Tooltip("Event to register with.")]
7+
public GameEvent Event;
8+
9+
[Tooltip("Response to invoke when Event is raised.")]
10+
public StringEvent Response;
11+
12+
[System.Serializable]
13+
public class StringEvent : UnityEvent<string> { }
14+
15+
private void OnEnable() {
16+
Event.RegisterListener(this);
17+
}
18+
19+
private void OnDisable() {
20+
Event.UnregisterListener(this);
21+
}
22+
23+
public void OnEventRaised(string context) {
24+
Response.Invoke(context);
25+
}
26+
}
27+
}

Events/GameEventListener.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.

0 commit comments

Comments
 (0)