I'm trying to build up a simple game engine in XNA much inspired by Unity. What I'm currently working on is Components that can be attached to gameobjects. I have classes like "PlayerController" and "Collider" that are components and therefor inherits from the Component class.
I'm trying to create methods than can add a new component depending on a Type argument, for example when trying to require a component:
public void RequireComponent(System.Type type)
{
//Create the component
Component comp = new Component();
//Convert the component to the requested type
comp = (type)comp; // This obviously doesn't work
//Add the component to the gameobject
gameObject.components.Add(comp);
}
For example, the rigidbody component needs the gameobject to have a collider, so it requires the collider component:
public override void Initialize(GameObject owner)
{
base.Initialize(owner);
RequireComponent(typeof(Collider));
}
Can this be done or is there a better way?