2023-11-23 22:07:49 +03:00
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
using Syntriax.Engine.Core.Exceptions;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
2024-01-27 00:51:34 +03:00
|
|
|
[System.Diagnostics.DebuggerDisplay("Name: {Name}, Initialized: {Initialized}")]
|
2024-02-02 12:11:51 +03:00
|
|
|
public class GameObject : BaseEntity, IGameObject
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-10-22 22:02:38 +03:00
|
|
|
public event IHierarchyObject.OnEnteredHierarchyDelegate? OnEnteredHierarchy = null;
|
|
|
|
public event IHierarchyObject.OnExitedHierarchyDelegate? OnExitedHierarchy = null;
|
|
|
|
|
2024-07-15 01:13:39 +03:00
|
|
|
public event IAssignableTransform.OnTransformAssignedDelegate? OnTransformAssigned = null;
|
|
|
|
public event IAssignableBehaviourController.OnBehaviourControllerAssignedDelegate? OnBehaviourControllerAssigned = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-07-15 01:13:39 +03:00
|
|
|
public event INameable.OnNameChangedDelegate? OnNameChanged = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-07-15 01:13:39 +03:00
|
|
|
public event IGameObject.OnUpdatedDelegate? OnUpdated = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
private ITransform _transform = null!;
|
|
|
|
private IBehaviourController _behaviourController = null!;
|
2024-10-22 22:02:38 +03:00
|
|
|
private IGameManager? _gameManager = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
private string _name = nameof(GameObject);
|
|
|
|
|
|
|
|
public ITransform Transform => _transform;
|
|
|
|
public IBehaviourController BehaviourController => _behaviourController;
|
2024-10-22 22:02:38 +03:00
|
|
|
public IGameManager? GameManager => _gameManager;
|
|
|
|
public bool IsInHierarchy => GameManager is not null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get => _name;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == _name) return;
|
|
|
|
|
2024-07-15 01:13:39 +03:00
|
|
|
string previousName = _name;
|
2023-11-23 22:07:49 +03:00
|
|
|
_name = value;
|
2024-07-15 01:13:39 +03:00
|
|
|
OnNameChanged?.Invoke(this, previousName);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:11:51 +03:00
|
|
|
protected override void InitializeInternal()
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-02-02 12:11:51 +03:00
|
|
|
base.InitializeInternal();
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
NotAssignedException.Check(this, _transform);
|
|
|
|
NotAssignedException.Check(this, _behaviourController);
|
|
|
|
}
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
public void Update()
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-10-26 22:45:24 +03:00
|
|
|
if (!StateEnable.Enabled)
|
2023-11-23 22:07:49 +03:00
|
|
|
return;
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
OnUpdated?.Invoke(this);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:11:51 +03:00
|
|
|
protected override void FinalizeInternal()
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
2024-02-02 12:11:51 +03:00
|
|
|
base.FinalizeInternal();
|
2023-11-23 22:07:49 +03:00
|
|
|
|
2024-02-02 12:11:51 +03:00
|
|
|
foreach (IBehaviour behaviour in _behaviourController.GetBehaviours<IBehaviour>())
|
|
|
|
behaviour.Finalize();
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool Assign(ITransform transform)
|
|
|
|
{
|
2024-10-22 20:57:12 +03:00
|
|
|
if (IsInitialized)
|
2023-11-23 22:07:49 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_transform = transform;
|
|
|
|
OnTransformAssigned?.Invoke(this);
|
2024-11-10 19:18:44 +03:00
|
|
|
transform.OnParentChanged += OnParentChangedInternal;
|
|
|
|
OnParentChangedInternal(transform, null, transform.Parent);
|
2023-11-23 22:07:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Assign(IBehaviourController behaviourController)
|
|
|
|
{
|
2024-10-22 20:57:12 +03:00
|
|
|
if (IsInitialized)
|
2023-11-23 22:07:49 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_behaviourController = behaviourController;
|
|
|
|
OnBehaviourControllerAssigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:11:51 +03:00
|
|
|
protected override void UnassignInternal()
|
2023-11-24 17:03:21 +03:00
|
|
|
{
|
2024-02-02 12:11:51 +03:00
|
|
|
base.UnassignInternal();
|
2023-11-24 17:03:21 +03:00
|
|
|
|
2024-11-10 19:18:44 +03:00
|
|
|
_transform.OnParentChanged -= OnParentChangedInternal;
|
|
|
|
OnParentChangedInternal(_transform, _transform.Parent, null);
|
2023-11-24 17:03:21 +03:00
|
|
|
_transform = null!;
|
|
|
|
_behaviourController = null!;
|
2024-01-30 12:26:17 +03:00
|
|
|
_gameManager = null!;
|
2023-11-24 17:03:21 +03:00
|
|
|
}
|
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
public GameObject() { OnBehaviourControllerAssigned += ConnectBehaviourController; }
|
2024-07-15 01:13:39 +03:00
|
|
|
|
2023-11-23 22:07:49 +03:00
|
|
|
private void ConnectBehaviourController(IAssignableBehaviourController controller)
|
|
|
|
{
|
|
|
|
controller.BehaviourController.OnBehaviourAdded += OnBehaviourAdded;
|
|
|
|
controller.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved;
|
|
|
|
}
|
2024-10-26 22:45:24 +03:00
|
|
|
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Finalize(); }
|
|
|
|
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (!IsInitialized) behaviour.Initialize(); }
|
2024-10-22 22:02:38 +03:00
|
|
|
|
2024-11-10 19:18:44 +03:00
|
|
|
private void OnParentChangedInternal(ITransform sender, ITransform? previousParent, ITransform? newParent)
|
|
|
|
{
|
|
|
|
if (previousParent is not null)
|
|
|
|
{
|
|
|
|
previousParent.OnParentChanged -= OnParentChangedInternal;
|
|
|
|
previousParent.GameObject.OnEnteredHierarchy -= EnterHierarchyInternal;
|
|
|
|
previousParent.GameObject.OnExitedHierarchy -= ExitHierarchyInternal;
|
|
|
|
}
|
|
|
|
if (newParent is not null)
|
|
|
|
{
|
|
|
|
newParent.OnParentChanged += OnParentChangedInternal;
|
|
|
|
newParent.GameObject.OnEnteredHierarchy += EnterHierarchyInternal;
|
|
|
|
newParent.GameObject.OnExitedHierarchy += ExitHierarchyInternal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnterHierarchyInternal(IHierarchyObject sender, IGameManager gameManager) => ((IHierarchyObject)this).EnterHierarchy(gameManager);
|
|
|
|
private void ExitHierarchyInternal(IHierarchyObject sender, IGameManager gameManager) => ((IHierarchyObject)this).ExitHierarchy();
|
|
|
|
|
2024-10-26 13:46:04 +03:00
|
|
|
bool IHierarchyObject.EnterHierarchy(IGameManager gameManager)
|
2024-10-22 22:02:38 +03:00
|
|
|
{
|
|
|
|
if (IsInHierarchy)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_gameManager = gameManager;
|
2024-11-03 20:13:25 +03:00
|
|
|
OnEnteredHierarchy?.Invoke(this, gameManager);
|
2024-10-22 22:02:38 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-26 13:46:04 +03:00
|
|
|
bool IHierarchyObject.ExitHierarchy()
|
2024-10-22 22:02:38 +03:00
|
|
|
{
|
2024-11-03 20:13:25 +03:00
|
|
|
if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
|
2024-10-22 22:02:38 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_gameManager = null;
|
2024-11-03 20:13:25 +03:00
|
|
|
OnExitedHierarchy?.Invoke(this, gameManager);
|
2024-10-22 22:02:38 +03:00
|
|
|
return true;
|
|
|
|
}
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|