how to load from a file
This commit is contained in:
parent
b38aadee74
commit
d10d57a3dc
17 changed files with 141 additions and 99 deletions
|
@ -12,7 +12,7 @@ var _ Prepper = &Actor{}
|
||||||
var errCollision = errors.New("collision detected")
|
var errCollision = errors.New("collision detected")
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(Actor{})
|
gob.Register(&Actor{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Thorson-style movement:
|
// Thorson-style movement:
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
package engine
|
package engine
|
||||||
|
|
||||||
|
import "encoding/gob"
|
||||||
|
|
||||||
// Ensure Anim satisfies Animer.
|
// Ensure Anim satisfies Animer.
|
||||||
var _ Animer = &Anim{}
|
var _ Animer = &Anim{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
gob.Register(&Anim{})
|
||||||
|
}
|
||||||
|
|
||||||
// AnimFrame describes a frame in an animation.
|
// AnimFrame describes a frame in an animation.
|
||||||
type AnimFrame struct {
|
type AnimFrame struct {
|
||||||
Frame int // show this frame
|
Frame int // show this frame
|
||||||
|
|
|
@ -13,7 +13,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(AnimRef{})
|
gob.Register(&AnimRef{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnimRef manages an Anim using a premade AnimDef from the cache.
|
// AnimRef manages an Anim using a premade AnimDef from the cache.
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"image"
|
"image"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
@ -38,6 +39,28 @@ func loadGobz(dst interface{}, assets fs.FS, path string) error {
|
||||||
return gob.NewDecoder(gz).Decode(dst)
|
return gob.NewDecoder(gz).Decode(dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveGobz takes an object, gob-encodes it, gzips it, and writes to disk.
|
||||||
|
func SaveGobz(src interface{}, name string) error {
|
||||||
|
f, err := os.CreateTemp(".", name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
gz := gzip.NewWriter(f)
|
||||||
|
if err := gob.NewEncoder(gz).Encode(src); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := gz.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.Rename(f.Name(), name)
|
||||||
|
}
|
||||||
|
|
||||||
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
||||||
// It is your responsibility to import _ "image/..." for whatever
|
// It is your responsibility to import _ "image/..." for whatever
|
||||||
// format the files are in, and to load it (either return it as a
|
// format the files are in, and to load it (either return it as a
|
||||||
|
|
|
@ -17,7 +17,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(Camera{})
|
gob.Register(&Camera{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Camera models a camera that is viewing a scene.
|
// Camera models a camera that is viewing a scene.
|
||||||
|
|
|
@ -24,8 +24,8 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(GobDumper{})
|
gob.Register(&GobDumper{})
|
||||||
gob.Register(PerfDisplay{})
|
gob.Register(&PerfDisplay{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DebugToast debugprints a string for a while, then disappears.
|
// DebugToast debugprints a string for a while, then disappears.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/gob"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
@ -9,6 +10,12 @@ import (
|
||||||
// Ensure Fill satisfies Drawer.
|
// Ensure Fill satisfies Drawer.
|
||||||
var _ Drawer = &Fill{}
|
var _ Drawer = &Fill{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
gob.Register(Fill{})
|
||||||
|
gob.Register(color.Gray{})
|
||||||
|
gob.Register(color.RGBA{})
|
||||||
|
}
|
||||||
|
|
||||||
// Fill fills the screen with a colour.
|
// Fill fills the screen with a colour.
|
||||||
type Fill struct {
|
type Fill struct {
|
||||||
ID
|
ID
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(Game{})
|
gob.Register(&Game{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Game implements the ebiten methods using a collection of components.
|
// Game implements the ebiten methods using a collection of components.
|
||||||
|
|
|
@ -17,7 +17,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(Image{})
|
gob.Register(&Image{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Image draws an image at a position.
|
// Image draws an image at a position.
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
var _ Scener = &Scene{}
|
var _ Scener = &Scene{}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(Scene{})
|
gob.Register(&Scene{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scene manages drawing and updating a bunch of components.
|
// Scene manages drawing and updating a bunch of components.
|
||||||
|
|
|
@ -24,7 +24,7 @@ func init() {
|
||||||
//
|
//
|
||||||
// var sc = &Scene{
|
// var sc = &Scene{
|
||||||
// Components: []interface{}{
|
// Components: []interface{}{
|
||||||
// SceneRef{Path: "assets/foo.gob.gz"} // inflated at Load time
|
// &SceneRef{Path: "assets/foo.gob.gz"} // inflated at Load time
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
type SceneRef struct {
|
type SceneRef struct {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
var _ Collider = SolidRect{}
|
var _ Collider = SolidRect{}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(SolidRect{})
|
gob.Register(&SolidRect{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type SolidRect struct {
|
type SolidRect struct {
|
||||||
|
|
|
@ -17,7 +17,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(Sprite{})
|
gob.Register(&Sprite{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sprite combines an Actor with the ability to Draw from a single spritesheet.
|
// Sprite combines an Actor with the ability to Draw from a single spritesheet.
|
||||||
|
|
|
@ -19,9 +19,9 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(AnimatedTile{})
|
gob.Register(&AnimatedTile{})
|
||||||
gob.Register(StaticTile(0))
|
gob.Register(StaticTile(0))
|
||||||
gob.Register(Tilemap{})
|
gob.Register(&Tilemap{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tilemap renders a grid of tiles.
|
// Tilemap renders a grid of tiles.
|
||||||
|
|
BIN
game/assets/level1.gobz
Normal file
BIN
game/assets/level1.gobz
Normal file
Binary file not shown.
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(Awakeman{})
|
gob.Register(&Awakeman{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type Awakeman struct {
|
type Awakeman struct {
|
||||||
|
|
8
main.go
8
main.go
|
@ -16,6 +16,7 @@ func main() {
|
||||||
ebiten.SetWindowSize(640, 480)
|
ebiten.SetWindowSize(640, 480)
|
||||||
ebiten.SetWindowTitle("TODO")
|
ebiten.SetWindowTitle("TODO")
|
||||||
|
|
||||||
|
if false {
|
||||||
redTileAnim := &engine.Anim{Frames: []engine.AnimFrame{
|
redTileAnim := &engine.Anim{Frames: []engine.AnimFrame{
|
||||||
{Frame: 3, Duration: 12},
|
{Frame: 3, Duration: 12},
|
||||||
{Frame: 4, Duration: 12},
|
{Frame: 4, Duration: 12},
|
||||||
|
@ -107,6 +108,11 @@ func main() {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := engine.SaveGobz(level1, "level1.gobz"); err != nil {
|
||||||
|
log.Fatalf("Couldn't save level1.gobz: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
g := &engine.Game{
|
g := &engine.Game{
|
||||||
ScreenHeight: 240,
|
ScreenHeight: 240,
|
||||||
ScreenWidth: 320,
|
ScreenWidth: 320,
|
||||||
|
@ -118,7 +124,7 @@ func main() {
|
||||||
},
|
},
|
||||||
&engine.Camera{
|
&engine.Camera{
|
||||||
ID: "game_camera",
|
ID: "game_camera",
|
||||||
Scene: level1,
|
Scene: &engine.SceneRef{Path: "assets/level1.gobz"},
|
||||||
},
|
},
|
||||||
&engine.DebugToast{
|
&engine.DebugToast{
|
||||||
ID: "toast",
|
ID: "toast",
|
||||||
|
|
Loading…
Reference in a new issue