2021-08-23 11:00:51 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"image"
|
|
|
|
"io/fs"
|
2021-08-25 16:46:30 +10:00
|
|
|
"path/filepath"
|
2021-08-23 11:00:51 +10:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
2021-08-23 11:02:24 +10:00
|
|
|
// Ensure SceneRef satisfies interfaces.
|
2021-08-27 11:49:11 +10:00
|
|
|
var _ interface {
|
|
|
|
Loader
|
|
|
|
Scener
|
|
|
|
} = &SceneRef{}
|
2021-08-23 11:02:24 +10:00
|
|
|
|
2021-08-23 18:08:14 +10:00
|
|
|
func init() {
|
2021-08-30 15:53:39 +10:00
|
|
|
gob.Register(&SceneRef{})
|
2021-08-23 18:08:14 +10:00
|
|
|
}
|
|
|
|
|
2021-08-23 11:00:51 +10:00
|
|
|
// SceneRef loads a gzipped, gob-encoded Scene from the asset FS.
|
|
|
|
// After Load, Scene is usable.
|
|
|
|
// This is mostly useful for scenes that refer to other scenes, e.g.
|
|
|
|
//
|
2021-08-26 13:08:51 +10:00
|
|
|
// sc := &Scene{
|
2021-08-23 11:00:51 +10:00
|
|
|
// Components: []interface{}{
|
2021-08-25 15:04:38 +10:00
|
|
|
// &SceneRef{Path: "assets/foo.gob.gz"} // inflated at Load time
|
2021-08-23 11:00:51 +10:00
|
|
|
// },
|
|
|
|
// }
|
|
|
|
type SceneRef struct {
|
|
|
|
Path string
|
|
|
|
|
|
|
|
scene *Scene // not exported for gob reasons
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the scene from the file.
|
|
|
|
func (r *SceneRef) Load(assets fs.FS) error {
|
|
|
|
sc := new(Scene)
|
2021-08-26 11:31:39 +10:00
|
|
|
if err := LoadGobz(sc, assets, r.Path); err != nil {
|
2021-08-23 11:00:51 +10:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.scene = sc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:46:30 +10:00
|
|
|
// Save saves the scene to a file in the current directory.
|
2021-08-26 11:31:39 +10:00
|
|
|
func (r *SceneRef) Save() error { return SaveGobz(r.scene, filepath.Base(r.Path)) }
|
2021-08-25 16:46:30 +10:00
|
|
|
|
2021-08-23 11:00:51 +10:00
|
|
|
// The rest of the methods forward to r.scene, as such they will
|
|
|
|
// panic if the scene isn't loaded.
|
|
|
|
|
|
|
|
// BoundingRect returns the Bounds from the scene.
|
|
|
|
func (r SceneRef) BoundingRect() image.Rectangle { return r.scene.BoundingRect() }
|
|
|
|
|
2021-08-23 11:10:46 +10:00
|
|
|
// IsDisabled returns the value of IsDisabled from the scene.
|
|
|
|
func (r SceneRef) IsDisabled() bool { return r.scene.IsDisabled() }
|
|
|
|
|
|
|
|
// Disable calls Disable on the scene.
|
|
|
|
func (r SceneRef) Disable() { r.scene.Disable() }
|
|
|
|
|
|
|
|
// Enable calls Enable on the scene.
|
|
|
|
func (r SceneRef) Enable() { r.scene.Enable() }
|
|
|
|
|
2021-08-23 11:00:51 +10:00
|
|
|
// IsHidden returns the value of IsHidden from the scene.
|
|
|
|
func (r SceneRef) IsHidden() bool { return r.scene.IsHidden() }
|
|
|
|
|
|
|
|
// Hide calls Hide on the scene.
|
|
|
|
func (r SceneRef) Hide() { r.scene.Hide() }
|
|
|
|
|
|
|
|
// Show calls Show on the scene.
|
|
|
|
func (r SceneRef) Show() { r.scene.Show() }
|
|
|
|
|
|
|
|
// Ident returns the value of Ident from the scene.
|
|
|
|
func (r SceneRef) Ident() string { return r.scene.Ident() }
|
|
|
|
|
|
|
|
// Scan returns the components in the scene.
|
|
|
|
func (r SceneRef) Scan() []interface{} { return r.scene.Scan() }
|
|
|
|
|
2021-08-31 22:05:21 +10:00
|
|
|
// Transform returns the value of Transform from the scene.
|
|
|
|
func (r SceneRef) Transform() ebiten.DrawImageOptions { return r.scene.Transform() }
|