Scan uses visitor pattern

This commit is contained in:
Josh Deprez 2021-09-22 15:55:38 +10:00
parent fe2558b2c1
commit 6d0838a369
15 changed files with 17 additions and 58 deletions

View file

@ -51,12 +51,12 @@ func (b *Billboard) Prepare(g *Game) error {
return nil
}
// Scan returns a slice containing Src.
//func (b *Billboard) Scan() []interface{} { return []interface{}{&b.Src} }
// Scan visits &b.Src.
func (b *Billboard) Scan(visit func(interface{}) error) error {
return visit(&b.Src)
}
// String returns "Billboard@(b.Pos)".
func (b *Billboard) String() string {
return fmt.Sprintf("Billboard@%v", b.Pos)
}

View file

@ -85,8 +85,7 @@ func (c *Camera) Prepare(game *Game) error {
return nil
}
// Scan returns s.Child.
//func (c *Camera) Scan() []interface{} { return []interface{}{c.Child} }
// Scan visits c.Child.
func (c *Camera) Scan(visit func(interface{}) error) error {
return visit(c.Child)
}

View file

@ -5,11 +5,10 @@ var _ interface {
Scanner
} = &Container{}
// Container contains many components.
// Container contains many components, in order.
type Container []interface{}
// Scan returns c.
//func (c Container) Scan() []interface{} { return c }
// Scan visits every component in the container.
func (c Container) Scan(visit func(interface{}) error) error {
for _, x := range c {
if err := visit(x); err != nil {

View file

@ -114,7 +114,7 @@ func (d *DrawDAG) Prepare(game *Game) error {
return d.Register(d, nil)
}
//func (d *DrawDAG) Scan() []interface{} { return []interface{}{d.Child} }
// Scan visits d.Child.
func (d *DrawDAG) Scan(visit func(interface{}) error) error {
return visit(d.Child)
}

View file

@ -57,7 +57,7 @@ func (d *DrawDFS) draw(component interface{}, screen *ebiten.Image, opts ebiten.
}
}
//func (d *DrawDFS) Scan() []interface{} { return []interface{}{d.Child} }
// Scan visits d.Child.
func (d *DrawDFS) Scan(visit func(interface{}) error) error {
return visit(d.Child)
}

View file

@ -170,8 +170,7 @@ func (g *Game) Query(ancestor interface{}, behaviour reflect.Type) map[interface
return g.byAB[abKey{ancestor, behaviour}]
}
// Scan returns g.Root.
//func (g *Game) Scan() []interface{} { return []interface{}{g.Root} }
// Scan visits g.Root.
func (g *Game) Scan(visit func(interface{}) error) error {
return visit(g.Root)
}

View file

@ -38,8 +38,7 @@ func (p *Parallax) Prepare(game *Game) error {
return nil
}
// Scan returns the child component.
//func (p *Parallax) Scan() []interface{} { return []interface{}{p.Child} }
// Scan visits p.Child.
func (p *Parallax) Scan(visit func(interface{}) error) error {
return visit(p.Child)
}

View file

@ -112,15 +112,7 @@ func (m *PrismMap) Prepare(g *Game) error {
return nil
}
// Scan returns the Sheet and all the Prisms.
/*func (m *PrismMap) Scan() []interface{} {
c := make([]interface{}, 1, len(m.Map)+1)
c[0] = &m.Sheet
for _, prism := range m.Map {
c = append(c, prism)
}
return c
}*/
// Scan visits &m.Sheet and all Prisms.
func (m *PrismMap) Scan(visit func(interface{}) error) error {
if err := visit(&m.Sheet); err != nil {
return err

View file

@ -38,7 +38,7 @@ type Scene struct {
Hides
}
//func (s *Scene) Scan() []interface{} { return []interface{}{s.Child} }
// Scan visits s.Child.
func (s *Scene) Scan(visit func(interface{}) error) error {
return visit(s.Child)
}

View file

@ -46,8 +46,7 @@ func (s *Sheet) Prepare(*Game) error {
return nil
}
// Scan returns the Src.
//func (s *Sheet) Scan() []interface{} { return []interface{}{&s.Src} }
// Scan visits &s.Src.
func (s *Sheet) Scan(visit func(interface{}) error) error {
return visit(&s.Src)
}

View file

@ -40,13 +40,7 @@ func (s *Sprite) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
screen.DrawImage(s.Sheet.SubImage(s.anim.Cell()), opts)
}
// Scan returns the Actor and the Sheet.
/*func (s *Sprite) Scan() []interface{} {
return []interface{}{
&s.Actor,
&s.Sheet,
}
}*/
// Scan visits &s.Actor and &s.Sheet.
func (s *Sprite) Scan(visit func(interface{}) error) error {
if err := visit(&s.Actor); err != nil {
return err

View file

@ -99,15 +99,7 @@ func (t *Tilemap) Load(fs.FS) error {
return nil
}
// Scan returns a slice containing Src and all non-nil tiles.
/*func (t *Tilemap) Scan() []interface{} {
c := make([]interface{}, 1, len(t.Map)+1)
c[0] = &t.Sheet
for _, tile := range t.Map {
c = append(c, tile)
}
return c
}*/
// Scan visits &t.Sheet and all tiles.
func (t *Tilemap) Scan(visit func(interface{}) error) error {
if err := visit(&t.Sheet); err != nil {
return err
@ -162,8 +154,7 @@ type AnimatedTile struct {
func (a *AnimatedTile) Cell() int { return a.anim.Cell() }
// Scan returns a.anim.
//func (a *AnimatedTile) Scan() []interface{} { return []interface{}{a.anim} }
// Scan visits a.anim.
func (a *AnimatedTile) Scan(visit func(interface{}) error) error {
return visit(a.anim)
}

View file

@ -60,17 +60,7 @@ func (w *Wall) CollidesWith(b geom.Box) bool {
return false
}
// Scan returns the Sheet and all WallUnits.
/*
func (w *Wall) Scan() []interface{} {
c := make([]interface{}, 1, len(w.Units)+1)
c[0] = &w.Sheet
for _, unit := range w.Units {
c = append(c, unit)
}
return c
}
*/
// Scan visits &w.Sheet and all WallUnits.
func (w *Wall) Scan(visit func(interface{}) error) error {
if err := visit(&w.Sheet); err != nil {
return err
@ -115,8 +105,7 @@ func (u *WallUnit) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
screen.DrawImage(u.wall.Sheet.SubImage(u.Tile.Cell()), opts)
}
// Scan returns the Tile.
//func (u *WallUnit) Scan() []interface{} { return []interface{}{u.Tile} }
// Scan visits u.Tile.
func (u *WallUnit) Scan(visit func(interface{}) error) error {
return visit(u.Tile)
}

View file

@ -270,7 +270,6 @@ func (aw *Awakeman) Prepare(game *engine.Game) error {
return nil
}
//func (aw *Awakeman) Scan() []interface{} { return []interface{}{&aw.Sprite} }
func (aw *Awakeman) Scan(visit func(interface{}) error) error {
return visit(&aw.Sprite)
}

View file

@ -55,7 +55,6 @@ func NewBubble(pos geom.Int3) *Bubble {
}
}
//func (b *Bubble) Scan() []interface{} { return []interface{}{&b.Sprite} }
func (b *Bubble) Scan(visit func(interface{}) error) error {
return visit(&b.Sprite)
}