Syntriax.Engine/Engine.Core/Factory/BehaviourFactory.cs

28 lines
967 B
C#
Raw Permalink Normal View History

2023-11-23 22:07:49 +03:00
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core.Factory;
public class BehaviourFactory
{
public T Instantiate<T>(IGameObject gameObject, params object?[]? args) where T : class, IBehaviour
=> Instantiate<T>(gameObject, stateEnable: null, args);
public T Instantiate<T>(IGameObject gameObject, IStateEnable? stateEnable, params object?[]? args)
where T : class, IBehaviour
{
T behaviour = TypeFactory.Get<T>(args);
stateEnable ??= TypeFactory.Get<StateEnable>();
if (!stateEnable.Assign(behaviour))
throw AssignException.From(stateEnable, behaviour);
if (!behaviour.Assign(gameObject.BehaviourController))
throw AssignException.From(behaviour, gameObject.BehaviourController);
if (!behaviour.Assign(stateEnable))
throw AssignException.From(behaviour, stateEnable);
return behaviour;
}
}