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