160 lines
5.1 KiB
C#
160 lines
5.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
[System.Diagnostics.DebuggerDisplay("Name: {Name}, Initialized: {Initialized}")]
|
|
public class HierarchyObject : BaseEntity, IHierarchyObject
|
|
{
|
|
public event IHierarchyObject.EnteredHierarchyEventHandler? OnEnteredHierarchy = null;
|
|
public event IHierarchyObject.ExitedHierarchyEventHandler? OnExitedHierarchy = null;
|
|
public event IHierarchyObject.ParentChangedEventHandler? OnParentChanged = null;
|
|
public event IHierarchyObject.ChildrenAddedEventHandler? OnChildrenAdded = null;
|
|
public event IHierarchyObject.ChildrenRemovedEventHandler? OnChildrenRemoved = null;
|
|
public event IHasBehaviourController.BehaviourControllerAssignedEventHandler? OnBehaviourControllerAssigned = null;
|
|
public event INameable.NameChangedEventHandler? OnNameChanged = null;
|
|
public event IActive.ActiveChangedEventHandler? OnActiveChanged = null;
|
|
|
|
private string _name = nameof(HierarchyObject);
|
|
private IGameManager _gameManager = null!;
|
|
private IBehaviourController _behaviourController = null!;
|
|
private bool _isActive = false;
|
|
private readonly List<IHierarchyObject> _children = [];
|
|
|
|
public IHierarchyObject? Parent { get; private set; } = null;
|
|
public IReadOnlyList<IHierarchyObject> Children => _children;
|
|
public IBehaviourController BehaviourController => _behaviourController;
|
|
public IGameManager GameManager => _gameManager;
|
|
public bool IsInHierarchy => _gameManager is not null;
|
|
public bool IsActive => _isActive;
|
|
|
|
public string Name
|
|
{
|
|
get => _name;
|
|
set
|
|
{
|
|
if (value == _name) return;
|
|
|
|
string previousName = _name;
|
|
_name = value;
|
|
OnNameChanged?.Invoke(this, previousName);
|
|
}
|
|
}
|
|
|
|
protected virtual void OnEnteringHierarchy(IGameManager gameManager) { }
|
|
bool IHierarchyObject.EnterHierarchy(IGameManager gameManager)
|
|
{
|
|
if (IsInHierarchy)
|
|
return false;
|
|
|
|
_gameManager = gameManager;
|
|
UpdateActive();
|
|
OnEnteringHierarchy(gameManager);
|
|
OnEnteredHierarchy?.Invoke(this, gameManager);
|
|
return true;
|
|
}
|
|
|
|
protected virtual void OnExitingHierarchy(IGameManager gameManager) { }
|
|
bool IHierarchyObject.ExitHierarchy()
|
|
{
|
|
if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
|
|
return false;
|
|
|
|
_gameManager = null!;
|
|
OnExitingHierarchy(gameManager);
|
|
OnExitedHierarchy?.Invoke(this, gameManager);
|
|
return true;
|
|
}
|
|
|
|
public void SetParent(IHierarchyObject? parent)
|
|
{
|
|
if (parent == this || Parent == parent)
|
|
return;
|
|
|
|
IHierarchyObject? previousParent = Parent;
|
|
if (previousParent is not null)
|
|
{
|
|
previousParent.RemoveChild(this);
|
|
previousParent.OnActiveChanged -= OnParentActiveChanged;
|
|
}
|
|
|
|
Parent = parent;
|
|
|
|
if (parent is not null)
|
|
{
|
|
parent.AddChild(this);
|
|
parent.OnActiveChanged += OnParentActiveChanged;
|
|
}
|
|
|
|
UpdateActive();
|
|
OnParentChanged?.Invoke(this, previousParent, parent);
|
|
}
|
|
|
|
public void AddChild(IHierarchyObject parent)
|
|
{
|
|
if (_children.Contains(parent))
|
|
return;
|
|
|
|
_children.Add(parent);
|
|
parent.SetParent(this);
|
|
OnChildrenAdded?.Invoke(this, parent);
|
|
}
|
|
|
|
public void RemoveChild(IHierarchyObject parent)
|
|
{
|
|
if (!_children.Remove(parent))
|
|
return;
|
|
|
|
parent.SetParent(null);
|
|
OnChildrenRemoved?.Invoke(this, parent);
|
|
}
|
|
|
|
protected virtual void OnAssign(IBehaviourController behaviourController) { }
|
|
public bool Assign(IBehaviourController behaviourController)
|
|
{
|
|
if (IsInitialized)
|
|
return false;
|
|
|
|
_behaviourController = behaviourController;
|
|
OnAssign(behaviourController);
|
|
OnBehaviourControllerAssigned?.Invoke(this);
|
|
return true;
|
|
}
|
|
|
|
protected override void OnAssign(IStateEnable stateEnable)
|
|
{
|
|
base.OnAssign(stateEnable);
|
|
|
|
stateEnable.OnEnabledChanged += OnStateEnabledChanged;
|
|
}
|
|
|
|
private void OnParentActiveChanged(IActive sender, bool previousState) => UpdateActive();
|
|
private void OnStateEnabledChanged(IStateEnable sender, bool previousState) => UpdateActive();
|
|
|
|
private void UpdateActive()
|
|
{
|
|
bool previousActive = IsActive;
|
|
_isActive = StateEnable.Enabled && (Parent?.IsActive ?? true);
|
|
|
|
if (previousActive != IsActive)
|
|
OnActiveChanged?.Invoke(this, previousActive);
|
|
}
|
|
|
|
protected override void UnassignInternal()
|
|
{
|
|
base.UnassignInternal();
|
|
StateEnable.OnEnabledChanged -= OnStateEnabledChanged;
|
|
}
|
|
|
|
protected override void InitializeInternal()
|
|
{
|
|
base.InitializeInternal();
|
|
_behaviourController ??= Factory.BehaviourControllerFactory.Instantiate(this);
|
|
}
|
|
|
|
public IEnumerator<IHierarchyObject> GetEnumerator() => _children.GetEnumerator();
|
|
IEnumerator IEnumerable.GetEnumerator() => _children.GetEnumerator();
|
|
}
|