encompass-cs/src/Engine.cs

50 lines
1.9 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>();
}
protected void UpdateComponent<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().ToString(), typeof(TComponent).ToString());
}
}
}
}