2021-07-30 14:17:40 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-07-30 17:26:23 +10:00
|
|
|
"encoding/gob"
|
2021-09-01 12:10:41 +10:00
|
|
|
"io/fs"
|
|
|
|
"path/filepath"
|
2021-07-30 14:17:40 +10:00
|
|
|
)
|
|
|
|
|
2021-09-01 12:10:41 +10:00
|
|
|
var (
|
2021-09-01 13:28:07 +10:00
|
|
|
_ scener = &Scene{}
|
2021-09-01 12:10:41 +10:00
|
|
|
|
|
|
|
_ interface {
|
|
|
|
Loader
|
2021-09-01 13:28:07 +10:00
|
|
|
Saver
|
|
|
|
scener
|
2021-09-01 12:10:41 +10:00
|
|
|
} = &SceneRef{}
|
|
|
|
)
|
2021-08-18 16:34:51 +10:00
|
|
|
|
2021-09-01 13:28:07 +10:00
|
|
|
type scener interface {
|
|
|
|
Bounder
|
|
|
|
Disabler
|
|
|
|
Hider
|
|
|
|
Identifier
|
|
|
|
Scanner
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:26:23 +10:00
|
|
|
func init() {
|
2021-08-25 15:04:38 +10:00
|
|
|
gob.Register(&Scene{})
|
2021-09-01 12:10:41 +10:00
|
|
|
gob.Register(&SceneRef{})
|
2021-07-30 17:26:23 +10:00
|
|
|
}
|
|
|
|
|
2021-09-01 13:28:07 +10:00
|
|
|
// Scene contains a bunch of components.
|
2021-07-30 14:25:32 +10:00
|
|
|
type Scene struct {
|
2021-08-23 10:34:56 +10:00
|
|
|
ID
|
2021-09-01 10:36:39 +10:00
|
|
|
Bounds // world coordinates
|
2021-07-30 14:17:40 +10:00
|
|
|
Components []interface{}
|
2021-08-23 11:10:46 +10:00
|
|
|
Disabled
|
2021-08-23 10:34:56 +10:00
|
|
|
Hidden
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
|
|
|
|
2021-08-28 18:07:53 +10:00
|
|
|
// Scan returns all immediate subcomponents (including the camera, if not nil).
|
2021-09-01 10:36:39 +10:00
|
|
|
func (s *Scene) Scan() []interface{} { return s.Components }
|
2021-09-01 12:10:41 +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.
|
|
|
|
//
|
|
|
|
// sc := &Scene{
|
|
|
|
// Components: []interface{}{
|
|
|
|
// &SceneRef{Path: "assets/foo.gob.gz"} // inflated at Load time
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
type SceneRef struct {
|
|
|
|
Path string
|
|
|
|
|
2021-09-01 13:28:07 +10:00
|
|
|
*Scene // not gob encoded
|
|
|
|
}
|
|
|
|
|
|
|
|
// GobDecode saves the byte slice as Path.
|
|
|
|
func (r *SceneRef) GobDecode(b []byte) error {
|
|
|
|
r.Path = string(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GobEncode returns Path as a byte slice.
|
|
|
|
func (r *SceneRef) GobEncode() ([]byte, error) {
|
|
|
|
return []byte(r.Path), nil
|
2021-09-01 12:10:41 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the scene from the file.
|
|
|
|
func (r *SceneRef) Load(assets fs.FS) error {
|
|
|
|
sc := new(Scene)
|
|
|
|
if err := LoadGobz(sc, assets, r.Path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-01 13:28:07 +10:00
|
|
|
r.Scene = sc
|
2021-09-01 12:10:41 +10:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save saves the scene to a file in the current directory.
|
2021-09-01 13:28:07 +10:00
|
|
|
func (r *SceneRef) Save() error {
|
|
|
|
return SaveGobz(r.Scene, filepath.Base(r.Path))
|
|
|
|
}
|