Factory/Syntriax.Modules.Factory/FactoryBase.cs

62 lines
1.4 KiB
C#

using System;
using UnityEngine;
namespace Syntriax.Modules.Factory
{
public abstract class FactoryBase : MonoBehaviour, IFactory
{
public bool IsInitialized { get; private set; } = false;
public Action<IFactory> OnInitialized { get; set; } = null;
public Action<IFactory> OnReset { get; set; } = null;
public Action<IFactory> OnStateChanged { get; set; } = null;
public void Initialize()
{
if (IsInitialized)
return;
IsInitialized = true;
try
{
OnFactoryInitialize();
OnInitialized?.Invoke(this);
OnStateChanged?.Invoke(this);
}
catch (System.Exception e)
{
Debug.LogError(e);
Reset();
}
}
public void Reset()
{
if (!IsInitialized)
return;
IsInitialized = false;
try
{
OnFactoryReset();
OnReset?.Invoke(this);
OnStateChanged?.Invoke(this);
}
catch (System.Exception e)
{
Debug.LogError(e);
}
}
/// <inheritdoc cref="Initialize"/>
protected abstract void OnFactoryInitialize();
/// <inheritdoc cref="Reset"/>
protected abstract void OnFactoryReset();
}
}