Factory/FactoryBase.cs

62 lines
1.4 KiB
C#
Raw Normal View History

2022-11-21 21:51:23 +03:00
using System;
2022-11-21 13:33:46 +03:00
using UnityEngine;
namespace Syntriax.Modules.Factory
{
public abstract class FactoryBase : MonoBehaviour, IFactory
{
2022-11-21 21:51:23 +03:00
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;
2022-11-21 22:18:47 +03:00
try
{
OnFactoryInitialize();
OnInitialized?.Invoke(this);
OnStateChanged?.Invoke(this);
}
catch (System.Exception e)
{
Debug.LogError(e);
Reset();
}
2022-11-21 21:51:23 +03:00
}
public void Reset()
{
if (!IsInitialized)
return;
IsInitialized = false;
2022-11-21 22:18:47 +03:00
try
{
OnFactoryReset();
OnReset?.Invoke(this);
OnStateChanged?.Invoke(this);
}
catch (System.Exception e)
{
Debug.LogError(e);
}
2022-11-21 21:51:23 +03:00
}
/// <inheritdoc cref="Initialize"/>
protected abstract void OnFactoryInitialize();
/// <inheritdoc cref="Reset"/>
protected abstract void OnFactoryReset();
2022-11-21 13:33:46 +03:00
}
}