ichigo/engine/camera.go

50 lines
1 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
Prepper
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-08-28 18:07:53 +10:00
// 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.
2021-08-08 22:07:55 +10:00
type Camera struct {
ID
2021-08-10 14:56:01 +10:00
2021-08-12 15:01:37 +10:00
// Camera controls
2021-08-31 12:52:28 +10:00
Centre image.Point // world coordinates
Filter ebiten.Filter
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-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
// Transform returns the camera transform.
2021-09-01 09:55:18 +10:00
func (c *Camera) Transform() (opts ebiten.DrawImageOptions) {
2021-08-31 22:05:21 +10:00
opts.GeoM.Translate(float2(c.Centre.Mul(-1)))
opts.GeoM.Scale(c.Zoom, c.Zoom)
opts.GeoM.Rotate(c.Rotation)
opts.GeoM.Translate(float64(c.game.ScreenWidth/2), float64(c.game.ScreenHeight/2))
return opts
}