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.

NodeBuilder.cs 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. namespace Svelto.ECS
  5. {
  6. public interface INodeBuilder
  7. {
  8. INode BuildNodeAndAddToList(ref ITypeSafeList list, int entityID);
  9. Type GetNodeType();
  10. }
  11. public class NodeBuilder<NodeType> : INodeBuilder where NodeType : NodeWithID, new()
  12. {
  13. public INode BuildNodeAndAddToList(ref ITypeSafeList list, int entityID)
  14. {
  15. if (list == null)
  16. list = new TypeSafeFasterListForECSForClasses<NodeType>();
  17. var castedList = list as TypeSafeFasterListForECSForClasses<NodeType>;
  18. var node = NodeWithID.BuildNode<NodeType>(entityID);
  19. castedList.Add(node);
  20. return node;
  21. }
  22. public Type GetNodeType()
  23. {
  24. return typeof(NodeType);
  25. }
  26. }
  27. public class StructNodeBuilder<NodeType> : INodeBuilder where NodeType : struct, IStructNodeWithID
  28. {
  29. public INode BuildNodeAndAddToList(ref ITypeSafeList list, int entityID)
  30. {
  31. var node = default(NodeType);
  32. node.ID = entityID;
  33. if (list == null)
  34. list = new TypeSafeFasterListForECSForStructs<NodeType>();
  35. var castedList = list as TypeSafeFasterListForECSForStructs<NodeType>;
  36. castedList.Add(node);
  37. return null;
  38. }
  39. public Type GetNodeType()
  40. {
  41. return typeof(NodeType);
  42. }
  43. }
  44. }