Пример #1
0
 public void Recycle(IEcsComponent item)
 {
     if (item != null)
     {
         _pool.Push(item);
     }
 }
Пример #2
0
 void OnComponentDetach(int entity, IEcsComponent component)
 {
     if (component is GameObjectComponent)
     {
         GameObject.Destroy((component as GameObjectComponent).Data);
     }
 }
Пример #3
0
        public void AddComponent(int index, IEcsComponent comp)
        {
            if (index >= _comps.Length)
            {
                Array.Resize(ref _comps, 2 * _comps.Length);
            }

            _comps[index] = (TC)comp;
        }
Пример #4
0
        public void DestroyComponentInstance(Type componentType, IEcsComponent instance)
        {
            IEcsComponentManager existing;

            if (!ComponentManagers.TryGetValue(componentType, out existing))
            {
                return;
            }
            existing.UnRegisterComponent(instance);
        }
Пример #5
0
        // TODO: Make a FilterCollection or something that does all this so systems can have multiple filters running.
        private void OnComponentAdded(int entity, IEcsComponent component)
        {
            if (!entities.Contains(entity) && world.MatchesComponentFilter <TComponentFilter>(entity))
            {
                TComponentFilter filter = world.CreateComponentFilter <TComponentFilter>(entity);
                filters.Add(filter);
                entities.Add(entity);

                Console.WriteLine(GetType().Name + " will process " + entity);
            }
        }
Пример #6
0
        private void OnComponentRemoved(int entity, IEcsComponent component)
        {
            int index = entities.IndexOf(entity);

            if (index >= 0 && !world.MatchesComponentFilter <TComponentFilter>(entity))
            {
                filters.RemoveAt(index);
                entities.RemoveAt(index);

                Console.WriteLine(GetType().Name + " will no longer process " + entity + " because it does not have the required components.");
            }
        }
Пример #7
0
        public bool TryGetComponent(int entityId, int componentId, out IEcsComponent component)
        {
            IEcsComponentManager manager;

            if (ComponentManagersById.TryGetValue(componentId, out manager))
            {
                component = manager.ForEntity(entityId);
                return(true);
            }
            component = null;
            return(false);
        }
Пример #8
0
        public void RegisterComponentInstance(Type componentType, IEcsComponent instance)
        {
            IEcsComponentManager existing;

            if (!ComponentManagers.TryGetValue(componentType, out existing))
            {
                var type = typeof(EcsComponentManagerOf <>).MakeGenericType(componentType);
                existing = Activator.CreateInstance(type) as EcsComponentManager;
                ComponentManagers.Add(componentType, existing);
            }
            existing.RegisterComponent(instance);
        }
Пример #9
0
        public void DestroyComponentInstance(Type componentType, IEcsComponent instance)
        {
            IEcsComponentManager existing;

            if (!ComponentManagers.TryGetValue(componentType, out existing))
            {
                return;
            }
            if (_componentRemovedSubject != null)
            {
                _componentRemovedSubject.OnNext(instance);
            }
            existing.UnRegisterComponent(instance);
        }
Пример #10
0
 protected override void AddItem(IEcsComponent component)
 {
     if (_components.ContainsKey(component.EntityId))
     {
         return;
     }
     else
     {
         _components.Add(component.EntityId, (TComponentType)component);
         _componentList.Add((TComponentType)component);
     }
     if (_CreatedObservable != null)
     {
         _CreatedObservable.OnNext((TComponentType)component);
     }
 }
Пример #11
0
        internal void AddComponent(byte index, IEcsComponent component)
        {
            EcsArchetype newArchetype = _archetypeManager.FindOrCreateNextArchetype(_archetype, index);

            foreach (byte curIndex in _archetype.Indices)
            {
                IEcsComponentPool componentPool = _archetype.GetComponentPool(curIndex);
                newArchetype.AddComponent(curIndex, componentPool.Get(ArchetypeIndex));
            }

            newArchetype.AddComponent(index, component);

            _archetype.RemoveEntity(this);
            _archetype = newArchetype;
            _archetype.AddEntity(this);
        }
