61 lines
2.5 KiB
C#
61 lines
2.5 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Encompass {
|
|
public abstract class Engine {
|
|
public readonly List<Type> mutateComponentTypes = new List<Type>();
|
|
|
|
private EntityManager entityManager;
|
|
private ComponentManager componentManager;
|
|
|
|
public Engine() {
|
|
var mutatesAttribute = this.GetType().GetCustomAttribute<Mutates>(false);
|
|
if (mutatesAttribute != null) {
|
|
mutateComponentTypes = mutatesAttribute.mutateComponentTypes;
|
|
}
|
|
}
|
|
|
|
internal void AssignEntityManager(EntityManager entityManager) {
|
|
this.entityManager = entityManager;
|
|
}
|
|
|
|
internal void AssignComponentManager(ComponentManager componentManager) {
|
|
this.componentManager = componentManager;
|
|
}
|
|
|
|
public abstract void Update(float dt);
|
|
|
|
protected Entity CreateEntity() {
|
|
return this.entityManager.CreateEntity();
|
|
}
|
|
|
|
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent {
|
|
return this.componentManager.GetActiveComponentsByType<TComponent>();
|
|
}
|
|
|
|
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent {
|
|
return this.componentManager.GetActiveComponentByType<TComponent>();
|
|
}
|
|
|
|
internal void UpdateComponentInWorld<TComponent>(TComponent originalComponent, TComponent newComponent) where TComponent : struct, IComponent {
|
|
if (mutateComponentTypes.Contains(typeof(TComponent))) {
|
|
this.componentManager.UpdateComponent(originalComponent, newComponent);
|
|
} else {
|
|
throw new IllegalComponentMutationException("Engine {0} tried to mutate undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
protected void UpdateComponent<TComponent>(TComponent component, Func<TComponent, TComponent> updateFunction) where TComponent : struct, IComponent {
|
|
var updatedComponent = updateFunction(component);
|
|
this.UpdateComponentInWorld(component, updatedComponent);
|
|
}
|
|
|
|
protected void UpdateComponents<TComponent>(IEnumerable<TComponent> components, Func<TComponent, TComponent> updateFunction) where TComponent : struct, IComponent {
|
|
foreach (var component in components) {
|
|
this.UpdateComponent(component, updateFunction);
|
|
}
|
|
}
|
|
}
|
|
}
|