ichigo/engine/scene.go

86 lines
1.5 KiB
Go
Raw Normal View History

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-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 {
2021-09-10 17:18:20 +10:00
BoundingRecter
2021-09-01 13:28:07 +10:00
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-22 14:37:00 +10:00
// Scene is a component for adding an identity, bounds, and other properties.
2021-07-30 14:25:32 +10:00
type Scene struct {
2021-08-23 10:34:56 +10:00
ID
2021-09-21 14:42:18 +10:00
Bounds // world coordinates
2021-09-22 14:37:00 +10:00
Child interface{}
2021-09-13 16:50:35 +10:00
Disables
Hides
}
2021-09-22 15:55:38 +10:00
// Scan visits s.Child.
2021-09-22 15:48:02 +10:00
func (s *Scene) Scan(visit func(interface{}) error) error {
return visit(s.Child)
}
2021-09-21 16:53:04 +10:00
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))
}