EngoEngine / ecs

A Go-implementation of the Entity-Component-System paradigm
MIT License
298 stars 43 forks source link

World System "auto-register" #54

Open merlindorin opened 2 years ago

merlindorin commented 2 years ago

Hi, thanks for all your work, having a lot of fun here using ecs & engo :)

One of the most boring, mistakable, and kind of dirty stuff is the registering process of a system.

Usually, we go to a code like that:

    rsys := &common.RenderSystem{}
    var r *common.Renderable
    var notr *common.NotRenderable
    w.AddSystemInterface(rsys, r, notr)

I would like to add a method to World in order to simplify this process (before, just an idea, can PR a complete work):

// didn't check the code, just the idea
type SomeSystem() {}
func(*SomeSystem) Add(basic *ecs.BasicEntity, space *common.SpaceComponent, component *BulletComponent) {}
func(*SomeSystem) Remove(basic ecs.BasicEntity) {}
func(s *SomeSystem) AutoRegister(w *ecs.World) {
    var able *SomeSystemAble
    var notAble *NotSomeSystemAble
    w.AddSystemInterface(s, able, notAble)
}

type Registerable interface {
    AutoRegister(w *ecs.World)
}

func (w *ecs.World) AutoRegisterSystem(r Registerable) *ecs.World {
    r.AutoRegister(w)
}

func (w *ecs.World) AutoRegisterSystem(r Registerable) *ecs.World {
    w.append(w.toBeRegistered, r)
}

world.AutoRegisterSystem(&SomeSystem{})

The idea is to delegate the registration process to the system... this can be done in multiple ways like above. Another interesting way can be done by using directly the method "AutoRegister" on the system but I think it's better to define an interface and delegate the registration to World.

This is what I currently do (with a decorator but the idea is here).

Feedback appreciated

Noofbiz commented 2 years ago

Yeah, one thing I don't like about the add by interface method is how bad that looks. The good news is that go 1.18 is adding generics, which will provide a way to do all the add by interface stuff without all the current complexity. I'll probably end up doing something like AddGenericSystem(s System) that will hopefully end up replacing all that with just a single line.