40 lines
1.4 KiB
C#
40 lines
1.4 KiB
C#
|
using Syntriax.Engine.Core.Abstract;
|
||
|
using Syntriax.Engine.Core.Exceptions;
|
||
|
|
||
|
namespace Syntriax.Engine.Core.Factory;
|
||
|
|
||
|
public class GameObjectFactory
|
||
|
{
|
||
|
public T Instantiate<T>(params object?[]? args) where T : class, IGameObject
|
||
|
=> Instantiate<T>(transform: null, behaviourController: null, stateEnable: null, args);
|
||
|
|
||
|
public T Instantiate<T>(
|
||
|
ITransform? transform = null,
|
||
|
IBehaviourController? behaviourController = null,
|
||
|
IStateEnable? stateEnable = null,
|
||
|
params object?[]? args
|
||
|
)
|
||
|
where T : class, IGameObject
|
||
|
{
|
||
|
T gameObject = TypeFactory.Get<T>(args);
|
||
|
|
||
|
transform ??= TypeFactory.Get<Transform>();
|
||
|
behaviourController ??= TypeFactory.Get<BehaviourController>();
|
||
|
stateEnable ??= TypeFactory.Get<StateEnable>();
|
||
|
|
||
|
if (!behaviourController.Assign(gameObject))
|
||
|
throw AssignException.From(behaviourController, gameObject);
|
||
|
if (!stateEnable.Assign(gameObject))
|
||
|
throw AssignException.From(stateEnable, gameObject);
|
||
|
|
||
|
if (!gameObject.Assign(transform))
|
||
|
throw AssignException.From(gameObject, transform);
|
||
|
if (!gameObject.Assign(behaviourController))
|
||
|
throw AssignException.From(gameObject, behaviourController);
|
||
|
if (!gameObject.Assign(stateEnable))
|
||
|
throw AssignException.From(gameObject, stateEnable);
|
||
|
|
||
|
return gameObject;
|
||
|
}
|
||
|
}
|