chore: Added Initial Engine Code
This commit is contained in:
6
Engine.Core/Factory/Abstract/IFactory.cs
Normal file
6
Engine.Core/Factory/Abstract/IFactory.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Syntriax.Engine.Core.Factory.Abstract;
|
||||
|
||||
public interface IFactory<TInterface> where TInterface : class
|
||||
{
|
||||
T Get<T>(params object?[]? args) where T : class, TInterface;
|
||||
}
|
21
Engine.Core/Factory/BehaviourControllerFactory.cs
Normal file
21
Engine.Core/Factory/BehaviourControllerFactory.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
using Syntriax.Engine.Core.Exceptions;
|
||||
|
||||
namespace Syntriax.Engine.Core.Factory;
|
||||
|
||||
public class BehaviourControllerFactory
|
||||
{
|
||||
public IBehaviourController Instantiate(IGameObject gameObject)
|
||||
=> Instantiate<BehaviourController>(gameObject);
|
||||
|
||||
public T Instantiate<T>(IGameObject gameObject, params object?[]? args)
|
||||
where T : class, IBehaviourController
|
||||
{
|
||||
T behaviourController = TypeFactory.Get<T>(args);
|
||||
|
||||
if (!behaviourController.Assign(gameObject))
|
||||
throw AssignException.From(behaviourController, gameObject);
|
||||
|
||||
return behaviourController;
|
||||
}
|
||||
}
|
27
Engine.Core/Factory/BehaviourFactory.cs
Normal file
27
Engine.Core/Factory/BehaviourFactory.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
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;
|
||||
}
|
||||
}
|
23
Engine.Core/Factory/FactoryBase.cs
Normal file
23
Engine.Core/Factory/FactoryBase.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using Syntriax.Engine.Core.Factory.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Core.Factory;
|
||||
|
||||
public abstract class FactoryBase<TInterface> : IFactory<TInterface>
|
||||
where TInterface : class
|
||||
{
|
||||
public virtual T Get<T>(params object?[]? args) where T : class, TInterface
|
||||
{
|
||||
T? result;
|
||||
|
||||
if (args is not null && args.Length != 0)
|
||||
result = Activator.CreateInstance(typeof(T), args) as T;
|
||||
else
|
||||
result = Activator.CreateInstance(typeof(T)) as T;
|
||||
|
||||
if (result is null)
|
||||
throw new Exception($"{typeof(TInterface).Name} of type {typeof(T).Name} could not be created.");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
39
Engine.Core/Factory/GameObjectFactory.cs
Normal file
39
Engine.Core/Factory/GameObjectFactory.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
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;
|
||||
}
|
||||
}
|
19
Engine.Core/Factory/StateEnableFactory.cs
Normal file
19
Engine.Core/Factory/StateEnableFactory.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
using Syntriax.Engine.Core.Exceptions;
|
||||
|
||||
namespace Syntriax.Engine.Core.Factory;
|
||||
|
||||
public class StateEnableFactory
|
||||
{
|
||||
public IStateEnable Instantiate(IEntity entity) => Instantiate<StateEnable>(entity);
|
||||
|
||||
public T Instantiate<T>(IEntity entity, params object?[]? args) where T : class, IStateEnable
|
||||
{
|
||||
T stateEnable = TypeFactory.Get<T>(args);
|
||||
|
||||
if (!stateEnable.Assign(entity))
|
||||
throw AssignException.From(stateEnable, entity);
|
||||
|
||||
return stateEnable;
|
||||
}
|
||||
}
|
10
Engine.Core/Factory/TransformFactory.cs
Normal file
10
Engine.Core/Factory/TransformFactory.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Core.Factory;
|
||||
|
||||
public class TransformFactory
|
||||
{
|
||||
public ITransform Instantiate() => TypeFactory.Get<Transform>();
|
||||
public T Instantiate<T>(params object?[]? args) where T : class, ITransform
|
||||
=> TypeFactory.Get<T>(args);
|
||||
}
|
21
Engine.Core/Factory/TypeFactory.cs
Normal file
21
Engine.Core/Factory/TypeFactory.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Factory;
|
||||
|
||||
public static class TypeFactory
|
||||
{
|
||||
public static T Get<T>(params object?[]? args) where T : class
|
||||
{
|
||||
T? result;
|
||||
|
||||
if (args is not null && args.Length != 0)
|
||||
result = Activator.CreateInstance(typeof(T), args) as T;
|
||||
else
|
||||
result = Activator.CreateInstance(typeof(T)) as T;
|
||||
|
||||
if (result is null)
|
||||
throw new Exception($"{typeof(T).Name} of type {typeof(T).Name} could not be created.");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user