public virtual void AddEntity(Entity instance) { if (instance == null) { Debug.LogError("OverallEntitySystem--AddEntity should not be passed a null value"); return; } if (this.GetEntity(instance.name) != null) { UnityEngine.Debug.LogError("StatComponentSystem -- You cannot add 2 statistics of the same name -- A statistic of name " + instance.name + " already exists in this System"); return; } instance.OnComponentsChanged += this.ResetCaches; this.entities.Add(instance); this.ResetCaches(); foreach (Component component in instance.GetComponents <Component>()) { foreach (object attribute in component.GetType().GetCustomAttributes(true)) { if (attribute is PartOfComponentSystemAttribute) { PartOfComponentSystemAttribute pocsa = (PartOfComponentSystemAttribute)attribute; bool isGlobalSystem = false; foreach (object parentSystemAttribute in pocsa.parentSystemType.GetCustomAttributes(true)) { if (parentSystemAttribute is GlobalComponentSystemAttribute) { isGlobalSystem = true; } } if (isGlobalSystem) { continue; } ComponentSystem system = (ComponentSystem)typeof(EntitySystem) .GetMethod("GetSystem") .MakeGenericMethod(pocsa.parentSystemType) .Invoke(this, null); if (system == null) { system = (ComponentSystem)pocsa.parentSystemType.GetConstructor(new Type[] { }).Invoke(null, null); this.AddSystem(system); } system.AddComponent(component); } } } instance.SetOverallSystem(this); }
public virtual void AddComponent(Component instance) { bool allowSearch = true; Type instanceType = instance.GetType(); while (allowSearch) { if (instanceType == typeof(Component)) { break; } foreach (object attribute in instanceType.GetCustomAttributes(false)) { if (attribute is AllowMultipleAttribute) { allowSearch = false; break; } } if (allowSearch && typeof(Entity).GetMethod("GetComponent").MakeGenericMethod(instanceType).Invoke(this, null) != null) { Debug.LogError(string.Format( "Entity--{0}--AddComponent--Cannot have two instances of a Component--{1}--on a single Entity", this.name, instanceType.ToString())); return; } instanceType = instanceType.BaseType; } // TODO: Merge these two searches of the attributes of the Instance's Type foreach (object attribute in instance.GetType().GetCustomAttributes(true)) { if (attribute is RequireComponentAttribute) { foreach (Type type in ((RequireComponentAttribute)attribute).required) { if (typeof(Entity).GetMethod("GetComponent").MakeGenericMethod(type).Invoke(this, null) == null) { this.AddComponent((Component)type.GetConstructor(new Type[] { }).Invoke(null, null)); } } } } this.components.Add(instance); instance.SetEntity(this); foreach (object attribute in instance.GetType().GetCustomAttributes(true)) { if (attribute is PartOfComponentSystemAttribute) { PartOfComponentSystemAttribute pocsa = (PartOfComponentSystemAttribute)attribute; foreach ( object parentSystemAttribute in pocsa.parentSystemType.GetCustomAttributes(true)) { if (parentSystemAttribute is GlobalComponentSystemAttribute) { ((ComponentSystem)pocsa .parentSystemType .GetProperty("Instance") .GetValue(null, null)) .AddComponent(instance); } } } } if (this.OnComponentsChanged != null) { this.OnComponentsChanged(); } this.ResetCaches(); }