This commit is contained in:
Josh Deprez 2021-08-10 15:23:13 +10:00
parent 88fa4f5a6e
commit 8b076efcc4
2 changed files with 14 additions and 5 deletions

View file

@ -11,18 +11,22 @@ type Camera struct {
Scene *Scene Scene *Scene
// camera controls // camera controls
Zoom float64
Centre image.Point Centre image.Point
Rotation float64
Zoom float64
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()) // move the c.Centre to the origin
scx, scy := float64(c.game.ScreenWidth/2), float64(c.game.ScreenHeight/2) geom.Translate(-float64(c.Centre.X), -float64(c.Centre.Y))
geom.Translate((scx/c.Zoom - float64(c.Centre.X)), (scy/c.Zoom - float64(c.Centre.Y))) // zoom and rotate
geom.Scale(c.Zoom, c.Zoom) geom.Scale(c.Zoom, c.Zoom)
geom.Rotate(c.Rotation)
// move the origin to the centre of screen space
geom.Translate(float64(c.game.ScreenWidth/2), float64(c.game.ScreenHeight/2))
c.Scene.Draw(screen, geom) c.Scene.Draw(screen, geom)
} }

View file

@ -3,6 +3,7 @@ package game
import ( import (
"encoding/gob" "encoding/gob"
"image" "image"
"math"
"drjosh.dev/gurgle/engine" "drjosh.dev/gurgle/engine"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@ -71,8 +72,12 @@ func (aw *Awakeman) Update() error {
if ebiten.IsKeyPressed(ebiten.KeyShift) { if ebiten.IsKeyPressed(ebiten.KeyShift) {
aw.camera.Zoom = 2 aw.camera.Zoom = 2
} }
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
aw.camera.Rotation += math.Pi / 6
}
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 })
// aw.Pos is top-left corner, so add half size to get centre
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()
} }