chore: Added Initial Engine Code

This commit is contained in:
2023-11-23 22:07:49 +03:00
parent a47586851c
commit 5fcf63c5a7
40 changed files with 1773 additions and 0 deletions

View 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;
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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);
}

View 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;
}
}