Пример #12
0
        protected override void RemoveItem(IEcsComponent component)
        {
            if (component == null)
            {
                return;
            }
            if (_components == null || !_components.ContainsKey(component.EntityId))
            {
                return;
            }

            _components.Remove(component.EntityId);
            _componentList.Remove((TComponentType)component);

            if (_RemovedObservable != null)
            {
                _RemovedObservable.OnNext((TComponentType)component);
            }
        }
Пример #13
0
        /// <summary>
        /// This method should be used to add any entity to the ecs component system
        ///
        /// > You can use this if you want to register components that aren't derived from EcsComponent which requires MonoBehaviour, but you won't be able to see it in the unity inspector.
        /// </summary>
        /// <param name="componentType">The type of component to register.</param>
        /// <param name="instance">The actual instance that is being registered</param>
        /// <code>
        /// System.ComponentSystem.RegisterComponent(typeof(CustomComponent), new CustomComponent());
        /// </code>
        public void RegisterComponentInstance(Type componentType, IEcsComponent instance)
        {
            IEcsComponentManager existing;

            if (!ComponentManagers.TryGetValue(componentType, out existing))
            {
                var type = typeof(EcsComponentManagerOf <>).MakeGenericType(componentType);
                existing = Activator.CreateInstance(type) as EcsComponentManager;
                ComponentManagers.Add(componentType, existing);
                if (instance.ComponentId > 0)
                {
                    ComponentManagersById.Add(instance.ComponentId, existing);
                }
            }
            if (_componentCreatedSubject != null)
            {
                _componentCreatedSubject.OnNext(instance);
            }
            existing.RegisterComponent(instance);
        }
        public int GetIndex()
        {
            int id;

            if (_reservedItemsCount > 0)
            {
                _reservedItemsCount--;
                id = _reservedItems[_reservedItemsCount];
                _reservedItems.RemoveAt(_reservedItemsCount);
            }
            else
            {
                id = _itemsCount;
                if (_itemsCount == Items.Length)
                {
                    var newItems = new IEcsComponent[_itemsCount << 1];
                    Array.Copy(Items, newItems, _itemsCount);
                    Items = newItems;
                }
                Items[_itemsCount++] = Activator.CreateInstance(_type) as IEcsComponent;
            }
            return(id);
        }
 protected abstract void RemoveItem(IEcsComponent component);
Пример #16
0
        public static T As <T>(this IEcsComponent component) where T : class, IEcsComponent
        {
            IEcsComponentManager manager = EcsComponentService.Instance.RegisterComponent(typeof(T));

            return((T)manager.ForEntity(component.EntityId));
        }
 public void RegisterComponent(IEcsComponent item)
 {
     AddItem(item);
 }
 public void UnRegisterComponent(IEcsComponent item)
 {
     RemoveItem(item);
 }
 protected abstract void AddItem(IEcsComponent component);
Пример #20
0
 public void AddComponent(byte index, IEcsComponent component)
 {
     _compPools[index].AddComponent(_length, component);
 }
Пример #21
0
 public bool TryGetComponent(int entity, Type componentType, out IEcsComponent component)
 {
     return(components[componentType].TryGetValue(entity, out component));
 }
Пример #22
0
 protected abstract void AddItem(IEcsComponent component);
Пример #23
0
 public void RegisterComponent(IEcsComponent item)
 {
     AddItem(item);
 }
Пример #24
0
 public void UnRegisterComponent(IEcsComponent item)
 {
     RemoveItem(item);
 }
Пример #25
0
 protected override void AddItem(IEcsComponent component)
 {
     base.AddItem(component);
 }
Пример #26
0
 protected abstract void RemoveItem(IEcsComponent component);
Пример #27
0
 private void OnNext(IEcsComponent _)
 {
     AddItem(_);
 }