From 1bf7920d2f257b2ce9c4b06d23c802ddf1add5d9 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Sun, 8 Aug 2021 22:07:55 +1000 Subject: [PATCH] cameraaaaa --- engine/camera.go | 32 ++++++++++++++++++++++++++++++++ engine/scene.go | 3 --- engine/tiles.go | 10 ++++------ game/aw.go | 10 +++++++++- main.go | 6 +++++- 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 engine/camera.go diff --git a/engine/camera.go b/engine/camera.go new file mode 100644 index 0000000..379e7b9 --- /dev/null +++ b/engine/camera.go @@ -0,0 +1,32 @@ +package engine + +import ( + "image" + + "github.com/hajimehoshi/ebiten/v2" +) + +type Camera struct { + ID + Scene *Scene + Transform GeoMDef + + game *Game +} + +func (c *Camera) Draw(screen *ebiten.Image, geom ebiten.GeoM) { + geom.Concat(*c.Transform.GeoM()) + c.Scene.Draw(screen, geom) +} + +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 } + +func (c *Camera) Centre(p image.Point) { + t := c.Transform.GeoM() + t.Reset() + t.Translate(float64(c.game.ScreenWidth/2-p.X), float64(c.game.ScreenHeight/2-p.Y)) +} diff --git a/engine/scene.go b/engine/scene.go index 6627926..dcadbcb 100644 --- a/engine/scene.go +++ b/engine/scene.go @@ -18,7 +18,6 @@ type Scene struct { Disabled bool Hidden bool ID - Transform GeoMDef ZPos } @@ -27,8 +26,6 @@ func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) { if s.Hidden { return } - geom.Concat(*s.Transform.GeoM()) - for _, i := range s.Components { if d, ok := i.(Drawer); ok { d.Draw(screen, geom) diff --git a/engine/tiles.go b/engine/tiles.go index 9d19e19..a4af391 100644 --- a/engine/tiles.go +++ b/engine/tiles.go @@ -18,11 +18,10 @@ type Tilemap struct { Disabled bool Hidden bool ID - Map [][]Tile - Ersatz bool // "fake wall" - Src ImageRef - TileSize int - Transform GeoMDef + Map [][]Tile + Ersatz bool // "fake wall" + Src ImageRef + TileSize int ZPos } @@ -59,7 +58,6 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) { } src := t.Src.Image() w, _ := src.Size() - geom.Concat(*t.Transform.GeoM()) for j, row := range t.Map { for i, tile := range row { if tile == nil { diff --git a/game/aw.go b/game/aw.go index 2ba0ef2..c430c2f 100644 --- a/game/aw.go +++ b/game/aw.go @@ -16,6 +16,9 @@ func init() { type Awakeman struct { engine.Sprite + CameraID string + + camera *engine.Camera vx, vy float64 facingLeft bool coyoteTimer int @@ -65,10 +68,15 @@ func (aw *Awakeman) Update() error { } 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))) + return aw.Sprite.Update() } -func (aw *Awakeman) Prepare(*engine.Game) { +func (aw *Awakeman) Prepare(game *engine.Game) { + aw.camera = game.Component(aw.CameraID).(*engine.Camera) + aw.animIdleLeft = &engine.Anim{Def: engine.AnimDefs["aw_idle_left"]} aw.animIdleRight = &engine.Anim{Def: engine.AnimDefs["aw_idle_right"]} aw.animRunLeft = &engine.Anim{Def: engine.AnimDefs["aw_run_left"]} diff --git a/main.go b/main.go index 1d3b5e8..4ffbbea 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,7 @@ func main() { Rect: image.Rect(320, 0, 321, 240), }, &game.Awakeman{ + CameraID: "level_1_camera", Sprite: engine.Sprite{ ID: "awakeman", Actor: engine.Actor{ @@ -93,7 +94,10 @@ func main() { &engine.GobDumper{ KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD}, }, - level1, + &engine.Camera{ + ID: "level_1_camera", + Scene: level1, + }, engine.PerfDisplay{}, }, },