Exemplo n.º 1
0
        /// <summary>
        /// Registers a a reactive group with the list of managers.  If the group already exists it will return it, if not it will create a new one and return that.
        /// </summary>
        /// <param name="componentId"></param>
        /// <typeparam name="TGroupType">The group type class. Usually derives from ReactiveGroup </typeparam>
        /// <typeparam name="TComponent"></typeparam>
        /// <returns>The instance of the group manager.</returns>
        public TGroupType RegisterGroup <TGroupType, TComponent>(int componentId = 0)
            where TComponent : IEcsComponent
            where TGroupType : IReactiveGroup, new()
        {
            IEcsComponentManager existing;

            if (!ComponentManagers.TryGetValue(typeof(TComponent), out existing))
            {
                existing = new TGroupType();
                ComponentManagers.Add(typeof(TComponent), existing);
                if (existing.ComponentId > 0)
                {
                    try
                    {
                        ComponentManagersById.Add(existing.ComponentId, existing);
                    }
                    catch (ArgumentException ex)
                    {
                        Debug.LogErrorFormat("Cannot register component {0} with ID {1}. Component with such Id is already registered: {2}.", typeof(TGroupType).Name, existing.ComponentId,
                                             ComponentManagersById[existing.ComponentId].For.Name);
                    }
                }
                return((TGroupType)existing);
            }
            else
            {
                return((TGroupType)existing);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers a component type with the component system.
        /// </summary>
        /// <param name="componentType">The type of component to register</param>
        /// <returns></returns>
        /// <code>
        /// var componentManager = System.ComponentSystem.RegisterComponent(typeof(PlayerComponent));
        /// foreach (var item in componentManager) {
        ///     Debug.Log(item.name);
        /// }
        /// </code>
        public IEcsComponentManager RegisterComponent(Type componentType)
        {
            IEcsComponentManager existing;

            if (!ComponentManagers.TryGetValue(componentType, out existing))
            {
                throw new Exception(string.Format("Component {0} not registered correctly.", componentType.Name));
            }
            return(existing);
        }
Exemplo n.º 3
0
        public IEnumerable <T> GetAllComponents <T>() where T : class, IEcsComponent
        {
            IEcsComponentManager existing;

            if (ComponentManagers.TryGetValue(typeof(T), out existing))
            {
                var manager = (EcsComponentManagerOf <T>)existing;
                foreach (var item in manager.Components)
                {
                    yield return((T)item);
                }
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public override void Loaded()
        {
            base.Loaded();
            var array = ComponentManagers.Where(p => typeof(GroupItem).IsAssignableFrom(p.Key)).ToArray();

            foreach (var item in array)
            {
                var reactiveGroup = item.Value as IReactiveGroup;
                if (reactiveGroup != null)
                {
                    foreach (var observable in reactiveGroup.Install(this))
                    {
                        observable.Subscribe(reactiveGroup.UpdateItem)
                        .DisposeWith(this);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public bool TryGetComponent <TComponent>(int entityId, out TComponent component) where TComponent : class, IEcsComponent
        {
            IEcsComponentManager existing;

            if (!ComponentManagers.TryGetValue(typeof(TComponent), out existing))
            {
                component = null;
                return(false);
            }
            var manager = (IEcsComponentManagerOf <TComponent>)existing;
            var result  = manager.Components.FirstOrDefault(p => p.EntityId == entityId);

            if (result != null)
            {
                component = result;
                return(true);
            }
            component = null;
            return(false);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        public IEcsComponentManagerOf <TComponent> RegisterComponent <TComponent>(int componentId = 0) where TComponent : class, IEcsComponent
        {
            IEcsComponentManager existing;

            if (!ComponentManagers.TryGetValue(typeof(TComponent), out existing))
            {
                existing             = new EcsComponentManagerOf <TComponent>();
                existing.ComponentId = componentId;
                ComponentManagers.Add(typeof(TComponent), existing);
                //if (componentId > 0)
                //    ComponentManagersById.Add(componentId, existing);
                //else
                //{
                //    // Throw warning here?
                //}
                return((IEcsComponentManagerOf <TComponent>)existing);
            }
            else
            {
                return((IEcsComponentManagerOf <TComponent>)existing);
            }
        }
Exemplo n.º 9
0
 internal ComponentManager GetNodeTypeHandler(NodeType handlesNodeType) => ComponentManagers.FirstOrDefault(c => c.HandlesNodeType(handlesNodeType));
Exemplo n.º 10
0
 internal T GetComponent <T>() where T : ComponentManager => ComponentManagers.OfType <T>().FirstOrDefault();