2023-11-23 22:07:49 +03:00
|
|
|
using System;
|
|
|
|
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
using Syntriax.Engine.Core.Exceptions;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
|
|
|
public class GameObject : IGameObject
|
|
|
|
{
|
|
|
|
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
|
|
|
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
|
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<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
|
|
|
|
|
|
|
|
public Action<IEntity>? OnNameChanged { get; set; } = null;
|
|
|
|
|
|
|
|
public Action<IInitialize>? OnInitialized { get; set; } = null;
|
|
|
|
public Action<IInitialize>? OnFinalized { get; set; } = null;
|
|
|
|
|
|
|
|
public Action<IGameObject, GameTime>? OnUpdated { get; set; } = null;
|
|
|
|
|
|
|
|
|
|
|
|
private ITransform _transform = null!;
|
|
|
|
private IBehaviourController _behaviourController = null!;
|
|
|
|
private IStateEnable _stateEnable = null!;
|
|
|
|
|
|
|
|
private string _name = nameof(GameObject);
|
|
|
|
private bool _initialized = false;
|
|
|
|
|
|
|
|
public ITransform Transform => _transform;
|
|
|
|
public IBehaviourController BehaviourController => _behaviourController;
|
|
|
|
public IStateEnable StateEnable => _stateEnable;
|
|
|
|
|
|
|
|
public bool Initialized
|
|
|
|
{
|
|
|
|
get => _initialized;
|
|
|
|
private set
|
|
|
|
{
|
|
|
|
if (value == _initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_initialized = value;
|
|
|
|
if (value)
|
|
|
|
OnInitialized?.Invoke(this);
|
|
|
|
else
|
|
|
|
OnFinalized?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get => _name;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == _name) return;
|
|
|
|
|
|
|
|
_name = value;
|
|
|
|
OnNameChanged?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Initialize()
|
|
|
|
{
|
|
|
|
if (Initialized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NotAssignedException.Check(this, _transform);
|
|
|
|
NotAssignedException.Check(this, _behaviourController);
|
|
|
|
NotAssignedException.Check(this, _stateEnable);
|
|
|
|
|
|
|
|
Initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Update(GameTime time)
|
|
|
|
{
|
|
|
|
if (!_stateEnable.Enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
OnUpdated?.Invoke(this, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Finalize()
|
|
|
|
{
|
|
|
|
if (!Initialized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
System.Threading.Tasks.Parallel.ForEach(
|
|
|
|
_behaviourController.GetBehaviours<IBehaviour>(),
|
|
|
|
behaviour => behaviour.Finalize()
|
|
|
|
);
|
|
|
|
|
|
|
|
Initialized = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Assign(IStateEnable stateEnable)
|
|
|
|
{
|
2023-11-24 16:37:09 +03:00
|
|
|
if (Initialized)
|
2023-11-23 22:07:49 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
_stateEnable = stateEnable;
|
|
|
|
OnStateEnableAssigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-24 17:03:21 +03:00
|
|
|
public bool Unassign()
|
|
|
|
{
|
|
|
|
if (Initialized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_stateEnable = null!;
|
|
|
|
_transform = null!;
|
|
|
|
_behaviourController = null!;
|
|
|
|
|
|
|
|
OnUnassigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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(); }
|
|
|
|
}
|