Event Driven Broker
This commit is contained in:
parent
b198b5d4fb
commit
f67c28fe96
@ -5,3 +5,10 @@ public enum DamageType
|
||||
Attack,
|
||||
Spell,
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public enum DamageApplicationMethod
|
||||
{
|
||||
Hit,
|
||||
Tick
|
||||
}
|
||||
8
Assets/Scripts/EventDrivenHooks.meta
Normal file
8
Assets/Scripts/EventDrivenHooks.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 52c2c97a7fdbe454e9c104a1db581634
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
63
Assets/Scripts/EventDrivenHooks/EntityEventBroker.cs
Normal file
63
Assets/Scripts/EventDrivenHooks/EntityEventBroker.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class EntityEventBroker : MonoBehaviour
|
||||
{
|
||||
public OrderedEvent<DamageArgs> OnOutgoingDamage = new OrderedEvent<DamageArgs>();
|
||||
|
||||
public OrderedEvent<DamageArgs> OnIncomingDamage = new OrderedEvent<DamageArgs>();
|
||||
}
|
||||
|
||||
|
||||
// A robust, orderable event system for delegates with priority support
|
||||
public class OrderedEvent<T>
|
||||
{
|
||||
private class Subscriber
|
||||
{
|
||||
public int Priority;
|
||||
public Action<T> Handler;
|
||||
}
|
||||
|
||||
private List<Subscriber> subscribers = new List<Subscriber>();
|
||||
|
||||
public void Subscribe(Action<T> handler, int priority = 0)
|
||||
{
|
||||
if (handler == null) return;
|
||||
subscribers.Add(new Subscriber { Priority = priority, Handler = handler });
|
||||
subscribers.Sort((a, b) => a.Priority.CompareTo(b.Priority));
|
||||
}
|
||||
|
||||
public void Unsubscribe(Action<T> handler)
|
||||
{
|
||||
subscribers.RemoveAll(s => s.Handler == handler);
|
||||
}
|
||||
|
||||
public void Invoke(T args)
|
||||
{
|
||||
foreach (var sub in subscribers)
|
||||
{
|
||||
sub.Handler.Invoke(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Example usage:
|
||||
|
||||
/*public class FireHelmetEffect : MonoBehaviour
|
||||
{
|
||||
public EntityEventBroker broker;
|
||||
void OnEnable() {
|
||||
// Priority 10, runs after lower numbers
|
||||
broker.OnOutgoingDamage.Subscribe(OnModifyDamage, priority: 10);
|
||||
}
|
||||
void OnDisable() {
|
||||
broker.OnOutgoingDamage.Unsubscribe(OnModifyDamage);
|
||||
}
|
||||
void OnModifyDamage(DamageArgs args) {
|
||||
args.damageType = DamageType.Fire;
|
||||
args.currentValue *= 1.5f;
|
||||
}
|
||||
}*/
|
||||
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45e09b6617beed447a13265764139f69
|
||||
14
Assets/Scripts/EventDrivenHooks/EventBrokerArgs.cs
Normal file
14
Assets/Scripts/EventDrivenHooks/EventBrokerArgs.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
|
||||
public class DamageArgs
|
||||
{
|
||||
public Taggable user;
|
||||
public List<Taggable> targets;
|
||||
public float currentValue;
|
||||
public DamageType damageType;
|
||||
public DamageApplicationMethod applicationMethod;
|
||||
public bool isCrit;
|
||||
}
|
||||
2
Assets/Scripts/EventDrivenHooks/EventBrokerArgs.cs.meta
Normal file
2
Assets/Scripts/EventDrivenHooks/EventBrokerArgs.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6bfae0d27b1543948886b9ee5f166c2c
|
||||
Loading…
x
Reference in New Issue
Block a user