back to previous way of doing camera

This commit is contained in:
Josh Deprez 2021-09-01 10:36:39 +10:00
parent 840847b25b
commit 8f495888d9
6 changed files with 13 additions and 32 deletions

View file

@ -11,6 +11,7 @@ import (
var _ interface {
Identifier
Prepper
Scanner
Transformer
} = &Camera{}
@ -18,11 +19,10 @@ func init() {
gob.Register(&Camera{})
}
// Camera models a camera that is viewing a scene. (Camera is a child of the
// scene it is viewing, for various reasons.) Changes to the fields take effect
// immediately.
// Camera models a camera that is viewing something.
type Camera struct {
ID
Child interface{}
// Camera controls
Centre image.Point // world coordinates
@ -39,6 +39,9 @@ func (c *Camera) Prepare(game *Game) error {
return nil
}
// Scan returns s.Child.
func (c *Camera) Scan() []interface{} { return []interface{}{c.Child} }
// Transform returns the camera transform.
func (c *Camera) Transform() (opts ebiten.DrawImageOptions) {
opts.GeoM.Translate(float2(c.Centre.Mul(-1)))

View file

@ -146,7 +146,6 @@ type Scener interface {
Hider
Identifier
Scanner
Transformer
}
// Saver components can be saved to disk.

View file

@ -2,8 +2,6 @@ package engine
import (
"encoding/gob"
"github.com/hajimehoshi/ebiten/v2"
)
// Ensure Scene satisfies Scener.
@ -17,7 +15,6 @@ func init() {
type Scene struct {
ID
Bounds // world coordinates
Camera *Camera // optional; applies a bunch of transforms to draw calls
Components []interface{}
Disabled
Hidden
@ -105,18 +102,5 @@ func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
}
*/
// Transform returns the camera transform
func (s *Scene) Transform() ebiten.DrawImageOptions {
if s.Camera == nil {
return ebiten.DrawImageOptions{}
}
return s.Camera.Transform()
}
// Scan returns all immediate subcomponents (including the camera, if not nil).
func (s *Scene) Scan() []interface{} {
if s.Camera != nil {
return append(s.Components, s.Camera)
}
return s.Components
}
func (s *Scene) Scan() []interface{} { return s.Components }

View file

@ -5,8 +5,6 @@ import (
"image"
"io/fs"
"path/filepath"
"github.com/hajimehoshi/ebiten/v2"
)
// Ensure SceneRef satisfies interfaces.
@ -76,6 +74,3 @@ func (r SceneRef) Ident() string { return r.scene.Ident() }
// Scan returns the components in the scene.
func (r SceneRef) Scan() []interface{} { return r.scene.Scan() }
// Transform returns the value of Transform from the scene.
func (r SceneRef) Transform() ebiten.DrawImageOptions { return r.scene.Transform() }

Binary file not shown.

10
main.go
View file

@ -33,7 +33,7 @@ func main() {
ebiten.SetWindowTitle("TODO")
// Change to true to rewrite level1.gobz
if true && runtime.GOOS != "js" {
if false && runtime.GOOS != "js" {
writeLevel1()
}
@ -43,7 +43,10 @@ func main() {
Root: &engine.Scene{
ID: "root",
Components: []interface{}{
&engine.SceneRef{Path: "assets/level1.gobz"},
&engine.Camera{
ID: "game_camera",
Child: &engine.SceneRef{Path: "assets/level1.gobz"},
},
&engine.DebugToast{ID: "toast", Pos: image.Pt(0, 15)},
engine.PerfDisplay{},
},
@ -107,9 +110,6 @@ func writeLevel1() {
level1 := &engine.Scene{
ID: "level_1",
Bounds: engine.Bounds(image.Rect(-32, -32, 320+32, 240+32)),
Camera: &engine.Camera{
ID: "game_camera",
},
Components: []interface{}{
&engine.Fill{
ID: "bg_fill",