changing everything to structs
parent
f3fa440583
commit
e4b4e47327
|
@ -1,4 +1,4 @@
|
|||
namespace Encompass
|
||||
{
|
||||
public abstract class Component {}
|
||||
public interface IComponent {}
|
||||
}
|
||||
|
|
|
@ -4,49 +4,45 @@ using System.Linq;
|
|||
|
||||
namespace Encompass {
|
||||
internal class ComponentManager {
|
||||
private Dictionary<uint, List<Component>> entityIDToComponents = new Dictionary<uint, List<Component>>();
|
||||
private Dictionary<uint, List<IComponent>> entityIDToComponents = new Dictionary<uint, List<IComponent>>();
|
||||
|
||||
private Dictionary<Type, List<Component>> activeComponents = new Dictionary<Type, List<Component>>();
|
||||
private Dictionary<Type, List<Component>> inactiveComponents = new Dictionary<Type, List<Component>>();
|
||||
private Dictionary<Type, List<IComponent>> activeComponents = new Dictionary<Type, List<IComponent>>();
|
||||
private Dictionary<Type, List<IComponent>> inactiveComponents = new Dictionary<Type, List<IComponent>>();
|
||||
|
||||
private List<Component> componentsToActivate = new List<Component>();
|
||||
private List<Component> componentsToDeactivate = new List<Component>();
|
||||
private List<Component> componentsToRemove = new List<Component>();
|
||||
|
||||
internal TComponent CreateComponent<TComponent>(uint entityID) where TComponent : Component, new() {
|
||||
var component = new TComponent();
|
||||
private List<IComponent> componentsToActivate = new List<IComponent>();
|
||||
private List<IComponent> componentsToDeactivate = new List<IComponent>();
|
||||
private List<IComponent> componentsToRemove = new List<IComponent>();
|
||||
|
||||
internal void AddComponent<TComponent>(uint entityID, TComponent component) where TComponent : struct, IComponent {
|
||||
if (!entityIDToComponents.ContainsKey(entityID)) {
|
||||
entityIDToComponents.Add(entityID, new List<Component>());
|
||||
entityIDToComponents.Add(entityID, new List<IComponent>());
|
||||
}
|
||||
|
||||
entityIDToComponents[entityID].Add(component);
|
||||
|
||||
if (!activeComponents.ContainsKey(typeof(TComponent))) {
|
||||
activeComponents.Add(typeof(TComponent), new List<Component>());
|
||||
inactiveComponents.Add(typeof(TComponent), new List<Component>());
|
||||
activeComponents.Add(typeof(TComponent), new List<IComponent>());
|
||||
inactiveComponents.Add(typeof(TComponent), new List<IComponent>());
|
||||
}
|
||||
|
||||
MarkForActivation(component);
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
internal IEnumerable<Component> GetComponentsByEntity(uint entityID) {
|
||||
internal IEnumerable<IComponent> GetComponentsByEntity(uint entityID) {
|
||||
return entityIDToComponents[entityID];
|
||||
}
|
||||
|
||||
internal IEnumerable<TComponent> GetActiveComponentsByType<TComponent>() where TComponent : Component {
|
||||
internal IEnumerable<TComponent> GetActiveComponentsByType<TComponent>() where TComponent : struct, IComponent {
|
||||
return activeComponents[typeof(TComponent)].Cast<TComponent>();
|
||||
}
|
||||
|
||||
internal IEnumerable<TComponent> GetComponentsByEntityAndType<TComponent>(uint entityID) where TComponent : Component {
|
||||
var entity_components = GetComponentsByEntity(entityID);
|
||||
internal IEnumerable<TComponent> GetComponentsByEntityAndType<TComponent>(uint entityID) where TComponent : struct, IComponent {
|
||||
var entity_components = GetComponentsByEntity(entityID).Cast<TComponent>();
|
||||
var active_components_by_type = GetActiveComponentsByType<TComponent>();
|
||||
return entity_components.Intersect(active_components_by_type).Cast<TComponent>();
|
||||
}
|
||||
|
||||
internal bool EntityHasComponentOfType<TComponent>(uint entityID) where TComponent : Component {
|
||||
internal bool EntityHasComponentOfType<TComponent>(uint entityID) where TComponent : struct, IComponent {
|
||||
return GetComponentsByEntityAndType<TComponent>(entityID).Any();
|
||||
}
|
||||
|
||||
|
@ -61,11 +57,11 @@ namespace Encompass {
|
|||
entityIDToComponents.Remove(entityID);
|
||||
}
|
||||
|
||||
internal void MarkForActivation(Component component) {
|
||||
internal void MarkForActivation(IComponent component) {
|
||||
componentsToActivate.Add(component);
|
||||
}
|
||||
|
||||
internal void MarkForRemoval(Component component) {
|
||||
internal void MarkForRemoval(IComponent component) {
|
||||
componentsToRemove.Add(component);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ using System.Linq;
|
|||
|
||||
namespace Encompass
|
||||
{
|
||||
public class Entity
|
||||
public struct Entity
|
||||
{
|
||||
public readonly uint id;
|
||||
|
||||
|
@ -15,19 +15,19 @@ namespace Encompass
|
|||
this.componentManager = componentManager;
|
||||
}
|
||||
|
||||
public TComponent AddComponent<TComponent>() where TComponent : Component, new() {
|
||||
return componentManager.CreateComponent<TComponent>(id);
|
||||
public void AddComponent<TComponent>(TComponent component) where TComponent : struct, IComponent {
|
||||
componentManager.AddComponent<TComponent>(id, component);
|
||||
}
|
||||
|
||||
public IEnumerable<TComponent> GetComponents<TComponent>() where TComponent : Component {
|
||||
public IEnumerable<TComponent> GetComponents<TComponent>() where TComponent : struct, IComponent {
|
||||
return componentManager.GetComponentsByEntityAndType<TComponent>(id);
|
||||
}
|
||||
|
||||
public TComponent GetComponent<TComponent>() where TComponent : Component {
|
||||
public TComponent GetComponent<TComponent>() where TComponent : struct, IComponent {
|
||||
return GetComponents<TComponent>().First();
|
||||
}
|
||||
|
||||
public bool HasComponent<TComponent>() where TComponent : Component {
|
||||
public bool HasComponent<TComponent>() where TComponent : struct, IComponent {
|
||||
return componentManager.EntityHasComponentOfType<TComponent>(id);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ using System.Linq;
|
|||
namespace Encompass
|
||||
{
|
||||
namespace Tests {
|
||||
class MockComponent : Component {
|
||||
struct MockComponent : IComponent {
|
||||
public string myString;
|
||||
public int myInt;
|
||||
}
|
||||
|
@ -17,9 +17,11 @@ namespace Encompass
|
|||
var world = new World();
|
||||
var entity = world.CreateEntity();
|
||||
|
||||
var mockComponent = entity.AddComponent<MockComponent>();
|
||||
mockComponent.myString = "hello";
|
||||
MockComponent mockComponent;
|
||||
mockComponent.myInt = 3;
|
||||
mockComponent.myString = "hello";
|
||||
|
||||
entity.AddComponent<MockComponent>(mockComponent);
|
||||
|
||||
world.Update();
|
||||
|
||||
|
@ -33,9 +35,21 @@ namespace Encompass
|
|||
var world = new World();
|
||||
var entity = world.CreateEntity();
|
||||
|
||||
var mockComponentA = entity.AddComponent<MockComponent>();
|
||||
var mockComponentB = entity.AddComponent<MockComponent>();
|
||||
var mockComponentC = entity.AddComponent<MockComponent>();
|
||||
MockComponent mockComponentA;
|
||||
mockComponentA.myInt = 3;
|
||||
mockComponentA.myString = "hello";
|
||||
|
||||
MockComponent mockComponentB;
|
||||
mockComponentB.myInt = 5;
|
||||
mockComponentB.myString = "wassup";
|
||||
|
||||
MockComponent mockComponentC;
|
||||
mockComponentC.myInt = 1;
|
||||
mockComponentC.myString = "howdy";
|
||||
|
||||
entity.AddComponent<MockComponent>(mockComponentA);
|
||||
entity.AddComponent<MockComponent>(mockComponentB);
|
||||
entity.AddComponent<MockComponent>(mockComponentC);
|
||||
|
||||
world.Update();
|
||||
|
||||
|
@ -50,9 +64,11 @@ namespace Encompass
|
|||
var world = new World();
|
||||
var entity = world.CreateEntity();
|
||||
|
||||
var mockComponent = entity.AddComponent<MockComponent>();
|
||||
mockComponent.myString = "hello";
|
||||
MockComponent mockComponent;
|
||||
mockComponent.myInt = 3;
|
||||
mockComponent.myString = "hello";
|
||||
|
||||
entity.AddComponent<MockComponent>(mockComponent);
|
||||
|
||||
world.Update();
|
||||
|
||||
|
@ -65,7 +81,11 @@ namespace Encompass
|
|||
var world = new World();
|
||||
var entity = world.CreateEntity();
|
||||
|
||||
var mockComponent = entity.AddComponent<MockComponent>();
|
||||
MockComponent mockComponent;
|
||||
mockComponent.myInt = 3;
|
||||
mockComponent.myString = "hello";
|
||||
|
||||
entity.AddComponent<MockComponent>(mockComponent);
|
||||
|
||||
world.Update();
|
||||
|
||||
|
|
Loading…
Reference in New Issue