feat: IActive interface added for hierarchy active state
This commit is contained in:
111
Engine.Core/BaseEntity.cs
Normal file
111
Engine.Core/BaseEntity.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
public abstract class BaseEntity : IEntity
|
||||
{
|
||||
public event IEntity.IdChangedEventHandler? OnIdChanged = null;
|
||||
|
||||
public event IInitializable.InitializedEventHandler? OnInitialized = null;
|
||||
public event IInitializable.FinalizedEventHandler? OnFinalized = null;
|
||||
|
||||
public event IHasStateEnable.StateEnableAssignedEventHandler? OnStateEnableAssigned = null;
|
||||
public event IAssignable.UnassignEventHandler? OnUnassigned = null;
|
||||
|
||||
private IStateEnable _stateEnable = null!;
|
||||
|
||||
private bool _initialized = false;
|
||||
private string _id = string.Empty;
|
||||
|
||||
public virtual IStateEnable StateEnable => _stateEnable;
|
||||
|
||||
public string Id
|
||||
{
|
||||
get => _id;
|
||||
set
|
||||
{
|
||||
if (IsInitialized)
|
||||
throw new($"Can't change {nameof(Id)} of {_id} because it's initialized");
|
||||
|
||||
if (value == _id)
|
||||
return;
|
||||
|
||||
string previousId = _id;
|
||||
|
||||
_id = value;
|
||||
OnIdChanged?.Invoke(this, previousId);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsInitialized
|
||||
{
|
||||
get => _initialized;
|
||||
private set
|
||||
{
|
||||
if (value == _initialized)
|
||||
return;
|
||||
|
||||
_initialized = value;
|
||||
if (value)
|
||||
OnInitialized?.Invoke(this);
|
||||
else
|
||||
OnFinalized?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnAssign(IStateEnable stateEnable) { }
|
||||
public bool Assign(IStateEnable stateEnable)
|
||||
{
|
||||
if (IsInitialized)
|
||||
return false;
|
||||
|
||||
_stateEnable = stateEnable;
|
||||
_stateEnable.Assign(this);
|
||||
OnAssign(stateEnable);
|
||||
OnStateEnableAssigned?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void UnassignInternal() { }
|
||||
public bool Unassign()
|
||||
{
|
||||
if (IsInitialized)
|
||||
return false;
|
||||
|
||||
UnassignInternal();
|
||||
|
||||
_stateEnable = null!;
|
||||
_stateEnable.Unassign();
|
||||
OnUnassigned?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void InitializeInternal() { }
|
||||
public bool Initialize()
|
||||
{
|
||||
if (IsInitialized)
|
||||
return false;
|
||||
|
||||
_stateEnable ??= Factory.StateEnableFactory.Instantiate(this);
|
||||
|
||||
InitializeInternal();
|
||||
|
||||
IsInitialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void FinalizeInternal() { }
|
||||
public bool Finalize()
|
||||
{
|
||||
if (!IsInitialized)
|
||||
return false;
|
||||
|
||||
FinalizeInternal();
|
||||
|
||||
IsInitialized = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected BaseEntity() => _id = Guid.NewGuid().ToString("D");
|
||||
protected BaseEntity(string id) => _id = id;
|
||||
}
|
Reference in New Issue
Block a user