This commit is contained in:
Josh Deprez 2021-09-01 12:12:25 +10:00
parent 2b5987a95e
commit 228f8b9b9a

View file

@ -17,16 +17,17 @@ func init() {
gob.Register(&Parallax{}) gob.Register(&Parallax{})
} }
// Parallax is a container that changes its transform based on the position of a // Parallax is a container that translates based on the position of a
// camera, intended to produce a "parallax" like effect. // camera, intended to produce a "parallax" like effect.
type Parallax struct { type Parallax struct {
CameraID string CameraID string
Factor float64 // how much to change in response to the camera Factor float64 // how much to translate in response to the camera
Child interface{} Child interface{}
camera *Camera camera *Camera
} }
// Prepare obtains a reference to the camera.
func (p *Parallax) Prepare(game *Game) error { func (p *Parallax) Prepare(game *Game) error {
c, ok := game.Component(p.CameraID).(*Camera) c, ok := game.Component(p.CameraID).(*Camera)
if !ok { if !ok {
@ -36,8 +37,10 @@ func (p *Parallax) Prepare(game *Game) error {
return nil return nil
} }
// Scan returns the child component.
func (p *Parallax) Scan() []interface{} { return []interface{}{p.Child} } func (p *Parallax) Scan() []interface{} { return []interface{}{p.Child} }
// Transform returns a GeoM translation of Factor * camera.Centre.
func (p *Parallax) Transform() (opts ebiten.DrawImageOptions) { func (p *Parallax) Transform() (opts ebiten.DrawImageOptions) {
x, y := float2(p.camera.Centre) x, y := float2(p.camera.Centre)
opts.GeoM.Translate(x*p.Factor, y*p.Factor) opts.GeoM.Translate(x*p.Factor, y*p.Factor)