C# multiple classes using generics and this -
i'm trying work on custom entity system framework develop game. i'm trying have component, , component system classes can derive further classes from. unsure of how directly explain i'm trying do, i'm going copy , paste code here, , try formulate specific question @ end.
component
public class component { public entity entity; public component() { } public component(entity e) { e.addcomponent(this); } } component system
public class componentsystem<t> 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 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 main 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 i'm trying have component automatically added component system on generation, not know how accomplish this. i've tried following, neither 1 works:
public void addtosystem<t, u>(t sys) t : componentsystem<u> { sys.addcomponent(this); } public void addtosystem<t>(t sys) { sys.addcomponent(this); } // fails because t has no method addcomponent
a reasonable design may turn componentsystem factory.
this way, whenever component queried instance of object, keep track of component instances hands out. use dependency injection initialize component.
there overhead:
- you need register objects produced factory (so component types won't coupled component system)
- you need define interface components (or base class if components have same functionality) implement component system knows them
- you need way of telling component system remove instances or else memory taken component system forever growing
just thoughts
Comments
Post a Comment