invert camera-scene relationship
This commit is contained in:
parent
2674234d62
commit
3452a32ddb
4 changed files with 84 additions and 94 deletions
|
@ -10,22 +10,18 @@ import (
|
|||
// Ensure Camera satisfies interfaces.
|
||||
var _ interface {
|
||||
Identifier
|
||||
Drawer
|
||||
Prepper
|
||||
Scanner
|
||||
Updater
|
||||
} = &Camera{}
|
||||
|
||||
func init() {
|
||||
gob.Register(&Camera{})
|
||||
}
|
||||
|
||||
// Camera models a camera that is viewing a scene.
|
||||
// Changes to the configuration take effect immediately.
|
||||
// Camera ignores Scene.Draw and calls Scene's children's Draw.
|
||||
// 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.
|
||||
type Camera struct {
|
||||
ID
|
||||
Scene Scener
|
||||
|
||||
// Camera controls
|
||||
Centre image.Point // world coordinates
|
||||
|
@ -35,84 +31,6 @@ type Camera struct {
|
|||
game *Game
|
||||
}
|
||||
|
||||
// Draw applies transformations to opts, then draws the contents of c.Scene.
|
||||
func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
||||
if c.Scene.IsHidden() {
|
||||
return
|
||||
}
|
||||
|
||||
br := c.Scene.BoundingRect()
|
||||
|
||||
// The lower bound on zoom is the larger of
|
||||
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
|
||||
zoom := c.Zoom
|
||||
sz := br.Size()
|
||||
if z := float64(c.game.ScreenWidth) / float64(sz.X); zoom < z {
|
||||
zoom = z
|
||||
}
|
||||
if z := float64(c.game.ScreenHeight) / float64(sz.Y); zoom < z {
|
||||
zoom = z
|
||||
}
|
||||
|
||||
// If the configured centre puts the camera out of bounds, move it.
|
||||
centre := c.Centre
|
||||
// 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 < br.Min.X {
|
||||
centre.X = br.Min.X + swz
|
||||
}
|
||||
if centre.Y-shz < br.Min.Y {
|
||||
centre.Y = br.Min.Y + shz
|
||||
}
|
||||
if centre.X+swz > br.Max.X {
|
||||
centre.X = br.Max.X - swz
|
||||
}
|
||||
if centre.Y+shz > br.Max.Y {
|
||||
centre.Y = br.Max.Y - shz
|
||||
}
|
||||
|
||||
// Apply other options
|
||||
opts.Filter = c.Filter
|
||||
|
||||
// Compute common matrix (parts independent of parallax).
|
||||
// Moving centre to the origin happens per component.
|
||||
var comm ebiten.GeoM
|
||||
// 2. Zoom (this is also where rotation would be)
|
||||
comm.Scale(zoom, zoom)
|
||||
// 3. Move the origin to the centre of screen space.
|
||||
comm.Translate(sw2, sh2)
|
||||
// 4. Apply transforms from the caller.
|
||||
comm.Concat(opts.GeoM)
|
||||
|
||||
// Draw everything.
|
||||
for _, i := range c.Scene.Scan() {
|
||||
d, ok := i.(Drawer)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
pf := 1.0
|
||||
if s, ok := i.(ParallaxScaler); ok {
|
||||
pf = s.ParallaxFactor()
|
||||
}
|
||||
var geom ebiten.GeoM
|
||||
// 1. Move centre to the origin, subject to parallax factor
|
||||
geom.Translate(-float64(centre.X)*pf, -float64(centre.Y)*pf)
|
||||
geom.Concat(comm)
|
||||
opts.GeoM = geom
|
||||
d.Draw(screen, opts)
|
||||
}
|
||||
}
|
||||
|
||||
// DrawOrder passes the call to c.Scene.
|
||||
func (c *Camera) DrawOrder() float64 { return c.Scene.DrawOrder() }
|
||||
|
||||
// Update passes the call to c.Scene.
|
||||
func (c *Camera) Update() error { return c.Scene.Update() }
|
||||
|
||||
// Scan returns the only child (c.Scene).
|
||||
func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} }
|
||||
|
||||
// Prepare grabs a copy of game (needed for screen dimensions)
|
||||
func (c *Camera) Prepare(game *Game) error {
|
||||
c.game = game
|
||||
|
|
|
@ -18,7 +18,8 @@ func init() {
|
|||
// Scene manages drawing and updating a bunch of components.
|
||||
type Scene struct {
|
||||
ID
|
||||
Bounds // world coordinates
|
||||
Bounds // world coordinates
|
||||
Camera *Camera // optional; applies a bunch of transforms to draw calls
|
||||
Components []interface{}
|
||||
Disabled
|
||||
Hidden
|
||||
|
@ -30,11 +31,77 @@ func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
|||
if s.Hidden {
|
||||
return
|
||||
}
|
||||
if s.Camera == nil {
|
||||
// Draw everything, no camera transforms.
|
||||
for _, i := range s.Components {
|
||||
if d, ok := i.(Drawer); ok {
|
||||
d.Draw(screen, opts)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// There is a camera; apply camera transforms.
|
||||
br := s.BoundingRect()
|
||||
|
||||
// The lower bound on zoom is the larger of
|
||||
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
|
||||
zoom := s.Camera.Zoom
|
||||
sz := br.Size()
|
||||
if z := float64(s.Camera.game.ScreenWidth) / float64(sz.X); zoom < z {
|
||||
zoom = z
|
||||
}
|
||||
if z := float64(s.Camera.game.ScreenHeight) / float64(sz.Y); zoom < z {
|
||||
zoom = z
|
||||
}
|
||||
|
||||
// If the configured centre puts the camera out of bounds, move it.
|
||||
centre := s.Camera.Centre
|
||||
// Camera frame currently Rectangle{ centre ± (screen/(2*zoom)) }.
|
||||
sw2, sh2 := float64(s.Camera.game.ScreenWidth/2), float64(s.Camera.game.ScreenHeight/2)
|
||||
swz, shz := int(sw2/zoom), int(sh2/zoom)
|
||||
if centre.X-swz < br.Min.X {
|
||||
centre.X = br.Min.X + swz
|
||||
}
|
||||
if centre.Y-shz < br.Min.Y {
|
||||
centre.Y = br.Min.Y + shz
|
||||
}
|
||||
if centre.X+swz > br.Max.X {
|
||||
centre.X = br.Max.X - swz
|
||||
}
|
||||
if centre.Y+shz > br.Max.Y {
|
||||
centre.Y = br.Max.Y - shz
|
||||
}
|
||||
|
||||
// Apply other options
|
||||
opts.Filter = s.Camera.Filter
|
||||
|
||||
// Compute common matrix (parts independent of parallax, which is step 1).
|
||||
// Moving centre to the origin happens per component.
|
||||
var comm ebiten.GeoM
|
||||
// 2. Zoom (this is also where rotation would be)
|
||||
comm.Scale(zoom, zoom)
|
||||
// 3. Move the origin to the centre of screen space.
|
||||
comm.Translate(sw2, sh2)
|
||||
// 4. Apply transforms from the caller.
|
||||
comm.Concat(opts.GeoM)
|
||||
|
||||
// Draw everything.
|
||||
for _, i := range s.Components {
|
||||
if d, ok := i.(Drawer); ok {
|
||||
d.Draw(screen, opts)
|
||||
d, ok := i.(Drawer)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
pf := 1.0
|
||||
if s, ok := i.(ParallaxScaler); ok {
|
||||
pf = s.ParallaxFactor()
|
||||
}
|
||||
var geom ebiten.GeoM
|
||||
// 1. Move centre to the origin, subject to parallax factor
|
||||
geom.Translate(-float64(centre.X)*pf, -float64(centre.Y)*pf)
|
||||
geom.Concat(comm)
|
||||
opts.GeoM = geom
|
||||
d.Draw(screen, opts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,8 +125,13 @@ func (s *Scene) sortByDrawOrder() {
|
|||
})
|
||||
}
|
||||
|
||||
// Scan returns all immediate subcomponents.
|
||||
func (s *Scene) Scan() []interface{} { return s.Components }
|
||||
// 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
|
||||
}
|
||||
|
||||
// Scene returns itself.
|
||||
func (s *Scene) Scene() *Scene { return s }
|
||||
|
|
Binary file not shown.
8
main.go
8
main.go
|
@ -33,10 +33,7 @@ func main() {
|
|||
Root: &engine.Scene{
|
||||
ID: "root",
|
||||
Components: []interface{}{
|
||||
&engine.Camera{
|
||||
ID: "game_camera",
|
||||
Scene: &engine.SceneRef{Path: "assets/level1.gobz"},
|
||||
},
|
||||
&engine.SceneRef{Path: "assets/level1.gobz"},
|
||||
&engine.DebugToast{ID: "toast", Pos: image.Pt(0, 15)},
|
||||
engine.PerfDisplay{},
|
||||
},
|
||||
|
@ -100,6 +97,9 @@ 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{
|
||||
Color: color.Gray{100},
|
||||
|
|
Loading…
Reference in a new issue