Exemple #1
0
        /// <summary>
        /// Adds a component to the actor.
        /// </summary>
        /// <typeparam name="T">The type of the added component</typeparam>
        public T AddComponent <T>() where T : Component, new()
        {
            T component = new T();

            components.Add(component);
            component.Initialize(this);

            // Check if the component implements any of these interfaces, if so then add them to their specific list
            if (component is IUpdateable updateable)
            {
                updateables.Add(updateable);
            }
            if (component is IDrawable drawable)
            {
                drawables.Add(drawable);
            }
            if (component is IDisposable disposable)
            {
                disposables.Add(disposable);
            }

            OnComponentAdded?.Invoke(component);
            Stage.OnComponentAdded(component);
            return(component);
        }
Exemple #2
0
        /// <summary>
        /// Adds a component at the specified index.
        /// You can only have one component at an index.
        /// Each component type must have its own constant index.
        /// The preferred way is to use the
        /// generated methods from the code generator.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="component"></param>
        public void AddComponent(int index, IComponent component)
        {
            if (!_isEnabled)
            {
                throw new EntityIsNotEnabledException(
                          "Cannot add component '" +
                          _contextInfo.componentNames[index] +
                          "' to " +
                          this +
                          "!");
            }

            if (HasComponent(index))
            {
                throw new EntityAlreadyHasComponentException(
                          index,
                          "Cannot add component '" +
                          _contextInfo.componentNames[index] +
                          "' to " +
                          this +
                          "!",
                          "You should check if an entity already has the component " +
                          "before adding it or use entity.ReplaceComponent().");
            }

            _components[index]     = component;
            _componentsCache       = null;
            _componentIndicesCache = null;
            _toStringCache         = null;
            OnComponentAdded?.Invoke(this, index, component);
        }
Exemple #3
0
        public Entity <T> AddInstance <T2>(T2 component) where T2 : T
        {
            if (!_isEnabled)
            {
                throw new EntityIsNotEnabledException <T>("Cannot add component '" + component + "' to " + this + "!");
            }

            var type = component.GetType();

            if (Has(type))
            {
                throw new EntityAlreadyHasComponentException <T>(
                          type,
                          "Cannot add component '" + component + "' to " + this + "!",
                          "You should check if an entity already has the component before adding it or use entity.ReplaceComponent()."
                          );
            }

            _components[type]    = component;
            _componentTypesCache = null;
            _componentsCache     = null;
            _toStringCache       = null;

            OnComponentAdded?.Invoke(this, type, component);

            return(this);
        }
        /// <summary>
        /// Add a component to the pool
        /// </summary>
        /// <param name="component"></param>
        public void AddComponent(ComponentEcs component)
        {
            if (Application.isEditor && !Application.isPlaying)
            {
                return;
            }
            if (!_componentPools.ContainsKey(component.ComponentType))
            {
                Debug.LogWarning($"Attempted to add unknown component type to Component Pools - {component.ComponentType}");
            }
            else
            {
                _componentPools[component.ComponentType].Add(component);
            }

            if (_componentsById.ContainsKey(component.Id))
            {
                if (!Application.isEditor || Application.isPlaying)
                {
                    Debug.LogError("Attempted to add existing GUID");
                }
            }
            else
            {
                _componentsById.Add(component.Id, component);
            }
            CurrentHash = Guid.NewGuid();

            OnComponentAdded?.Invoke(component);
        }
Exemple #5
0
        /// <summary>
        /// Adds a component to the actor.
        /// </summary>
        /// <param name="type">The type of the added component. Must have a parameterless constructor!</param>
        public Component AddComponent(Type type)
        {
            Component component = (Component)Activator.CreateInstance(type);

            components.Add(component);
            component.Initialize(this);

            // Check if the component implements any of these interfaces, if so then add them to their specific list
            if (component is IUpdateable updateable)
            {
                updateables.Add(updateable);
            }
            if (component is IDrawable drawable)
            {
                drawables.Add(drawable);
            }
            if (component is IDisposable disposable)
            {
                disposables.Add(disposable);
            }

            OnComponentAdded?.Invoke(component);
            Stage.OnComponentAdded(component);
            return(component);
        }
Exemple #6
0
        public UTinyObject AddComponent(UTinyType.Reference type)
        {
            var component = NewComponent(type);

            s_ComponentsProperty.Add(this, component);
            OnComponentAdded?.Invoke(this, component);
            return(component);
        }
Exemple #7
0
        public TComponent AddComponent <TComponent>() where TComponent : class, IComponent, new()
        {
            var component = context.GetOrCreateComponent <TComponent>();

            componentsMap[component.GetType()] = component;
            OnComponentAdded?.Invoke(this, component);
            return(component);
        }
