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
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"encoding/gob"
|
||||
"image"
|
||||
"io/fs"
|
||||
"log"
|
||||
|
@ -71,3 +73,31 @@ func (r *ImageRef) Image() *ebiten.Image {
|
|||
imageCache[r.Path] = 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
|
||||
|
||||
// Camera controls
|
||||
Bounds image.Rectangle // world coordinates
|
||||
Centre image.Point // world coordinates
|
||||
Centre image.Point // world coordinates
|
||||
Filter ebiten.Filter
|
||||
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
|
||||
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
|
||||
zoom := c.Zoom
|
||||
sz := c.Bounds.Size()
|
||||
sz := c.Scene.Bounds.Size()
|
||||
if z := float64(c.game.ScreenWidth) / float64(sz.X); 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)) }.
|
||||
sw2, sh2 := float64(c.game.ScreenWidth/2), float64(c.game.ScreenHeight/2)
|
||||
swz, shz := int(sw2/zoom), int(sh2/zoom)
|
||||
if centre.X-swz < c.Bounds.Min.X {
|
||||
centre.X = c.Bounds.Min.X + swz
|
||||
if centre.X-swz < c.Scene.Bounds.Min.X {
|
||||
centre.X = c.Scene.Bounds.Min.X + swz
|
||||
}
|
||||
if centre.Y-shz < c.Bounds.Min.Y {
|
||||
centre.Y = c.Bounds.Min.Y + shz
|
||||
if centre.Y-shz < c.Scene.Bounds.Min.Y {
|
||||
centre.Y = c.Scene.Bounds.Min.Y + shz
|
||||
}
|
||||
if centre.X+swz > c.Bounds.Max.X {
|
||||
centre.X = c.Bounds.Max.X - swz
|
||||
if centre.X+swz > c.Scene.Bounds.Max.X {
|
||||
centre.X = c.Scene.Bounds.Max.X - swz
|
||||
}
|
||||
if centre.Y+shz > c.Bounds.Max.Y {
|
||||
centre.Y = c.Bounds.Max.Y - shz
|
||||
if centre.Y+shz > c.Scene.Bounds.Max.Y {
|
||||
centre.Y = c.Scene.Bounds.Max.Y - shz
|
||||
}
|
||||
|
||||
// Apply other options
|
||||
|
|
|
@ -2,6 +2,7 @@ package engine
|
|||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"image"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
|
@ -24,11 +25,12 @@ func init() {
|
|||
|
||||
// Scene manages drawing and updating a bunch of components.
|
||||
type Scene struct {
|
||||
Bounds image.Rectangle // world coordinates
|
||||
Components []interface{}
|
||||
Disabled bool
|
||||
ZOrder
|
||||
Hidden bool
|
||||
Hidden bool
|
||||
ID
|
||||
ZOrder
|
||||
}
|
||||
|
||||
// Draw draws all components in order.
|
||||
|
|
8
main.go
8
main.go
|
@ -48,7 +48,8 @@ func main() {
|
|||
}
|
||||
|
||||
level1 := &engine.Scene{
|
||||
ID: "level_1",
|
||||
ID: "level_1",
|
||||
Bounds: image.Rect(-32, -32, 320+32, 240+32),
|
||||
Components: []interface{}{
|
||||
&engine.Fill{
|
||||
Color: color.Gray{100},
|
||||
|
@ -108,9 +109,8 @@ func main() {
|
|||
KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD},
|
||||
},
|
||||
&engine.Camera{
|
||||
Bounds: image.Rect(-32, -32, 320+32, 240+32),
|
||||
ID: "game_camera",
|
||||
Scene: level1,
|
||||
ID: "game_camera",
|
||||
Scene: level1,
|
||||
},
|
||||
engine.PerfDisplay{},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue