2023-11-23 22:07:49 +03:00
|
|
|
using System;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
|
|
|
|
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
|
2024-01-30 12:26:17 +03:00
|
|
|
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
public Action<IEntity>? OnNameChanged { get; set; } = null;
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
public Action<IGameObject>? OnUpdated { get; set; } = null;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
private ITransform _transform = null!;
|
|
|
|
private IBehaviourController _behaviourController = null!;
|
|
|
|
private IStateEnable _stateEnable = null!;
|
2024-01-30 12:26:17 +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-01-30 12:26:17 +03:00
|
|
|
public IGameManager GameManager => _gameManager;
|
2023-11-23 22:07:49 +03:00
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get => _name;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == _name) return;
|
|
|
|
|
|
|
|
_name = value;
|
|
|
|
OnNameChanged?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
NotAssignedException.Check(this, _stateEnable);
|
2024-01-30 12:26:17 +03:00
|
|
|
NotAssignedException.Check(this, _gameManager);
|
2023-11-23 22:07:49 +03:00
|
|
|
}
|
|
|
|
|
2024-01-22 22:45:40 +03:00
|
|
|
public void Update()
|
2023-11-23 22:07:49 +03:00
|
|
|
{
|
|
|
|
if (!_stateEnable.Enabled)
|
|
|
|
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)
|
|
|
|
{
|
2023-11-24 16:37:09 +03:00
|
|
|
if (Initialized)
|
2023-11-23 22:07:49 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_transform = transform;
|
|
|
|
OnTransformAssigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Assign(IBehaviourController behaviourController)
|
|
|
|
{
|
2023-11-24 16:37:09 +03:00
|
|
|
if (Initialized)
|
2023-11-23 22:07:49 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_behaviourController = behaviourController;
|
|
|
|
OnBehaviourControllerAssigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-30 12:26:17 +03:00
|
|
|
public bool Assign(IGameManager gameManager)
|
|
|
|
{
|
|
|
|
if (Initialized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_gameManager = gameManager;
|
|
|
|
OnGameManagerAssigned?.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
|
|
|
|
|
|
|
_stateEnable = null!;
|
|
|
|
_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; }
|
|
|
|
private void ConnectBehaviourController(IAssignableBehaviourController controller)
|
|
|
|
{
|
|
|
|
controller.BehaviourController.OnBehaviourAdded += OnBehaviourAdded;
|
|
|
|
controller.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved;
|
|
|
|
}
|
|
|
|
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (Initialized) behaviour.Initialize(); }
|
|
|
|
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (Initialized) behaviour.Finalize(); }
|
|
|
|
}
|