move Bounds from Camera to Scene; add SceneRef
This commit is contained in:
parent
eed4909fb7
commit
91bf4939e7
4 changed files with 48 additions and 17 deletions
|
@ -1,6 +1,8 @@
|
||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/gzip"
|
||||||
|
"encoding/gob"
|
||||||
"image"
|
"image"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
|
@ -71,3 +73,31 @@ func (r *ImageRef) Image() *ebiten.Image {
|
||||||
imageCache[r.Path] = r.image
|
imageCache[r.Path] = r.image
|
||||||
return r.image
|
return r.image
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SceneRef loads a gzipped, gob-encoded Scene from the asset FS.
|
||||||
|
type SceneRef struct {
|
||||||
|
Path string
|
||||||
|
|
||||||
|
scene *Scene
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SceneRef) Scene() *Scene {
|
||||||
|
if r.scene != nil {
|
||||||
|
return r.scene
|
||||||
|
}
|
||||||
|
f, err := AssetFS.Open(r.Path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Couldn't open asset: %v", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
gz, err := gzip.NewReader(f)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Couldn't gunzip asset: %v", err)
|
||||||
|
}
|
||||||
|
r.scene = new(Scene)
|
||||||
|
if err := gob.NewDecoder(gz).Decode(r.scene); err != nil {
|
||||||
|
log.Fatalf("Couldn't decode asset: %v", err)
|
||||||
|
}
|
||||||
|
return r.scene
|
||||||
|
}
|
||||||
|
|
|
@ -28,8 +28,7 @@ type Camera struct {
|
||||||
Scene *Scene
|
Scene *Scene
|
||||||
|
|
||||||
// Camera controls
|
// Camera controls
|
||||||
Bounds image.Rectangle // world coordinates
|
Centre image.Point // world coordinates
|
||||||
Centre image.Point // world coordinates
|
|
||||||
Filter ebiten.Filter
|
Filter ebiten.Filter
|
||||||
Zoom float64 // unitless
|
Zoom float64 // unitless
|
||||||
|
|
||||||
|
@ -45,7 +44,7 @@ func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
||||||
// The lower bound on zoom is the larger of
|
// The lower bound on zoom is the larger of
|
||||||
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
|
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
|
||||||
zoom := c.Zoom
|
zoom := c.Zoom
|
||||||
sz := c.Bounds.Size()
|
sz := c.Scene.Bounds.Size()
|
||||||
if z := float64(c.game.ScreenWidth) / float64(sz.X); zoom < z {
|
if z := float64(c.game.ScreenWidth) / float64(sz.X); zoom < z {
|
||||||
zoom = z
|
zoom = z
|
||||||
}
|
}
|
||||||
|
@ -58,17 +57,17 @@ func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
||||||
// Camera frame currently Rectangle{ centre ± (screen/(2*zoom)) }.
|
// Camera frame currently Rectangle{ centre ± (screen/(2*zoom)) }.
|
||||||
sw2, sh2 := float64(c.game.ScreenWidth/2), float64(c.game.ScreenHeight/2)
|
sw2, sh2 := float64(c.game.ScreenWidth/2), float64(c.game.ScreenHeight/2)
|
||||||
swz, shz := int(sw2/zoom), int(sh2/zoom)
|
swz, shz := int(sw2/zoom), int(sh2/zoom)
|
||||||
if centre.X-swz < c.Bounds.Min.X {
|
if centre.X-swz < c.Scene.Bounds.Min.X {
|
||||||
centre.X = c.Bounds.Min.X + swz
|
centre.X = c.Scene.Bounds.Min.X + swz
|
||||||
}
|
}
|
||||||
if centre.Y-shz < c.Bounds.Min.Y {
|
if centre.Y-shz < c.Scene.Bounds.Min.Y {
|
||||||
centre.Y = c.Bounds.Min.Y + shz
|
centre.Y = c.Scene.Bounds.Min.Y + shz
|
||||||
}
|
}
|
||||||
if centre.X+swz > c.Bounds.Max.X {
|
if centre.X+swz > c.Scene.Bounds.Max.X {
|
||||||
centre.X = c.Bounds.Max.X - swz
|
centre.X = c.Scene.Bounds.Max.X - swz
|
||||||
}
|
}
|
||||||
if centre.Y+shz > c.Bounds.Max.Y {
|
if centre.Y+shz > c.Scene.Bounds.Max.Y {
|
||||||
centre.Y = c.Bounds.Max.Y - shz
|
centre.Y = c.Scene.Bounds.Max.Y - shz
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply other options
|
// Apply other options
|
||||||
|
|
|
@ -2,6 +2,7 @@ package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"image"
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
@ -24,11 +25,12 @@ func init() {
|
||||||
|
|
||||||
// Scene manages drawing and updating a bunch of components.
|
// Scene manages drawing and updating a bunch of components.
|
||||||
type Scene struct {
|
type Scene struct {
|
||||||
|
Bounds image.Rectangle // world coordinates
|
||||||
Components []interface{}
|
Components []interface{}
|
||||||
Disabled bool
|
Disabled bool
|
||||||
ZOrder
|
Hidden bool
|
||||||
Hidden bool
|
|
||||||
ID
|
ID
|
||||||
|
ZOrder
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw draws all components in order.
|
// Draw draws all components in order.
|
||||||
|
|
8
main.go
8
main.go
|
@ -48,7 +48,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
level1 := &engine.Scene{
|
level1 := &engine.Scene{
|
||||||
ID: "level_1",
|
ID: "level_1",
|
||||||
|
Bounds: image.Rect(-32, -32, 320+32, 240+32),
|
||||||
Components: []interface{}{
|
Components: []interface{}{
|
||||||
&engine.Fill{
|
&engine.Fill{
|
||||||
Color: color.Gray{100},
|
Color: color.Gray{100},
|
||||||
|
@ -108,9 +109,8 @@ func main() {
|
||||||
KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD},
|
KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD},
|
||||||
},
|
},
|
||||||
&engine.Camera{
|
&engine.Camera{
|
||||||
Bounds: image.Rect(-32, -32, 320+32, 240+32),
|
ID: "game_camera",
|
||||||
ID: "game_camera",
|
Scene: level1,
|
||||||
Scene: level1,
|
|
||||||
},
|
},
|
||||||
engine.PerfDisplay{},
|
engine.PerfDisplay{},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue