Syntriax.Engine/Engine.Core/StateEnable.cs

50 lines
1.1 KiB
C#
Raw Normal View History

2023-11-23 22:07:49 +03:00
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public class StateEnable : IStateEnable
{
2024-07-15 01:13:39 +03:00
public event IAssignable.OnUnassignedDelegate? OnUnassigned = null;
public event IAssignableEntity.OnEntityAssignedDelegate? OnEntityAssigned = null;
public event IStateEnable.OnNameChangedDelegate? OnEnabledChanged = null;
2023-11-23 22:07:49 +03:00
private bool _enabled = true;
private IEntity _entity = null!;
public IEntity Entity => _entity;
public bool Enabled
{
get => _enabled;
set
{
if (value == _enabled)
return;
2024-07-15 01:13:39 +03:00
bool previousState = _enabled;
2023-11-23 22:07:49 +03:00
_enabled = value;
2024-07-15 01:13:39 +03:00
OnEnabledChanged?.Invoke(this, previousState);
2023-11-23 22:07:49 +03:00
}
}
public bool Assign(IEntity entity)
{
if (_entity is not null && _entity.IsInitialized)
2023-11-23 22:07:49 +03:00
return false;
_entity = entity;
OnEntityAssigned?.Invoke(this);
return true;
}
2023-11-24 17:03:21 +03:00
public bool Unassign()
{
if (_entity is null)
return false;
_entity = null!;
OnUnassigned?.Invoke(this);
return true;
}
2023-11-23 22:07:49 +03:00
}