using UnityEngine;
using UnityEngine.Events;
namespace LobbyRooms
{
///
/// Observes an Observed class, intitializes with Observed State when beginning observation
///
///
public abstract class ObserverBehaviour : MonoBehaviour where T : Observed
{
public T observed { get; set; }
public UnityEvent OnObservedUpdated;
protected virtual void UpdateObserver(T obs)
{
observed = obs;
OnObservedUpdated?.Invoke(observed);
}
public void BeginObserving(T target)
{
if (target == null)
{
Debug.LogError($"Needs a Target of type {typeof(T)} to begin observing.", gameObject);
return;
}
UpdateObserver(target);
observed.onChanged += UpdateObserver;
}
public void EndObserving()
{
if (observed == null)
return;
if (observed.onChanged != null)
observed.onChanged -= UpdateObserver;
observed = null;
}
void Awake()
{
if (observed == null)
return;
BeginObserving(observed);
}
void OnDestroy()
{
if (observed == null)
return;
EndObserving();
}
}
}