2021-08-08 22:07:55 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
2021-08-12 15:01:37 +10:00
|
|
|
// Camera models a camera that is viewing a scene. Changes to the
|
|
|
|
// configuration take effect immediately.
|
2021-08-08 22:07:55 +10:00
|
|
|
type Camera struct {
|
|
|
|
ID
|
2021-08-10 14:56:01 +10:00
|
|
|
Scene *Scene
|
|
|
|
|
2021-08-12 15:01:37 +10:00
|
|
|
// Camera controls
|
2021-08-12 13:49:22 +10:00
|
|
|
Bounds image.Rectangle // world coordinates
|
|
|
|
Centre image.Point // world coordinates
|
|
|
|
//Rotation float64 // radians
|
|
|
|
Zoom float64 // unitless
|
2021-08-08 22:07:55 +10:00
|
|
|
|
2021-08-12 14:16:09 +10:00
|
|
|
Filter ebiten.Filter
|
2021-08-12 14:06:01 +10:00
|
|
|
|
2021-08-12 15:01:37 +10:00
|
|
|
game *Game
|
|
|
|
zoomBound float64
|
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-12 15:01:37 +10:00
|
|
|
zoom := c.Zoom
|
|
|
|
if zoom < c.zoomBound {
|
|
|
|
zoom = c.zoomBound
|
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)
|
|
|
|
if centre.X-swz < c.Bounds.Min.X {
|
|
|
|
centre.X = c.Bounds.Min.X + swz
|
|
|
|
}
|
|
|
|
if centre.Y-shz < c.Bounds.Min.Y {
|
|
|
|
centre.Y = c.Bounds.Min.Y + shz
|
|
|
|
}
|
|
|
|
if centre.X+swz > c.Bounds.Max.X {
|
|
|
|
centre.X = c.Bounds.Max.X - swz
|
|
|
|
}
|
|
|
|
if centre.Y+shz > c.Bounds.Max.Y {
|
|
|
|
centre.Y = c.Bounds.Max.Y - shz
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply camera controls to geom.
|
2021-08-12 21:11:32 +10:00
|
|
|
// 1. Move centre to the origin
|
2021-08-12 14:06:01 +10:00
|
|
|
opts.GeoM.Translate(-float64(centre.X), -float64(centre.Y))
|
2021-08-12 13:49:22 +10:00
|
|
|
// 2. Zoom and rotate
|
2021-08-12 14:06:01 +10:00
|
|
|
opts.GeoM.Scale(zoom, zoom)
|
2021-08-12 13:49:22 +10:00
|
|
|
//geom.Rotate(c.Rotation)
|
|
|
|
// 3. Move the origin to the centre of screen space.
|
2021-08-12 14:06:01 +10:00
|
|
|
opts.GeoM.Translate(sw2, sh2)
|
|
|
|
|
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-12 14:06:01 +10:00
|
|
|
c.Scene.Draw(screen, opts)
|
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-08 22:07:55 +10:00
|
|
|
func (c *Camera) Update() error { return c.Scene.Update() }
|
|
|
|
|
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-12 15:01:37 +10:00
|
|
|
// Prepare, among other things, computes the lower bound for Zoom based on
|
|
|
|
// c.Bounds and game.ScreenWidth/Height.
|
2021-08-10 14:56:01 +10:00
|
|
|
func (c *Camera) Prepare(game *Game) {
|
|
|
|
c.game = game
|
2021-08-12 15:01:37 +10:00
|
|
|
|
|
|
|
// The lower bound on zoom is the larger of
|
|
|
|
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
|
|
|
|
sz := c.Bounds.Size()
|
|
|
|
c.zoomBound = float64(c.game.ScreenWidth) / float64(sz.X)
|
|
|
|
if z := float64(c.game.ScreenHeight) / float64(sz.Y); c.zoomBound < z {
|
|
|
|
c.zoomBound = z
|
2021-08-10 14:56:01 +10:00
|
|
|
}
|
2021-08-09 13:58:33 +10:00
|
|
|
}
|