mizu
mizu copied to clipboard
The scene must be initialized in a certain order
Problem
At the moment, scene initialization should always go in the order: components > systems > entities. Otherwise, they will not find each other.
type Game struct{}
func (g *Game) Setup(w engine.World) {
w.AddComponents()
w.AddSystems()
w.AddEntities()
}
Solution
It is necessary to correct this behavior by replacing the functions of adding all parts with a builder that will automatically initialize at the very end of the scene creation.
func (w *world) AddComponents(components ...interface{}) {
w.futureComponents = append(w.futureComponents, components...)
}
func (w *world) AddSystems(systems ...interface{}) {
w.futureSystems = append(w.futureSystems, components...)
}
func (w *world) AddEntities(entities ...interface{}) {
w.futureEntities = append(w.futureEntities, components...)
}
func (w *world) ChangeScene(next Scene) {
next.Setup(w)
w.buildScene()
}