mizu
mizu copied to clipboard
Filtering entities with external slice reuse
Problem
We need an alternative to Filter, which will allow you to use your own list (for example, for sorting), but at the same time, it will not do the allocation once again.
Solution
You can make an analogue of append, but for entities.
func (v *view) AppendEntities(entities []Entity) {
for _, en := range v.w.entities {
if en.mask.contains(v.mask) {
entities = append(entities, en)
}
}
}
type System struct {
entities []Entity
balls engine.View
}
func (s *System) Update(w engine.World) {
// Clear balls
s.entities = s.entities[:0]
...
// Add balls
balls.AppendEntities(s.entities)
}