Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EntityDescriptor.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Reflection;
  3. using Svelto.DataStructures;
  4. #if NETFX_CORE
  5. using BindingFlags = System.Reflection.BindingFlags;
  6. #endif
  7. namespace Svelto.ECS
  8. {
  9. public class EntityDescriptor
  10. {
  11. protected EntityDescriptor(INodeBuilder[] nodesToBuild, params object[] componentsImplementor)
  12. {
  13. _implementors = componentsImplementor;
  14. _nodesToBuild = nodesToBuild;
  15. }
  16. /* protected EntityDescriptor(IStructNodeBuilder[] structNodesToBuild)
  17. {
  18. _structNodesToBuild = structNodesToBuild;
  19. }*/
  20. public void AddImplementors(params object[] componentsImplementor)
  21. {
  22. var implementors = new object[componentsImplementor.Length + _implementors.Length];
  23. Array.Copy(_implementors, implementors, _implementors.Length);
  24. Array.Copy(componentsImplementor, 0, implementors, _implementors.Length, componentsImplementor.Length);
  25. _implementors = implementors;
  26. }
  27. public virtual FasterList<INode> BuildNodes(int ID, Action<FasterReadOnlyList<INode>> removeAction)
  28. {
  29. var nodes = new FasterList<INode>();
  30. for (int index = _nodesToBuild.Length - 1; index >= 0; index--)
  31. {
  32. var nodeBuilder = _nodesToBuild[index];
  33. var node = FillNode(nodeBuilder.Build(ID), () =>
  34. {
  35. removeAction(new FasterReadOnlyList<INode>());
  36. nodes.Clear();
  37. }
  38. );
  39. nodes.Add(node);
  40. }
  41. return nodes;
  42. }
  43. TNode FillNode<TNode>(TNode node, Action removeAction) where TNode: INode
  44. {
  45. var fields = node.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
  46. for (int i = fields.Length - 1; i >=0 ; --i)
  47. {
  48. var field = fields[i];
  49. Type fieldType = field.FieldType;
  50. object component = null;
  51. for (int j = 0; j < _implementors.Length; j++)
  52. {
  53. var implementor = _implementors[j];
  54. if (implementor != null && fieldType.IsAssignableFrom(implementor.GetType()))
  55. {
  56. component = implementor;
  57. if (fieldType.IsAssignableFrom(typeof(IRemoveEntityComponent)))
  58. (component as IRemoveEntityComponent).removeEntity = removeAction;
  59. break;
  60. }
  61. }
  62. if (component == null)
  63. {
  64. Exception e = new Exception("Svelto.ECS: Implementor not found for a Node. " +
  65. "Implementor Type: " + field.FieldType.Name + " - Node: " + node.GetType().Name + " - EntityDescriptor " + this);
  66. throw e;
  67. }
  68. field.SetValue(node, component);
  69. }
  70. return node;
  71. }
  72. object[] _implementors;
  73. readonly INodeBuilder[] _nodesToBuild;
  74. // readonly IStructNodeBuilder[] _structNodesToBuild;
  75. }
  76. public interface INodeBuilder
  77. {
  78. NodeWithID Build(int ID);
  79. }
  80. public class NodeBuilder<NodeType> : INodeBuilder where NodeType:NodeWithID, new()
  81. {
  82. public NodeWithID Build(int ID)
  83. {
  84. NodeWithID node = NodeWithID.BuildNode<NodeType>(ID);
  85. return (NodeType)node;
  86. }
  87. }
  88. /*
  89. public interface IStructNodeBuilder
  90. {}
  91. public class StructNodeBuilder<NodeType> : IStructNodeBuilder where NodeType : struct
  92. {
  93. public NodeType Build()
  94. {
  95. return new NodeType();
  96. }
  97. }*/
  98. }