Prepare returns error

This commit is contained in:
Josh Deprez 2021-08-27 14:52:24 +10:00
parent 9553eac378
commit cf34a7785c
9 changed files with 25 additions and 15 deletions

View file

@ -85,8 +85,9 @@ func (a *Actor) MoveY(dy float64, onCollide func()) {
}
}
func (a *Actor) Prepare(g *Game) {
func (a *Actor) Prepare(g *Game) error {
a.collisionDomain = g.Component(a.CollisionDomain)
return nil
}
func sign(m int) int {

View file

@ -111,4 +111,7 @@ func (c *Camera) Update() error { return c.Scene.Update() }
func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} }
// Prepare grabs a copy of game (needed for screen dimensions)
func (c *Camera) Prepare(game *Game) { c.game = game }
func (c *Camera) Prepare(game *Game) error {
c.game = game
return nil
}

View file

@ -104,7 +104,7 @@ func walk(c interface{}, p []interface{}, v func(interface{}, []interface{}) err
// fastidiously calling RegisterComponent/UnregisterComponent).
func (g *Game) LoadAndPrepare(assets fs.FS) error {
// Load all the Loaders
if err := Walk(g.Root, func(c interface{}, _ []interface{}) error {
if err := Walk(g, func(c interface{}, _ []interface{}) error {
l, ok := c.(Loader)
if !ok {
return nil
@ -117,17 +117,17 @@ func (g *Game) LoadAndPrepare(assets fs.FS) error {
// Build the component databases
g.dbmu.Lock()
g.db = make(map[string]Identifier)
if err := Walk(g.Root, g.registerComponent); err != nil {
if err := Walk(g, g.registerComponent); err != nil {
return err
}
g.dbmu.Unlock()
// Prepare all the Preppers
Walk(g.Root, func(c interface{}, _ []interface{}) error {
if p, ok := c.(Prepper); ok {
p.Prepare(g)
}
return Walk(g, func(c interface{}, _ []interface{}) error {
p, ok := c.(Prepper)
if !ok {
return nil
}
return p.Prepare(g)
})
return nil
}

View file

@ -80,7 +80,7 @@ type ParallaxScaler interface {
// database has been populated but before the game is run. The component can
// store the reference to game, if needed, and also query the component database.
type Prepper interface {
Prepare(game *Game)
Prepare(game *Game) error
}
// Scanner components can be scanned. It is called when the game tree is walked

View file

@ -39,7 +39,10 @@ func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
}
// Prepare does an initial Z-order sort.
func (s *Scene) Prepare(game *Game) { s.sortByDrawOrder() }
func (s *Scene) Prepare(game *Game) error {
s.sortByDrawOrder()
return nil
}
// sortByDrawOrder sorts the components by Z position.
// Everything without a Z sorts first. Stable sort is used to avoid Z-fighting

View file

@ -83,7 +83,7 @@ func (r SceneRef) Show() { r.scene.Show() }
func (r SceneRef) Ident() string { return r.scene.Ident() }
// Prepare prepares the scene.
func (r SceneRef) Prepare(g *Game) { r.scene.Prepare(g) }
func (r SceneRef) Prepare(g *Game) error { return r.scene.Prepare(g) }
// Scan returns the components in the scene.
func (r SceneRef) Scan() []interface{} { return r.scene.Scan() }

View file

@ -21,9 +21,10 @@ type Sheet struct {
w int // width as measured in number of cells
}
func (s *Sheet) Prepare(*Game) {
func (s *Sheet) Prepare(*Game) error {
s.w, _ = s.Src.Image().Size()
s.w /= s.CellSize.X
return nil
}
func (s *Sheet) Scan() []interface{} { return []interface{}{&s.Src} }

View file

@ -92,9 +92,10 @@ func (u *WallUnit) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
screen.DrawImage(src, &opts)
}
func (u *WallUnit) Prepare(g *Game) {
func (u *WallUnit) Prepare(g *Game) error {
u.wall = g.Component(u.WallID).(*Wall)
u.wall.regUnit(u)
return nil
}
func (u *WallUnit) Update() error {

View file

@ -188,7 +188,7 @@ func (aw *Awakeman) realUpdate() error {
return nil
}
func (aw *Awakeman) Prepare(game *engine.Game) {
func (aw *Awakeman) Prepare(game *engine.Game) error {
aw.camera = game.Component(aw.CameraID).(*engine.Camera)
aw.toast, _ = game.Component(aw.ToastID).(*engine.DebugToast)
@ -222,6 +222,7 @@ func (aw *Awakeman) Prepare(game *engine.Game) {
{Frame: 8, Duration: 6},
{Frame: 9, Duration: 6},
}}
return nil
}
func (aw *Awakeman) Scan() []interface{} { return []interface{}{&aw.Sprite} }