Syntriax.Engine/Engine.Core/GameObject.cs

118 lines
3.5 KiB
C#

using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Name: {Name}, Initialized: {Initialized}")]
public class GameObject : BaseEntity, IGameObject
{
public event IAssignableTransform.OnTransformAssignedDelegate? OnTransformAssigned = null;
public event IAssignableGameManager.OnGameManagerAssignedDelegate? OnGameManagerAssigned = null;
public event IAssignableBehaviourController.OnBehaviourControllerAssignedDelegate? OnBehaviourControllerAssigned = null;
public event INameable.OnNameChangedDelegate? OnNameChanged = null;
public event IGameObject.OnUpdatedDelegate? OnUpdated = null;
private ITransform _transform = null!;
private IBehaviourController _behaviourController = null!;
private IStateEnable _stateEnable = null!;
private IGameManager _gameManager = null!;
private string _name = nameof(GameObject);
public ITransform Transform => _transform;
public IBehaviourController BehaviourController => _behaviourController;
public IGameManager GameManager => _gameManager;
public string Name
{
get => _name;
set
{
if (value == _name) return;
string previousName = _name;
_name = value;
OnNameChanged?.Invoke(this, previousName);
}
}
protected override void InitializeInternal()
{
base.InitializeInternal();
NotAssignedException.Check(this, _transform);
NotAssignedException.Check(this, _behaviourController);
NotAssignedException.Check(this, _stateEnable);
NotAssignedException.Check(this, _gameManager);
}
public void Update()
{
if (!_stateEnable.Enabled)
return;
OnUpdated?.Invoke(this);
}
protected override void FinalizeInternal()
{
base.FinalizeInternal();
foreach (IBehaviour behaviour in _behaviourController.GetBehaviours<IBehaviour>())
behaviour.Finalize();
}
public bool Assign(ITransform transform)
{
if (IsInitialized)
return false;
_transform = transform;
OnTransformAssigned?.Invoke(this);
return true;
}
public bool Assign(IBehaviourController behaviourController)
{
if (IsInitialized)
return false;
_behaviourController = behaviourController;
OnBehaviourControllerAssigned?.Invoke(this);
return true;
}
public bool Assign(IGameManager gameManager)
{
if (IsInitialized)
return false;
_gameManager = gameManager;
OnGameManagerAssigned?.Invoke(this);
return true;
}
protected override void UnassignInternal()
{
base.UnassignInternal();
_stateEnable = null!;
_transform = null!;
_behaviourController = null!;
_gameManager = null!;
}
public GameObject() { OnBehaviourControllerAssigned += ConnectBehaviourController; }
private void ConnectBehaviourController(IAssignableBehaviourController controller)
{
controller.BehaviourController.OnBehaviourAdded += OnBehaviourAdded;
controller.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved;
}
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Initialize(); }
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Finalize(); }
}