Entitas  0.35.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
EntityExtension.cs
2 
3 namespace Entitas {
4 
5  public static class EntityExtension {
6 
7  public const string COMPONENT_SUFFIX = "Component";
8 
9  public static string AddComponentSuffix(this string componentName) {
10  return componentName.EndsWith(COMPONENT_SUFFIX, System.StringComparison.Ordinal)
11  ? componentName
12  : componentName + COMPONENT_SUFFIX;
13  }
14 
15  public static string RemoveComponentSuffix(this string componentName) {
16  return componentName.EndsWith(COMPONENT_SUFFIX, System.StringComparison.Ordinal)
17  ? componentName.Substring(0, componentName.Length - COMPONENT_SUFFIX.Length)
18  : componentName;
19  }
20 
21  /// Adds copies of all specified components to the target entity.
22  /// If replaceExisting is true it will replace exisintg components.
23  public static void CopyTo(this Entity entity,
24  Entity target,
25  bool replaceExisting = false,
26  params int[] indices) {
27  var componentIndices = indices.Length == 0
28  ? entity.GetComponentIndices()
29  : indices;
30  for(int i = 0; i < componentIndices.Length; i++) {
31  var index = componentIndices[i];
32  var component = entity.GetComponent(index);
33  var clonedComponent = target.CreateComponent(
34  index, component.GetType()
35  );
36  component.CopyPublicMemberValues(clonedComponent);
37 
38  if(replaceExisting) {
39  target.ReplaceComponent(index, clonedComponent);
40  } else {
41  target.AddComponent(index, clonedComponent);
42  }
43  }
44  }
45  }
46 }
Entity ReplaceComponent(int index, IComponent component)
Definition: Entity.cs:178
static void CopyTo(this Entity entity, Entity target, bool replaceExisting=false, params int[] indices)
IComponent CreateComponent(int index, Type type)
Definition: Entity.cs:342
IComponent GetComponent(int index)
Definition: Entity.cs:230
int [] GetComponentIndices()
Returns all indices of added components.
Definition: Entity.cs:266
Entity AddComponent(int index, IComponent component)
Definition: Entity.cs:114