示例#1
0
        public void Remove(Component component)
        {
            var idx = ComponentTypeManager.GetIndexFor(component.GetType());

            this.componentsToAdd.Remove(idx);
            this.componentIdxToRemove.Add(idx);
        }
示例#2
0
        public void Remove <T>() where T : Component
        {
            var idx = ComponentTypeManager.GetIndexFor(typeof(T));

            this.componentsToAdd.Remove(idx);
            this.componentIdxToRemove.Add(idx);
        }
示例#3
0
        public T Add <T>(T component) where T : Component
        {
            var idx = ComponentTypeManager.GetIndexFor(component.GetType());

            this.componentsToAdd.Add(idx, component);
            return(component);
        }
示例#4
0
        private void ComponentEnabled(Component component)
        {
            var idx = ComponentTypeManager.GetIndexFor(component.GetType());

            this.componentIdxToDisable.Remove(idx);
            this.componentIdxToEnable.Add(idx);
        }
示例#5
0
        public void RemoveComponent <T>()
            where T : UnityEngine.Component
        {
            var c = _gameObject.GetComponent <T>();

            GameObject.Destroy(c);
            this.ComponentBits.set(ComponentTypeManager.GetIndexFor(typeof(T)), false);
            _scene.EntityProcessors.onComponentRemoved(this);
        }
示例#6
0
        public T AddComponent <T>()
            where T : UnityEngine.Component
        {
            var c = _gameObject.AddComponent <T>();

            if (c is BaseMonoBehaviour)
            {
                object b = (object)c;
                ((BaseMonoBehaviour)b).Entity = this;
            }
            this.ComponentBits.set(ComponentTypeManager.GetIndexFor(typeof(T)));
            _scene.EntityProcessors.onComponentAdded(this);
            return(c);
        }
示例#7
0
 public static void Destroy(Entity entity)
 {
     foreach (var c in entity.GameObject.GetComponents(typeof(MonoBehaviour)))
     {
         //Debug.Log("Removed [" + entity.name + "] " + c.GetType().ToString());
         if (c is Entity)
         {
             continue;
         }
         entity.ComponentBits.set(ComponentTypeManager.GetIndexFor(c.GetType()), false);
         entity.Scene.EntityProcessors.onComponentRemoved(entity);
         GameObject.Destroy(c);
     }
     GameObject.Destroy(entity.GameObject);
 }
示例#8
0
        public T Get <T>(bool withPending = true) where T : Component
        {
            var idx = ComponentTypeManager.GetIndexFor(typeof(T));

            if (this.components.ContainsKey(idx))
            {
                return((T)this.components[idx]);
            }

            if (withPending && this.componentsToAdd.ContainsKey(idx))
            {
                return((T)this.componentsToAdd[idx]);
            }

            return(null);
        }
示例#9
0
        internal bool CommitChanges()
        {
            if (this.componentIdxToDisable.Count > 0)
            {
                foreach (var idx in this.componentIdxToDisable)
                {
                    this.Bits.Set(idx, false);
                }
            }

            if (this.componentIdxToEnable.Count > 0)
            {
                if (!this.entity.Enabled)
                {
                    this.componentIdxToEnable.Clear();
                }

                foreach (var idx in this.componentIdxToEnable)
                {
                    this.Bits.Set(idx, true);
                }
            }

            if (this.componentIdxToRemove.Count > 0)
            {
                foreach (var idx in this.componentIdxToRemove)
                {
                    this.Bits.Set(idx, false);

                    if (this.components.ContainsKey(idx))
                    {
                        var component = this.components[idx];
                        component.Entity             = null;
                        component.ComponentEnabled  -= this.ComponentEnabled;
                        component.ComponentDisabled -= this.ComponentDisabled;
                        this.components.Remove(idx);
                    }
                }
            }

            if (this.componentsToAdd.Count > 0)
            {
                foreach (var component in this.componentsToAdd)
                {
                    var idx = ComponentTypeManager.GetIndexFor(component.Value.GetType());
                    this.Bits.Set(idx, this.entity.Enabled && component.Value.Enabled);

                    component.Value.Entity             = this.entity;
                    component.Value.ComponentEnabled  += this.ComponentEnabled;
                    component.Value.ComponentDisabled += this.ComponentDisabled;
                    this.components.Add(idx, component.Value);
                }
            }

            var isSomethingChanged = this.componentIdxToRemove.Count > 0 || this.componentsToAdd.Count > 0 ||
                                     this.componentIdxToDisable.Count > 0 ||
                                     this.componentIdxToEnable.Count > 0;

            this.componentIdxToRemove.Clear();
            this.componentsToAdd.Clear();
            this.componentIdxToDisable.Clear();
            this.componentIdxToEnable.Clear();

            return(isSomethingChanged);
        }