Polosky
Polosky

Reputation: 88

C# multiple classes using generics and this

I'm trying to work on a custom Entity System Framework to develop a game. Essentially what I'm trying to do with this is have a Component, and Component System classes that you can derive further classes from. I really am unsure of how to directly explain what it is that I'm trying to do, so I'm going to copy and paste my code here, and then try to formulate a specific question at the end.

Component

public class Component 
{
    public Entity entity;

    public Component() { }

    public Component(Entity e)
    { e.AddComponent(this); }
}

Component System

public class ComponentSystem<T> where T : Component
{
    protected List<T> components;

    public void AddComponent(T com)
    {
        this.components.Add(com);
    }

    public void RemoveComponent(T com)
    {
        this.components.Remove(com);
    }

    public void Update(double timeDelta = 0.0f)
    {
    }
}

How Main is now

public Main(string[] args)
{
    TagSystem Tags = new TagSystem();
    GroupSystem Groups = new GroupSystem();

    Entity e = new Entity();
    e.AddComponent(new TagComponent("UniqueID"));
    e.AddComponent(new GroupComponent());

    Tags.AddComponent(e.GetComponent<TagComponent>());
    Groups.AddComponent(e.GetComponent<GroupComponent>());
}

How I would like Main to look

public Main(string[] args)
{
    TagSystem Tags = new TagSystem();
    GroupSystem Groups = new GroupSystem();

    Entity e = new Entity();
    e.AddComponent(new TagComponent(Tags, "UniqueID"));
    e.AddComponent(new GroupComponent(Groups));

    // Or this?
    Entity e = new Entity();
    e.AddComponent(new TagComponent<TagSystem>(Tags, "UniqueID"));
    e.AddComponent(new GroupComponent<GroupSystem>(Groups));
}

I guess what I'm trying to do is to have the Component automatically added to the Component System on generation, but I really do not know how to accomplish this. I've tried the following, but neither one works:

public void AddToSystem<T, U>(T sys) where T : ComponentSystem<U> 
{ sys.AddComponent(this); }
public void AddToSystem<T>(T sys) 
{ sys.AddComponent(this); } // fails because T has no method AddComponent

Upvotes: 1

Views: 93

Answers (1)

Steve
Steve

Reputation: 633

A reasonable design may be to turn the ComponentSystem into a Factory.

This way, whenever the Component is queried for an instance of the object, it will keep track of the component instances it hands out. You could use Dependency Injection to initialize the component.

There is some overhead:

  1. You need to register objects that are produced by the factory (so component types won't be coupled to the Component System)
  2. You need to define an interface for the components (or a base class if all components will have the same functionality) to implement so the Component system knows what to do with them
  3. You need a way of telling the component system to remove instances or else the memory taken up by the component system will forever be growing

Just some thoughts

Upvotes: 2

Related Questions