new camera paradigm
This commit is contained in:
parent
ed61e27440
commit
88fa4f5a6e
2 changed files with 18 additions and 30 deletions
|
@ -9,14 +9,20 @@ import (
|
||||||
type Camera struct {
|
type Camera struct {
|
||||||
ID
|
ID
|
||||||
Scene *Scene
|
Scene *Scene
|
||||||
Transform GeoMDef
|
|
||||||
|
// camera controls
|
||||||
|
Zoom float64
|
||||||
|
Centre image.Point
|
||||||
|
|
||||||
game *Game
|
game *Game
|
||||||
// TODO: camera constraints
|
// TODO: camera constraints
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Camera) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
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)
|
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) Scan() []interface{} { return []interface{}{c.Scene} }
|
||||||
|
|
||||||
func (c *Camera) Prepare(game *Game) { c.game = game }
|
func (c *Camera) Prepare(game *Game) {
|
||||||
|
c.game = game
|
||||||
// Centre centres the camera on a world coordinate.
|
if c.Zoom == 0 {
|
||||||
func (c *Camera) Centre(p image.Point) {
|
c.Zoom = 1
|
||||||
// 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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) }
|
|
||||||
|
|
17
game/aw.go
17
game/aw.go
|
@ -67,22 +67,13 @@ func (aw *Awakeman) Update() error {
|
||||||
aw.SetAnim(aw.animIdleLeft)
|
aw.SetAnim(aw.animIdleLeft)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
zc := false
|
aw.camera.Zoom = 1
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyShift) {
|
if ebiten.IsKeyPressed(ebiten.KeyShift) {
|
||||||
aw.camera.Zoom(2)
|
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.MoveX(aw.vx, func() { aw.vx = -aw.vx * bounceDampen })
|
||||||
aw.MoveY(aw.vy, func() { aw.vy = -aw.vy * 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()
|
return aw.Sprite.Update()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue