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"
|
|
|
|
)
|
|
|
|
|
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-09-03 09:42:50 +10:00
|
|
|
Centre image.Point // world coordinates
|
|
|
|
Rotation float64 // radians
|
|
|
|
Zoom float64 // unitless
|
|
|
|
IsoProjection image.Point
|
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).
|
2021-09-03 10:30:14 +10:00
|
|
|
func (c *Camera) PointAt(centre Point3, zoom float64) {
|
2021-09-01 12:07:49 +10:00
|
|
|
// Special sauce: if Child has a BoundingRect, make some adjustments
|
|
|
|
bnd, ok := c.Child.(Bounder)
|
|
|
|
if !ok {
|
2021-09-03 10:30:14 +10:00
|
|
|
c.Centre = centre.IsoProject(c.IsoProjection)
|
|
|
|
c.Zoom = zoom
|
2021-09-01 12:07:49 +10:00
|
|
|
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-02 13:42:44 +10:00
|
|
|
sw2, sh2 := cfloat(c.game.ScreenSize.Div(2))
|
2021-09-01 12:07:49 +10:00
|
|
|
swz, shz := int(sw2/zoom), int(sh2/zoom)
|
2021-09-03 10:30:14 +10:00
|
|
|
cent := centre.IsoProject(c.IsoProjection)
|
|
|
|
if cent.X-swz < br.Min.X {
|
|
|
|
cent.X = br.Min.X + swz
|
2021-09-01 12:07:49 +10:00
|
|
|
}
|
2021-09-03 10:30:14 +10:00
|
|
|
if cent.Y-shz < br.Min.Y {
|
|
|
|
cent.Y = br.Min.Y + shz
|
2021-09-01 12:07:49 +10:00
|
|
|
}
|
2021-09-03 10:30:14 +10:00
|
|
|
if cent.X+swz > br.Max.X {
|
|
|
|
cent.X = br.Max.X - swz
|
2021-09-01 12:07:49 +10:00
|
|
|
}
|
2021-09-03 10:30:14 +10:00
|
|
|
if cent.Y+shz > br.Max.Y {
|
|
|
|
cent.Y = br.Max.Y - shz
|
2021-09-01 12:07:49 +10:00
|
|
|
}
|
2021-09-03 10:30:14 +10:00
|
|
|
c.Centre, c.Zoom = cent, zoom
|
2021-09-01 12:07:49 +10:00
|
|
|
}
|
|
|
|
|
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-03 10:08:56 +10:00
|
|
|
func (c *Camera) Transform(pt Transform) (tf Transform) {
|
2021-09-03 09:42:50 +10:00
|
|
|
tf.IsoProjection = c.IsoProjection
|
|
|
|
tf.Opts.GeoM.Translate(cfloat(c.Centre.Mul(-1)))
|
|
|
|
tf.Opts.GeoM.Scale(c.Zoom, c.Zoom)
|
|
|
|
tf.Opts.GeoM.Rotate(c.Rotation)
|
|
|
|
tf.Opts.GeoM.Translate(cfloat(c.game.ScreenSize.Div(2)))
|
2021-09-03 10:08:56 +10:00
|
|
|
return tf.Concat(pt)
|
2021-08-31 22:05:21 +10:00
|
|
|
}
|