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