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
|
|
|
|
Prepper
|
2021-09-01 10:36:39 +10:00
|
|
|
Scanner
|
2021-08-31 22:05:21 +10:00
|
|
|
Transformer
|
2021-08-27 11:49:11 +10:00
|
|
|
} = &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-09-01 10:36:39 +10:00
|
|
|
// Camera models a camera that is viewing something.
|
2021-08-08 22:07:55 +10:00
|
|
|
type Camera struct {
|
|
|
|
ID
|
2021-09-01 10:36:39 +10:00
|
|
|
Child interface{}
|
2021-08-10 14:56:01 +10:00
|
|
|
|
2021-08-12 15:01:37 +10:00
|
|
|
// Camera controls
|
2021-09-01 12:07:49 +10:00
|
|
|
// These directly manipulate the camera. If you want to restrict the camera
|
|
|
|
// view area to the child's bounding rectangle, use PointAt.
|
2021-08-31 12:52:28 +10:00
|
|
|
Centre image.Point // world coordinates
|
2021-09-01 12:07:49 +10:00
|
|
|
Rotation float64 // radians
|
|
|
|
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-09-01 12:07:49 +10:00
|
|
|
// PointAt points the camera at a particular centre point and zoom, but adjusts
|
|
|
|
// for the bounds of the child component (if available).
|
|
|
|
func (c *Camera) PointAt(centre image.Point, zoom float64) {
|
|
|
|
// Special sauce: if Child has a BoundingRect, make some adjustments
|
|
|
|
bnd, ok := c.Child.(Bounder)
|
|
|
|
if !ok {
|
|
|
|
c.Centre, c.Zoom = centre, zoom
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The child has boundaries; respect them.
|
|
|
|
br := bnd.BoundingRect()
|
|
|
|
|
|
|
|
// The lower bound on zoom is the larger of
|
|
|
|
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
|
|
|
|
sz := br.Size()
|
|
|
|
if z := float64(c.game.ScreenSize.X) / float64(sz.X); zoom < z {
|
|
|
|
zoom = z
|
|
|
|
}
|
|
|
|
if z := float64(c.game.ScreenSize.Y) / float64(sz.Y); zoom < z {
|
|
|
|
zoom = z
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the configured centre puts the camera out of bounds, move it.
|
|
|
|
// Camera frame currently Rectangle{ centre ± (screen/(2*zoom)) }.
|
2021-09-01 17:24:43 +10:00
|
|
|
sw2, sh2 := pfloat(c.game.ScreenSize.Div(2))
|
2021-09-01 12:07:49 +10:00
|
|
|
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
|
|
|
|
}
|
|
|
|
c.Centre, c.Zoom = centre, zoom
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:52:01 +10:00
|
|
|
// Prepare grabs a copy of game (needed for screen dimensions)
|
2021-08-27 14:52:24 +10:00
|
|
|
func (c *Camera) Prepare(game *Game) error {
|
|
|
|
c.game = game
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-31 22:05:21 +10:00
|
|
|
|
2021-09-01 10:36:39 +10:00
|
|
|
// Scan returns s.Child.
|
|
|
|
func (c *Camera) Scan() []interface{} { return []interface{}{c.Child} }
|
|
|
|
|
2021-08-31 22:05:21 +10:00
|
|
|
// Transform returns the camera transform.
|
2021-09-01 09:55:18 +10:00
|
|
|
func (c *Camera) Transform() (opts ebiten.DrawImageOptions) {
|
2021-09-01 17:24:43 +10:00
|
|
|
opts.GeoM.Translate(pfloat(c.Centre.Mul(-1)))
|
2021-08-31 22:05:21 +10:00
|
|
|
opts.GeoM.Scale(c.Zoom, c.Zoom)
|
|
|
|
opts.GeoM.Rotate(c.Rotation)
|
2021-09-01 17:24:43 +10:00
|
|
|
opts.GeoM.Translate(pfloat(c.game.ScreenSize.Div(2)))
|
2021-08-31 22:05:21 +10:00
|
|
|
return opts
|
|
|
|
}
|