public void AddComponent(IEntityComponent myComponent)
        {
            if (myComponent == null)
            {
                throw new ArgumentNullException("Componenet is null");
            }

            if (componentList.Contains(myComponent))
            {
                return;
                //take this code out if you want to have two of each component
            }

            componentList.Add(myComponent);
            ComponentsDictionary.Add(myComponent.name, myComponent);

            IEntityUpdateable updateable = myComponent as IEntityUpdateable;
            IEntityDrawable drawable = myComponent as IEntityDrawable;

            if (updateable != null)
            {
                updateableComponentList.Add(updateable);
            }
            if (drawable != null)
            {
                drawableComponentList.Add(drawable);
            }

            myComponent.Initialize();
            myComponent.Start();
        }
Beispiel #2
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);
        }
        internal void RegisterComponent(IEntityComponent component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }
            if (component.Entity == null)
            {
                throw new EntityComponentException("Malformed IEntityComponent, not attached to an Entity.");
            }
            if (component.EntityManager != this)
            {
                throw new ArgumentException("Can only register component with the EntityManagerComponent it's entity is managed by.", "component");
            }

            if (_updateThread != null && System.Threading.Thread.CurrentThread == _updateThread)
            {
                //occurred during the main update thread while this Update method is running
                switch (_phase)
                {
                case UpdatePhase.StartingPhase:
                    //we initialize here, this is happening because another componented add this component during its 'Start' routine, and it will expect the component to be initialized right away.
                    component.Initialize();
                    _startPool.Enqueue(component);
                    break;

                case UpdatePhase.None:
                case UpdatePhase.InitializingPhase:
                case UpdatePhase.UpdatingPhase:
                case UpdatePhase.CleanUp:
                    lock (_lock)
                    {
                        _componentInitializeCache.Add(component);
                    }
                    break;
                }
            }
            else
            {
                //happened outside of the main update loop, just lock and add if entity has already been initialized
                lock (_lock)
                {
                    _componentInitializeCache.Add(component);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Adds a component to this entity.  Only allows one component of each type
        /// to be in a single entity.
        /// </summary>
        /// <param name="aComponent">The component to add</param>
        public void AddComponent(IEntityComponent aComponent)
        {
            if (aComponent == null)
            {
                throw new ArgumentNullException("component is null");
            }

            //if you would like more than one of any type of component into this entity,
            //remove this if block.
            if (_components.Contains(aComponent))
            {
                return;
            }

            //add to master and lookup list
            _components.Add(aComponent);
            Components.Add(aComponent.Name, aComponent);

            IEntityUpdateable updateable = aComponent as IEntityUpdateable;
            IEntityDrawable drawable = aComponent as IEntityDrawable;

            //if the component can be updated, add it to that list
            if (updateable != null)
            {
                _updateableComponents.Add(updateable);
                updateable.UpdateOrderChanged += OnComponentUpdateOrderChanged;
                OnComponentUpdateOrderChanged(this, EventArgs.Empty);
            }

            //if the component can be draw, add it to that list
            if (drawable != null)
            {
                _drawableComponents.Add(drawable);
                drawable.DrawOrderChanged += OnComponentDrawOrderChanged;
                OnComponentDrawOrderChanged(this, EventArgs.Empty);
            }

            //if the entity has already initialized, call this item's initialize and start methods
            if (_isInitialized)
            {
                aComponent.Initialize();

                aComponent.Start();
            }
        }
Beispiel #5
0
        /// <summary>
        /// Attaches a component to this entity.
        /// </summary>
        /// <param name="component">The component to attach to the entity.</param>
        public void AttachComponent(IEntityComponent component)
        {
            // Don't allow null parameters.
            if (component == null)
            {
                throw new ArgumentNullException("Component is null.");
            }

            // Don't allow multiples of the same type of component
            String id = component.ID;
            for (int i = 0; i < components.Count; i++)
            {
                if (components[i].ID.Equals(id))
                {
                    //throw new ArgumentException("Component already exists in entity.");
                }
            }

            // Add the component
            components.Add(component);

            if (component is IEntityUpdateable)
            {
                updateableComponents.Add((IEntityUpdateable)component);
            }

            if (component is IEntityDrawable)
            {
                drawableComponents.Add((IEntityDrawable)component);
            }

            // Initialize the component if this entity has already been initialized.
            if (isInitialized)
            {
                component.Initialize();
                component.Start();
            }
        }