Class ComponentContainer
A class implementing a flexible, type-based system for adding components to objects. To utilize it, you can either give your object that you want to have components a ComponentContainer field, or if you wish to avoid the extra field to access components, you may either inherit from ComponentContainer, or have your object implement IHasComponentsvia a backing field of type ComponentContainer.
Implements
Inherited Members
Namespace: GoRogue
Assembly: GoRogue.dll
Syntax
public class ComponentContainer : IHasComponents
Remarks
The component system is designed to be as efficient as possible at run-time for accessing components and determining if a ComponentContainer possesses one or more given types of components, and as well remains flexible and type-safe. Components may be of any type, although it is recommended that you not use value-types. The system also remains accurate with respect to types, even when the components added implement interfaces or have an inheritance heirarchy.
For example, suppose we have the following structure:
interface ITickable
{
void Tick();
}
public class PlayerTickable : ITickable
{
public void Tick() => Console.WriteLine("Player ticked!");
}
If we then add a component of type PlayerTickable to an object called obj. obj.GetComponent<PlayerTickable>() will return the instance we added, as will obj.GetComponent<ITickable>(). Similarly, obj.HasComponent(typeof(ITickable)) and obj.HasComponent(typeof(PlayerTickable)) both return true.
Constructors
| Improve this Doc View SourceComponentContainer()
Constructor.
Declaration
public ComponentContainer()
Methods
| Improve this Doc View SourceAddComponent(Object)
Adds the given object as a component. Throws an exception if that specific instance is already in this ComponentContainer.
Declaration
public virtual void AddComponent(object component)
Parameters
Type | Name | Description |
---|---|---|
System.Object | component | Component to add. |
GetComponent<T>()
Gets the first component of type T found, or default(T) if no component of that type has been added to the object.
Declaration
public T GetComponent<T>()
Returns
Type | Description |
---|---|
T | The first component of Type T that was added to the ComponentContainer, or default(T) if no components of the given type have been added. |
Type Parameters
Name | Description |
---|---|
T | Type of component to retrieve. |
GetComponents<T>()
Gets all components of type T that are attached to the given object.
Declaration
public IEnumerable<T> GetComponents<T>()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | All components of Type T that are in the ComponentContainer. |
Type Parameters
Name | Description |
---|---|
T | Type of components to retrieve. |
HasComponent(Type)
Returns whether or not the current ComponentContainer has at least one component of the specified type. Type may be specified by using typeof(MyComponentType).
Declaration
public bool HasComponent(Type componentType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | componentType | The type of component to check for. |
Returns
Type | Description |
---|---|
System.Boolean | True if the ComponentContainer has at least one component of the specified type, false otherwise. |
HasComponent<T>()
Returns whether or not the current ComponentContainer has at least one component of type T.
Declaration
public bool HasComponent<T>()
Returns
Type | Description |
---|---|
System.Boolean | True if the ComponentContainer has at least one component of the specified type, false otherwise. |
Type Parameters
Name | Description |
---|---|
T | Type of component to check for. |
HasComponents(Type[])
Returns whether or not the current ComponentContainer has all the given types of components. Types may be specified by using typeof(MyComponentType)
Declaration
public bool HasComponents(params Type[] componentTypes)
Parameters
Type | Name | Description |
---|---|---|
System.Type[] | componentTypes | One or more component types to check for. |
Returns
Type | Description |
---|---|
System.Boolean | True if the ComponentContainer has at least one component of each specified type, false otherwise. |
RemoveComponent(Object)
Removes the given component. Throws an exception if the component does not exist in the ComponentContainer.
Declaration
public void RemoveComponent(object component)
Parameters
Type | Name | Description |
---|---|---|
System.Object | component | Component to remove. |
RemoveComponents(Object[])
Removes the given component(s). Throws an exception if a component given does not exist in the ComponentContainer.
Declaration
public virtual void RemoveComponents(params object[] components)
Parameters
Type | Name | Description |
---|---|---|
System.Object[] | components | One or more component instances to remove. |