public void RemoveHecsComponent <T>() where T : IComponent { var index = TypesMap.GetIndexByType <T>(); if (components[index] != null) { RemoveHecsComponent(components[index]); } }
/// <summary> /// Base method for add hecs component, all methods with adds component functionality shoulds use this method at the end /// </summary> /// <param name="component"></param> /// <param name="owner">this for actor, actor can assign self as owner</param> /// <param name="silently"></param> public void AddHecsComponent(IComponent component, IEntity owner = null, bool silently = false) { if (component == null) { throw new Exception($"compontent is null " + ID); } if (TypesMap.GetComponentInfo(component.GetTypeHashCode, out var info)) { component.ComponentsMask = info.ComponentsMask; } else { throw new Exception("we dont have needed type in TypesMap, u need to run codogen or check this type manualy" + component.GetType().Name); } if (components[component.ComponentsMask.Index] != null) { return; } if (owner == null) { component.Owner = this; } else { component.Owner = owner; ComponentAdditionalProcessing(component, owner); } components[component.ComponentsMask.Index] = component; ComponentContext.AddComponent(component); if (IsInited) { if (component is IInitable initable) { initable.Init(); } if (component is IAfterEntityInit afterEntityInit) { afterEntityInit.AfterEntityInit(); } } ComponentsMask += component.ComponentsMask; if (!silently && IsInited) { EntityManager.AddOrRemoveComponent(component, true); } }
public T GetOrAddComponent <T>(IEntity owner = null) where T : class, IComponent { var index = TypesMap.GetIndexByType <T>(); var needed = components[index]; if (needed != null) { return((T)needed); } var newComp = TypesMap.GetComponentFromFactory <T>(); AddHecsComponent(newComp, owner); return(newComp); }
T IHECSFactory.GetComponentFromFactory <T>() { var hash = TypesMap.GetHashOfComponentByType(typeof(T)); return((T)GetComponentFromFactory(hash)); }
public bool ContainsMask <T>() where T : IComponent { var index = TypesMap.GetIndexByType <T>(); return(components[index] != null); }