diff --git a/engine/camera.go b/engine/camera.go index ca88a54..6ca75b6 100644 --- a/engine/camera.go +++ b/engine/camera.go @@ -8,15 +8,21 @@ import ( type Camera struct { ID - Scene *Scene - Transform GeoMDef + Scene *Scene + + // camera controls + Zoom float64 + Centre image.Point game *Game // TODO: camera constraints } func (c *Camera) Draw(screen *ebiten.Image, geom ebiten.GeoM) { - geom.Concat(*c.Transform.GeoM()) + //geom.Concat(*c.Transform.GeoM()) + scx, scy := float64(c.game.ScreenWidth/2), float64(c.game.ScreenHeight/2) + geom.Translate((scx/c.Zoom - float64(c.Centre.X)), (scy/c.Zoom - float64(c.Centre.Y))) + geom.Scale(c.Zoom, c.Zoom) c.Scene.Draw(screen, geom) } @@ -24,18 +30,9 @@ func (c *Camera) Update() error { return c.Scene.Update() } func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} } -func (c *Camera) Prepare(game *Game) { c.game = game } - -// Centre centres the camera on a world coordinate. -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.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) Prepare(game *Game) { + c.game = game + if c.Zoom == 0 { + c.Zoom = 1 + } } - -// Zoom scales the existing camera transform by f along both X and Y. -func (c *Camera) Zoom(f float64) { c.Transform.GeoM().Scale(f, f) } diff --git a/game/aw.go b/game/aw.go index 677bdd4..7a4b0b6 100644 --- a/game/aw.go +++ b/game/aw.go @@ -67,22 +67,13 @@ func (aw *Awakeman) Update() error { aw.SetAnim(aw.animIdleLeft) } } - zc := false - if inpututil.IsKeyJustPressed(ebiten.KeyShift) { - aw.camera.Zoom(2) - zc = true + aw.camera.Zoom = 1 + if ebiten.IsKeyPressed(ebiten.KeyShift) { + aw.camera.Zoom = 2 } - 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 }) - if zc || aw.Pos != p { - aw.camera.Centre(aw.Pos.Add(aw.Size.Div(2))) - } + aw.camera.Centre = aw.Pos.Add(aw.Size.Div(2)) return aw.Sprite.Update() }