Syntriax.Engine/Engine.Core/StateEnable.cs

50 lines
1.1 KiB
C#

using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public class StateEnable : IStateEnable
{
public event IAssignable.OnUnassignedDelegate? OnUnassigned = null;
public event IAssignableEntity.OnEntityAssignedDelegate? OnEntityAssigned = null;
public event IStateEnable.OnNameChangedDelegate? OnEnabledChanged = null;
private bool _enabled = true;
private IEntity _entity = null!;
public IEntity Entity => _entity;
public bool Enabled
{
get => _enabled;
set
{
if (value == _enabled)
return;
bool previousState = _enabled;
_enabled = value;
OnEnabledChanged?.Invoke(this, previousState);
}
}
public bool Assign(IEntity entity)
{
if (_entity is not null && _entity.IsInitialized)
return false;
_entity = entity;
OnEntityAssigned?.Invoke(this);
return true;
}
public bool Unassign()
{
if (_entity is null)
return false;
_entity = null!;
OnUnassigned?.Invoke(this);
return true;
}
}