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(); }
/// <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(); } }
/// <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(); } }