Syntriax.Engine/Engine.Core/Behaviour.cs

112 lines
2.7 KiB
C#
Raw Normal View History

2023-11-23 22:07:49 +03:00
using System;
2023-11-24 17:20:43 +03:00
2023-11-23 22:07:49 +03:00
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Priority: {Priority}, Initialized: {Initialized}")]
2023-11-23 22:07:49 +03:00
public abstract class Behaviour : IBehaviour
{
2023-11-24 17:03:21 +03:00
public Action<IAssignable>? OnUnassigned { get; set; } = null;
2023-11-23 22:07:49 +03:00
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
public Action<IInitialize>? OnInitialized { get; set; } = null;
public Action<IInitialize>? OnFinalized { get; set; } = null;
public Action<IBehaviour>? OnPriorityChanged { get; set; } = null;
private IBehaviourController _behaviourController = null!;
private IStateEnable _stateEnable = null!;
private bool _initialized = false;
private int _priority = 0;
public IStateEnable StateEnable => _stateEnable;
public IBehaviourController BehaviourController => _behaviourController;
public bool Initialized
{
get => _initialized;
private set
{
if (value == _initialized)
return;
_initialized = value;
if (value)
OnInitialized?.Invoke(this);
else
OnFinalized?.Invoke(this);
}
}
public int Priority
{
get => _priority;
set
{
if (value == _priority)
return;
_priority = value;
OnPriorityChanged?.Invoke(this);
}
}
public bool Assign(IStateEnable stateEnable)
{
if (Initialized)
2023-11-23 22:07:49 +03:00
return false;
_stateEnable = stateEnable;
_stateEnable.Assign(this);
OnStateEnableAssigned?.Invoke(this);
return true;
}
public bool Assign(IBehaviourController behaviourController)
{
if (Initialized)
2023-11-23 22:07:49 +03:00
return false;
_behaviourController = behaviourController;
OnBehaviourControllerAssigned?.Invoke(this);
return true;
}
2023-11-24 17:03:21 +03:00
public bool Unassign()
{
if (Initialized)
return false;
_stateEnable = null!;
_behaviourController = null!;
OnUnassigned?.Invoke(this);
return true;
}
2023-11-23 22:07:49 +03:00
public bool Initialize()
{
if (Initialized)
return false;
NotAssignedException.Check(this, _behaviourController);
NotAssignedException.Check(this, _stateEnable);
Initialized = true;
return true;
}
public bool Finalize()
{
if (!Initialized)
return false;
Initialized = false;
return true;
}
}