示例#1
0
        public void RemoveHecsComponent <T>() where T : IComponent
        {
            var index = TypesMap.GetIndexByType <T>();

            if (components[index] != null)
            {
                RemoveHecsComponent(components[index]);
            }
        }
示例#2
0
        /// <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);
            }
        }
示例#3
0
        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);
        }
示例#4
0
        T IHECSFactory.GetComponentFromFactory <T>()
        {
            var hash = TypesMap.GetHashOfComponentByType(typeof(T));

            return((T)GetComponentFromFactory(hash));
        }
示例#5
0
        public bool ContainsMask <T>() where T : IComponent
        {
            var index = TypesMap.GetIndexByType <T>();

            return(components[index] != null);
        }