47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
namespace Engine.Core;
|
|
|
|
public class StateEnable : IStateEnable
|
|
{
|
|
public Event<IStateEnable, IStateEnable.EnabledChangedArguments> OnEnabledChanged { get; } = new();
|
|
public Event<IHasEntity> OnEntityAssigned { get; } = new();
|
|
public Event<IAssignable>? OnUnassigned { get; } = new();
|
|
|
|
public IEntity Entity { get; private set; } = null!;
|
|
|
|
public bool Enabled
|
|
{
|
|
get;
|
|
set
|
|
{
|
|
if (value == field)
|
|
return;
|
|
|
|
bool previousState = field;
|
|
field = value;
|
|
OnEnabledChanged?.Invoke(this, new(previousState));
|
|
}
|
|
} = true;
|
|
|
|
protected virtual void OnAssign(IEntity entity) { }
|
|
public bool Assign(IEntity entity)
|
|
{
|
|
if (Entity is not null && Entity.IsInitialized)
|
|
return false;
|
|
|
|
Entity = entity;
|
|
OnAssign(entity);
|
|
OnEntityAssigned?.Invoke(this);
|
|
return true;
|
|
}
|
|
|
|
public bool Unassign()
|
|
{
|
|
if (Entity is null)
|
|
return false;
|
|
|
|
Entity = null!;
|
|
OnUnassigned?.Invoke(this);
|
|
return true;
|
|
}
|
|
}
|