camera improvements

This commit is contained in:
Josh Deprez 2021-08-09 13:58:33 +10:00
parent c43a1edb74
commit a6508de705
2 changed files with 25 additions and 5 deletions

View file

@ -26,7 +26,16 @@ func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} }
func (c *Camera) Prepare(game *Game) { c.game = game }
func (c *Camera) Centre(p image.Point) {
// Currently the centre of the screen c is A^-1.c in world coordinates
// So it is off by (p - A^-1.c)
t := c.Transform.GeoM()
t.Reset()
t.Translate(float64(c.game.ScreenWidth/2-p.X), float64(c.game.ScreenHeight/2-p.Y))
t.Invert()
wcx, wcy := t.Apply(float64(c.game.ScreenWidth/2), float64(c.game.ScreenHeight/2))
t.Translate(float64(p.X)-wcx, float64(p.Y)-wcy)
t.Invert()
}
func (c *Camera) Zoom(f float64) {
t := c.Transform.GeoM()
t.Scale(f, f)
}

View file

@ -67,11 +67,22 @@ func (aw *Awakeman) Update() error {
aw.SetAnim(aw.animIdleLeft)
}
}
zc := false
if inpututil.IsKeyJustPressed(ebiten.KeyShift) {
aw.camera.Zoom(2)
zc = true
}
if inpututil.IsKeyJustReleased(ebiten.KeyShift) {
aw.camera.Zoom(0.5)
zc = true
}
p := aw.Pos
aw.MoveX(aw.vx, func() { aw.vx = -aw.vx * bounceDampen })
aw.MoveY(aw.vy, func() { aw.vy = -aw.vy * bounceDampen })
aw.camera.Centre(aw.Pos.Add(aw.Size.Div(2)))
if zc || aw.Pos != p {
aw.camera.Centre(aw.Pos.Add(aw.Size.Div(2)))
}
return aw.Sprite.Update()
}