Exemple #8
0
        /// <summary>
        /// Add component by type
        /// </summary>
        /// <typeparam name="TComponent">Component type</typeparam>
        /// <returns></returns>
        public TComponent Add <TComponent>() where TComponent : IComponent, new()
        {
            TComponent component = new TComponent();

            Components.Add(component.GetType(), component);

            OnComponentAdded?.Invoke(this, component);
            return(component);
        }
Exemple #9
0
        /// <summary>
        /// Add component by id
        /// </summary>
        /// <param name="componentId"></param>
        public IComponent Add(int componentId)
        {
            IComponent component = (IComponent)Activator.CreateInstance(ComponentLookup.Get(componentId));

            Components.Add(component.GetType(), component);

            OnComponentAdded?.Invoke(this, component);
            return(component);
        }
Exemple #10
0
        public void AddComponent <T>(Action <T> configure) where T : class, IComponent, new()
        {
            var component = _componentFactory.GetComponent <T>();

            configure(component);

            _components[component.GetType()] = component;
            OnComponentAdded?.Invoke(this, component);
        }
Exemple #11
0
        public IEntityComponent EntityComponentCreateOrUpdateWithModel(string entityId, CLASS_ID_COMPONENT classId, object data)
        {
            IDCLEntity entity = GetEntityForUpdate(entityId);

            if (entity == null)
            {
                Debug.LogError($"scene '{sceneData.id}': Can't create entity component if the entity {entityId} doesn't exist!");
                return(null);
            }

            IEntityComponent newComponent = null;

            if (classId == CLASS_ID_COMPONENT.UUID_CALLBACK)
            {
                OnPointerEvent.Model model = JsonUtility.FromJson <OnPointerEvent.Model>(data as string);
                classId = model.GetClassIdFromType();
            }

            if (!entity.components.ContainsKey(classId))
            {
                var factory = Environment.i.world.componentFactory;
                newComponent = factory.CreateComponent((int)classId) as IEntityComponent;

                if (newComponent != null)
                {
                    entity.components.Add(classId, newComponent);
                    OnComponentAdded?.Invoke(newComponent);

                    newComponent.Initialize(this, entity);

                    if (data is string json)
                    {
                        newComponent.UpdateFromJSON(json);
                    }
                    else
                    {
                        newComponent.UpdateFromModel(data as BaseModel);
                    }
                }
            }
            else
            {
                newComponent = EntityComponentUpdate(entity, classId, data as string);
            }

            if (newComponent != null && newComponent is IOutOfSceneBoundariesHandler)
            {
                Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity);
            }

            OnChanged?.Invoke();
            Environment.i.platform.physicsSyncController.MarkDirty();
            Environment.i.platform.cullingController.MarkDirty();
            return(newComponent);
        }
Exemple #12
0
        public ECSEntity AddComponent(ECSComponent component)
        {
            if (!components.Contains(component))
            {
                components.Add(component);

                component.Entity = this;

                component.Init();

                OnComponentAdded?.Invoke(component);
            }
            return(this);
        }
Exemple #13
0
        public T Add <T>() where T : Component
        {
            Assert.IsFalse(Has <T>(), $"{gameObject} already has {typeof(T)}!");

            T component = Activator.CreateInstance <T>();

            component.gameObject = gameObject;
            Game.main.Add(component);

            list.Add(component);
            OnComponentAdded?.Invoke(gameObject, component);

            return(component);
        }
Exemple #14
0
        public TComponent AddComponent <TComponent>(int entity, TComponent component) where TComponent : IEcsComponent
        {
            var componentsOfType = components[component.GetType()];

            if (!componentsOfType.ContainsKey(entity))
            {
                componentsOfType.Add(entity, component);
                Console.WriteLine("Added " + component.GetType() + " to " + entity);
                OnComponentAdded?.Invoke(entity, component);
                return(component);
            }
            else
            {
                throw new ExistingComponentException(entity + " already has a " + component.GetType() + " component");
            }
        }
Exemple #15
0
        /// <summary>
        /// Link the entity and component together
        /// </summary>
        /// <typeparam name="E">type extending Entity</typeparam>
        /// <typeparam name="C">type implementing IComponent</typeparam>
        /// <param name="entity">The entity that the component will be added to</param>
        /// <param name="component">The component being added</param>
        public void AddComponent <E, C>(E entity, C component) where E : Entity where C : IComponent
        {
            if (entity.Parent.Id != Id)
            {
                throw new ArgumentException(nameof(entity));
            }
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }

            if (entityComponentMap.ContainsKey(entity.Id))
            {
                entityComponentMap[entity.Id].Item2.Add(component);
                OnComponentAdded?.Invoke(this, new ComponentAddedEventArgs(entity, component));
            }
        }
Exemple #16
0
 internal void ComponentAdded(Component component)
 {
     OnComponentAdded?.Invoke(component);
 }
Exemple #17
0
 public void AddComponent(IPlayerComponent i_ComponentToAdd)
 {
     m_PlayListComponents.Add(i_ComponentToAdd);
     OnComponentAdded.Invoke(i_ComponentToAdd);
 }