2018-10-30 15:05:06 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
2019-04-07 10:03:16 -04:00
|
|
|
using UnityEngine.Serialization;
|
2018-10-30 15:05:06 -04:00
|
|
|
|
|
|
|
namespace UnityAtoms
|
|
|
|
{
|
2019-09-25 15:05:06 -04:00
|
|
|
public abstract class AtomListener<T, GA, E, UER> : MonoBehaviour, IAtomListener<T>, IAtomListenerIcon
|
|
|
|
where GA : AtomAction<T>
|
|
|
|
where E : AtomEvent<T> where UER : UnityEvent<T>
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-04-07 10:03:16 -04:00
|
|
|
[FormerlySerializedAs("Event")]
|
2018-10-30 15:05:06 -04:00
|
|
|
[SerializeField]
|
2019-04-08 10:14:50 -04:00
|
|
|
private E _event = null;
|
2018-10-30 15:05:06 -04:00
|
|
|
|
2019-04-07 10:03:16 -04:00
|
|
|
public E GameEvent { get { return _event; } set { _event = value; } }
|
2018-10-30 15:05:06 -04:00
|
|
|
|
2019-04-07 10:03:16 -04:00
|
|
|
[FormerlySerializedAs("UnityEventResponse")]
|
2018-10-30 15:05:06 -04:00
|
|
|
[SerializeField]
|
2019-04-08 10:14:50 -04:00
|
|
|
private UER _unityEventResponse = null;
|
2018-10-30 15:05:06 -04:00
|
|
|
|
2019-04-07 10:03:16 -04:00
|
|
|
[FormerlySerializedAs("GameActionResponses")]
|
2018-10-30 15:05:06 -04:00
|
|
|
[SerializeField]
|
2019-09-25 15:05:06 -04:00
|
|
|
private List<GA> _actionResponses = new List<GA>();
|
2018-10-30 15:05:06 -04:00
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
2019-04-11 04:11:13 -04:00
|
|
|
if (GameEvent == null) return;
|
2018-10-30 15:05:06 -04:00
|
|
|
GameEvent.RegisterListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
{
|
2019-04-11 04:11:13 -04:00
|
|
|
if (GameEvent == null) return;
|
2018-10-30 15:05:06 -04:00
|
|
|
GameEvent.UnregisterListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnEventRaised(T item)
|
|
|
|
{
|
2019-04-07 10:03:16 -04:00
|
|
|
if (_unityEventResponse != null) { _unityEventResponse.Invoke(item); }
|
2019-09-25 15:05:06 -04:00
|
|
|
for (int i = 0; _actionResponses != null && i < _actionResponses.Count; ++i)
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-09-25 15:05:06 -04:00
|
|
|
_actionResponses[i].Do(item);
|
2018-10-30 15:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 15:05:06 -04:00
|
|
|
public abstract class AtomListener<T1, T2, GA, E, UER> : MonoBehaviour, IAtomListener<T1, T2>, IAtomListenerIcon
|
|
|
|
where GA : AtomAction<T1, T2>
|
|
|
|
where E : AtomEvent<T1, T2>
|
2019-04-07 10:03:16 -04:00
|
|
|
where UER : UnityEvent<T1, T2>
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-04-07 10:03:16 -04:00
|
|
|
[FormerlySerializedAs("Event")]
|
2018-10-30 15:05:06 -04:00
|
|
|
[SerializeField]
|
2019-04-07 10:03:16 -04:00
|
|
|
private E _event;
|
2018-10-30 15:05:06 -04:00
|
|
|
|
2019-04-07 10:03:16 -04:00
|
|
|
public E GameEvent { get { return _event; } set { _event = value; } }
|
2018-10-30 15:05:06 -04:00
|
|
|
|
2019-04-07 10:03:16 -04:00
|
|
|
[FormerlySerializedAs("UnityEventResponse")]
|
2018-10-30 15:05:06 -04:00
|
|
|
[SerializeField]
|
2019-04-07 10:03:16 -04:00
|
|
|
private UER _unityEventResponse;
|
2018-10-30 15:05:06 -04:00
|
|
|
|
2019-04-07 10:03:16 -04:00
|
|
|
[FormerlySerializedAs("GameActionResponses")]
|
2018-10-30 15:05:06 -04:00
|
|
|
[SerializeField]
|
2019-09-25 15:05:06 -04:00
|
|
|
private List<GA> _actionResponses = new List<GA>();
|
2018-10-30 15:05:06 -04:00
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
2019-04-07 10:03:16 -04:00
|
|
|
if (_event == null) return;
|
2018-10-30 15:05:06 -04:00
|
|
|
GameEvent.RegisterListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
{
|
2019-04-07 10:03:16 -04:00
|
|
|
if (_event == null) return;
|
2018-10-30 15:05:06 -04:00
|
|
|
GameEvent.UnregisterListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnEventRaised(T1 first, T2 second)
|
|
|
|
{
|
2019-04-07 10:03:16 -04:00
|
|
|
if (_unityEventResponse != null) { _unityEventResponse.Invoke(first, second); }
|
2019-09-25 15:05:06 -04:00
|
|
|
for (int i = 0; _actionResponses != null && i < _actionResponses.Count; ++i)
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-09-25 15:05:06 -04:00
|
|
|
_actionResponses[i].Do(first, second);
|
2018-10-30 15:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-17 18:43:20 -04:00
|
|
|
}
|