ichigo/engine/camera.go

115 lines
2.7 KiB
Go
Raw Normal View History

2021-08-08 22:07:55 +10:00
package engine
import (
2021-08-18 16:34:51 +10:00
"encoding/gob"
2021-08-08 22:07:55 +10:00
"image"
"github.com/hajimehoshi/ebiten/v2"
)
2021-08-18 16:34:51 +10:00
// Ensure Camera satisfies interfaces.
2021-08-27 11:49:11 +10:00
var _ interface {
Identifier
Drawer
Prepper
Scanner
Updater
} = &Camera{}
2021-08-18 16:34:51 +10:00
func init() {
2021-08-25 15:04:38 +10:00
gob.Register(&Camera{})
2021-08-18 16:34:51 +10:00
}
2021-08-18 15:41:03 +10:00
// 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.
2021-08-08 22:07:55 +10:00
type Camera struct {
ID
2021-08-20 16:46:26 +10:00
Scene Scener
2021-08-10 14:56:01 +10:00
2021-08-12 15:01:37 +10:00
// Camera controls
Centre image.Point // world coordinates
2021-08-12 14:16:09 +10:00
Filter ebiten.Filter
2021-08-18 15:41:03 +10:00
Zoom float64 // unitless
2021-08-12 14:06:01 +10:00
2021-08-18 15:23:02 +10:00
game *Game
2021-08-08 22:07:55 +10:00
}
2021-08-12 15:01:37 +10:00
// Draw applies transformations to opts, then calls c.Scene.Draw with it.
2021-08-12 14:06:01 +10:00
func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
2021-08-23 10:34:56 +10:00
if c.Scene.IsHidden() {
2021-08-18 15:41:03 +10:00
return
}
2021-08-23 10:34:56 +10:00
br := c.Scene.BoundingRect()
2021-08-18 15:23:02 +10:00
// The lower bound on zoom is the larger of
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
2021-08-12 15:01:37 +10:00
zoom := c.Zoom
2021-08-23 10:34:56 +10:00
sz := br.Size()
2021-08-18 15:23:02 +10:00
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
2021-08-12 13:49:22 +10:00
}
2021-08-12 21:11:32 +10:00
// If the configured centre puts the camera out of bounds, move it.
2021-08-12 15:01:37 +10:00
centre := c.Centre
2021-08-12 13:49:22 +10:00
// 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)
2021-08-23 10:34:56 +10:00
if centre.X-swz < br.Min.X {
centre.X = br.Min.X + swz
2021-08-12 13:49:22 +10:00
}
2021-08-23 10:34:56 +10:00
if centre.Y-shz < br.Min.Y {
centre.Y = br.Min.Y + shz
2021-08-12 13:49:22 +10:00
}
2021-08-23 10:34:56 +10:00
if centre.X+swz > br.Max.X {
centre.X = br.Max.X - swz
2021-08-12 13:49:22 +10:00
}
2021-08-23 10:34:56 +10:00
if centre.Y+shz > br.Max.Y {
centre.Y = br.Max.Y - shz
2021-08-12 13:49:22 +10:00
}
2021-08-12 15:01:37 +10:00
// Apply other options
2021-08-12 14:06:01 +10:00
opts.Filter = c.Filter
2021-08-12 13:49:22 +10:00
2021-08-18 17:44:03 +10:00
// Compute common matrix (parts independent of parallax).
2021-08-18 15:46:28 +10:00
// 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)
2021-08-18 15:41:03 +10:00
// Draw everything.
2021-08-20 15:52:01 +10:00
for _, i := range c.Scene.Scan() {
2021-08-26 09:55:55 +10:00
d, ok := i.(Drawer)
if !ok {
continue
2021-08-18 15:41:03 +10:00
}
2021-08-26 09:55:55 +10:00
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)
2021-08-18 15:41:03 +10:00
}
2021-08-08 22:07:55 +10:00
}
2021-08-12 15:01:37 +10:00
// Update passes the call to c.Scene.
2021-08-23 10:34:56 +10:00
func (c *Camera) Update() error { return c.Scene.Update() }
2021-08-08 22:07:55 +10:00
2021-08-12 15:01:37 +10:00
// Scan returns the only child (c.Scene).
2021-08-08 22:07:55 +10:00
func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} }
2021-08-20 15:52:01 +10:00
// Prepare grabs a copy of game (needed for screen dimensions)
2021-08-18 15:23:02 +10:00
func (c *Camera) Prepare(game *Game) { c.game = game }