minor refactoring

This commit is contained in:
Josh Deprez 2021-12-03 14:13:56 +11:00
parent 6621a3c6e5
commit 00fd633838
4 changed files with 308 additions and 293 deletions

View file

@ -455,6 +455,16 @@ func concatOpts(a, b ebiten.DrawImageOptions) ebiten.DrawImageOptions {
// recursive operations, return Skip for components that should be skipped.
type VisitFunc func(interface{}) error
// Many calls a VisitFunc for multiple args, and returns on first non-nil error.
func (v VisitFunc) Many(x ...interface{}) error {
for _, c := range x {
if err := v(c); err != nil {
return err
}
}
return nil
}
// Skip is an "error" value that can be returned from visitor callbacks. It
// tells recursive methods of Game to skip processing the current item and its
// descendants, but will otherwise continue processing.

View file

@ -58,10 +58,7 @@ func (s *Sprite) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
// Scan visits &s.Actor and &s.Sheet.
func (s *Sprite) Scan(visit VisitFunc) error {
if err := visit(&s.Actor); err != nil {
return err
}
return visit(&s.Sheet)
return visit.Many(&s.Actor, &s.Sheet)
}
// Anim returns the current Anim.

View file

@ -119,7 +119,7 @@ func (aw *Awakeman) realUpdate() error {
ε = 0.2
restitution = -0.3
gravity = 0.25
airResistance = -0.005 // ⇒ terminal velocity = 10
airResistance = -0.005
jumpVelocity = -3.3
sqrt2 = 1.414213562373095
runVelocity = sqrt2

View file

@ -45,7 +45,16 @@ func Level1() *engine.Scene {
&engine.DrawDAG{
ChunkSize: 16,
Child: engine.MakeContainer(
&engine.PrismMap{
level1PrismMap(),
level1Awakeman(),
), // Container
}, // DrawDAG
), // Container
} // Scene
}
func level1PrismMap() *engine.PrismMap {
return &engine.PrismMap{
ID: "hexagons",
PosToWorld: geom.IntMatrix3x4{
// For each tile in the X direction, go right by 24 and
@ -271,8 +280,11 @@ func Level1() *engine.Scene {
geom.Pt3(0, 0, 13): {},
}, // Map
}, // PrismMap
&Awakeman{
} // PrismMap
}
func level1Awakeman() *Awakeman {
return &Awakeman{
CameraID: "game_camera",
ToastID: "toast",
Sprite: engine.Sprite{
@ -332,9 +344,5 @@ func Level1() *engine.Scene {
Src: engine.ImageRef{Path: "assets/aw.png"},
}, // Sheet
}, // Sprite
}, // Awakeman
), // Container
}, // DrawDAG
), // Container
} // Scene
} // Awakeman
}