using UnityEngine;
using UnityEngine.Assertions;
namespace UnityAtoms
{
///
/// A Variable Instancer is a MonoBehaviour that takes a variable as a base and creates an in memory copy of it OnEnable.
/// This is handy when you want to use atoms for prefabs that are instantiated at runtime. Use together with AtomCollection to
/// react accordingly when a prefab with an associated atom is added or deleted to the scene.
///
/// Variable of type T.
/// The value type.
/// Event of type T.
/// Event x 2 of type T.
/// Function of type T => T
[EditorIcon("atom-icon-hotpink")]
[DefaultExecutionOrder(Runtime.ExecutionOrder.VARIABLE_INSTANCER)]
public abstract class AtomVariableInstancer : MonoBehaviour
where V : AtomVariable
where E1 : AtomEvent
where E2 : AtomEvent
where F : AtomFunction
{
///
/// Getter for retrieving the in memory runtime variable.
///
public V Variable { get => _inMemoryCopy; }
///
/// Getter for retrieving the value of the in memory runtime variable.
///
public T Value { get => _inMemoryCopy.Value; set => _inMemoryCopy.Value = value; }
private V _inMemoryCopy;
///
/// The variable that the in memory copy will be based on when created at runtime.
///
[SerializeField]
private V _base = null;
///
/// If assigned the in memory copy variable will be added to the collection on Start using the gameObject's instance id as key. The value will also be removed from the collection OnDestroy.
///
[SerializeField]
private AtomCollection _syncToCollection = null;
///
/// If assigned the in memory copy variable will be added to the list on Start. The value will also be removed from the list OnDestroy.
///
[SerializeField]
private AtomList _syncToList = null;
private void OnEnable()
{
Assert.IsNotNull(_base);
_inMemoryCopy = Instantiate(_base);
if (_base.Changed != null)
{
_inMemoryCopy.Changed = Instantiate(_base.Changed);
}
if (_base.ChangedWithHistory != null)
{
_inMemoryCopy.ChangedWithHistory = Instantiate(_base.ChangedWithHistory);
}
}
void Start()
{
// Adding to the collection on Start instead of OnEnable because of timing issues that otherwise occurs when listeners register themselves OnEnable.
// This is an issue when a Game Object has a Variable Instancer attached to it when the scene starts and at the same time their is an AtomBaseListener listening to the associated Added event to a Collection.
if (_syncToCollection != null)
{
_syncToCollection.Value.Add(GetInstanceID().ToString(), _inMemoryCopy);
}
if (_syncToList != null)
{
_syncToList.Value.Add(_inMemoryCopy);
}
}
private void OnDestroy()
{
if (_syncToCollection != null)
{
_syncToCollection.Value.Remove(GetInstanceID().ToString());
}
if (_syncToList != null)
{
_syncToList.Value.Remove(_inMemoryCopy);
}
}
}
}