2024-02-02 12:11:51 +03:00
|
|
|
using System;
|
|
|
|
|
2024-10-26 22:45:24 +03:00
|
|
|
using Syntriax.Engine.Core.Exceptions;
|
|
|
|
|
2024-02-02 12:11:51 +03:00
|
|
|
namespace Syntriax.Engine.Core.Abstract;
|
|
|
|
|
|
|
|
public abstract class BaseEntity : IEntity
|
|
|
|
{
|
2024-11-23 23:14:44 +03:00
|
|
|
public event IEntity.OnIdChangedEventHandler? OnIdChanged = null;
|
2024-02-02 12:11:51 +03:00
|
|
|
|
2024-11-23 23:14:44 +03:00
|
|
|
public event IInitialize.OnInitializedEventHandler? OnInitialized = null;
|
|
|
|
public event IInitialize.OnFinalizedEventHandler? OnFinalized = null;
|
2024-02-02 12:11:51 +03:00
|
|
|
|
2024-11-23 23:14:44 +03:00
|
|
|
public event IAssignableStateEnable.OnStateEnableAssignedEventHandler? OnStateEnableAssigned = null;
|
|
|
|
public event IAssignable.OnUnassignedEventHandler? OnUnassigned = null;
|
2024-02-02 12:11:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
private IStateEnable _stateEnable = null!;
|
|
|
|
|
|
|
|
private bool _initialized = false;
|
|
|
|
private string _id = string.Empty;
|
|
|
|
|
|
|
|
public virtual IStateEnable StateEnable => _stateEnable;
|
|
|
|
|
|
|
|
public virtual bool IsActive => StateEnable.Enabled;
|
|
|
|
|
|
|
|
public string Id
|
|
|
|
{
|
|
|
|
get => _id;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == _id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
string previousId = _id;
|
|
|
|
|
|
|
|
_id = value;
|
|
|
|
OnIdChanged?.Invoke(this, previousId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 20:57:12 +03:00
|
|
|
public bool IsInitialized
|
2024-02-02 12:11:51 +03:00
|
|
|
{
|
|
|
|
get => _initialized;
|
|
|
|
private set
|
|
|
|
{
|
|
|
|
if (value == _initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_initialized = value;
|
|
|
|
if (value)
|
|
|
|
OnInitialized?.Invoke(this);
|
|
|
|
else
|
|
|
|
OnFinalized?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Assign(IStateEnable stateEnable)
|
|
|
|
{
|
2024-10-22 20:57:12 +03:00
|
|
|
if (IsInitialized)
|
2024-02-02 12:11:51 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_stateEnable = stateEnable;
|
|
|
|
_stateEnable.Assign(this);
|
|
|
|
OnStateEnableAssigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void UnassignInternal() { }
|
|
|
|
public bool Unassign()
|
|
|
|
{
|
2024-10-22 20:57:12 +03:00
|
|
|
if (IsInitialized)
|
2024-02-02 12:11:51 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
UnassignInternal();
|
|
|
|
|
2024-10-26 22:45:24 +03:00
|
|
|
_stateEnable = null!;
|
|
|
|
_stateEnable.Unassign();
|
2024-02-02 12:11:51 +03:00
|
|
|
OnUnassigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void InitializeInternal() { }
|
|
|
|
public bool Initialize()
|
|
|
|
{
|
2024-10-22 20:57:12 +03:00
|
|
|
if (IsInitialized)
|
2024-02-02 12:11:51 +03:00
|
|
|
return false;
|
|
|
|
|
2024-10-26 22:45:24 +03:00
|
|
|
NotAssignedException.Check(this, _stateEnable);
|
|
|
|
|
2024-02-02 12:11:51 +03:00
|
|
|
InitializeInternal();
|
|
|
|
|
2024-10-22 20:57:12 +03:00
|
|
|
IsInitialized = true;
|
2024-02-02 12:11:51 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void FinalizeInternal() { }
|
|
|
|
public bool Finalize()
|
|
|
|
{
|
2024-10-22 20:57:12 +03:00
|
|
|
if (!IsInitialized)
|
2024-02-02 12:11:51 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
FinalizeInternal();
|
|
|
|
|
2024-10-22 20:57:12 +03:00
|
|
|
IsInitialized = false;
|
2024-02-02 12:11:51 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected BaseEntity() => _id = Guid.NewGuid().ToString("D");
|
|
|
|
protected BaseEntity(string id) => _id = id;
|
|
|
|
}
|