feat: Added IAssignable.Unassign()

This commit is contained in:
Syntriax 2023-11-24 17:03:21 +03:00
parent 6f68e29fb8
commit 5a01b01215
6 changed files with 79 additions and 2 deletions

View File

@ -1,6 +1,22 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the class implementing it has Assignable fields that are necessary for the engine to work properly.
/// </summary>
public interface IAssignable { }
public interface IAssignable
{
/// <summary>
/// Callback triggered when the <see cref="IAssignable"/>'s fields are unassigned and completely ready to recycle.
/// </summary>
Action<IAssignable>? OnUnassigned { get; set; }
/// <summary>
/// Unassign <see cref="IAssignable"/>'s all fields and make it ready to recycle.
/// </summary>
/// <returns>
/// <see cref="true"/>, if the fields are unsigned successfully, <see cref="false"/> if not.
/// </returns>
bool Unassign();
}

View File

@ -6,6 +6,7 @@ namespace Syntriax.Engine.Core;
public abstract class Behaviour : IBehaviour
{
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
@ -73,6 +74,19 @@ public abstract class Behaviour : IBehaviour
return true;
}
public bool Unassign()
{
if (Initialized)
return false;
_stateEnable = null!;
_behaviourController = null!;
_stateEnable.Unassign();
OnUnassigned?.Invoke(this);
return true;
}
public bool Initialize()
{
if (Initialized)

View File

@ -15,6 +15,7 @@ public class BehaviourController : IBehaviourController
public Action<IBehaviourController, IBehaviour>? OnBehaviourAdded { get; set; } = null;
public Action<IBehaviourController, IBehaviour>? OnBehaviourRemoved { get; set; } = null;
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableGameObject>? OnGameObjectAssigned { get; set; } = null;
@ -95,6 +96,16 @@ public class BehaviourController : IBehaviourController
return true;
}
public bool Unassign()
{
if (GameObject.Initialized)
return false;
_gameObject = null!;
OnUnassigned?.Invoke(this);
return true;
}
public void Update(GameTime gameTime)
{
if (!GameObject.StateEnable.Enabled)

View File

@ -14,6 +14,7 @@ public class GameManager : IInitialize
{
public Action<IInitialize>? OnInitialized { get; set; } = null;
public Action<IInitialize>? OnFinalized { get; set; } = null;
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
@ -98,6 +99,16 @@ public class GameManager : IInitialize
return true;
}
public bool Unassign()
{
if (Initialized)
return false;
_stateEnable = null!;
OnUnassigned?.Invoke(this);
return true;
}
public void Update(GameTime time)
{
foreach (var gameObject in GameObjects)

View File

@ -11,6 +11,7 @@ public class GameObject : IGameObject
{
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
public Action<IEntity>? OnNameChanged { get; set; } = null;
@ -126,6 +127,19 @@ public class GameObject : IGameObject
return true;
}
public bool Unassign()
{
if (Initialized)
return false;
_stateEnable = null!;
_transform = null!;
_behaviourController = null!;
OnUnassigned?.Invoke(this);
return true;
}
public GameObject() { OnBehaviourControllerAssigned += ConnectBehaviourController; }
private void ConnectBehaviourController(IAssignableBehaviourController controller)
{

View File

@ -6,6 +6,7 @@ namespace Syntriax.Engine.Core;
public class StateEnable : IStateEnable
{
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableEntity>? OnEntityAssigned { get; set; } = null;
public Action<IStateEnable>? OnEnabledChanged { get; set; } = null;
@ -29,11 +30,21 @@ public class StateEnable : IStateEnable
public bool Assign(IEntity entity)
{
if (_entity is not null) // TODO: IInitialize Maybe?
if (_entity is not null) // TODO: Add IInitialize to IAssignable or IEntity maybe?
return false;
_entity = entity;
OnEntityAssigned?.Invoke(this);
return true;
}
public bool Unassign()
{
if (_entity is null)
return false;
_entity = null!;
OnUnassigned?.Invoke(this);
return true;
}
}