From cdcad142455bc9aee4153d8e10b923e0e4630e09 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Wed, 18 Aug 2021 15:23:02 +1000 Subject: [PATCH] some tidyups --- engine/actor.go | 1 - engine/camera.go | 28 +++++++++++----------------- engine/debug.go | 2 +- engine/fill.go | 4 ++-- engine/scene.go | 7 ++++--- engine/sprite.go | 2 +- engine/tiles.go | 4 ++-- main.go | 20 ++++++++++---------- 8 files changed, 31 insertions(+), 37 deletions(-) diff --git a/engine/actor.go b/engine/actor.go index d0fe306..3a6a526 100644 --- a/engine/actor.go +++ b/engine/actor.go @@ -23,7 +23,6 @@ type Actor struct { } func (a *Actor) CollidesAt(p image.Point) bool { - // TODO: more efficient test? hit := false Walk(a.collisionDomain, func(c interface{}) bool { if coll, ok := c.(Collider); ok { diff --git a/engine/camera.go b/engine/camera.go index 43efcf4..e16fa2e 100644 --- a/engine/camera.go +++ b/engine/camera.go @@ -20,15 +20,20 @@ type Camera struct { Filter ebiten.Filter - game *Game - zoomBound float64 + game *Game } // Draw applies transformations to opts, then calls c.Scene.Draw with it. func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) { + // The lower bound on zoom is the larger of + // { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) } zoom := c.Zoom - if zoom < c.zoomBound { - zoom = c.zoomBound + sz := c.Bounds.Size() + if z := float64(c.game.ScreenWidth) / float64(sz.X); zoom < z { + zoom = z + } + if z := float64(c.game.ScreenHeight) / float64(sz.Y); zoom < z { + zoom = z } // If the configured centre puts the camera out of bounds, move it. @@ -70,16 +75,5 @@ func (c *Camera) Update() error { return c.Scene.Update() } // Scan returns the only child (c.Scene). func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} } -// Prepare, among other things, computes the lower bound for Zoom based on -// c.Bounds and game.ScreenWidth/Height. -func (c *Camera) Prepare(game *Game) { - c.game = game - - // The lower bound on zoom is the larger of - // { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) } - sz := c.Bounds.Size() - c.zoomBound = float64(c.game.ScreenWidth) / float64(sz.X) - if z := float64(c.game.ScreenHeight) / float64(sz.Y); c.zoomBound < z { - c.zoomBound = z - } -} +// Prepare grabs a copy of game. +func (c *Camera) Prepare(game *Game) { c.game = game } diff --git a/engine/debug.go b/engine/debug.go index bdf857e..5a60bdc 100644 --- a/engine/debug.go +++ b/engine/debug.go @@ -30,7 +30,7 @@ func (p PerfDisplay) Draw(screen *ebiten.Image, _ ebiten.DrawImageOptions) { ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS())) } -func (PerfDisplay) Z() float64 { +func (PerfDisplay) DrawOrder() float64 { // Always draw on top return math.MaxFloat64 } diff --git a/engine/fill.go b/engine/fill.go index 43d58f8..45be3a4 100644 --- a/engine/fill.go +++ b/engine/fill.go @@ -8,10 +8,10 @@ import ( // Fill fills the screen with a colour. type Fill struct { - Color color.Color + Color color.Color + DrawOrder Hidden bool ID - DrawOrder } func (f *Fill) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) { diff --git a/engine/scene.go b/engine/scene.go index 4030e21..5200630 100644 --- a/engine/scene.go +++ b/engine/scene.go @@ -16,9 +16,9 @@ func init() { type Scene struct { Components []interface{} Disabled bool - Hidden bool - ID DrawOrder + Hidden bool + ID } // Draw draws all components in order. @@ -26,6 +26,7 @@ func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) { if s.Hidden { return } + // Draw everything. for _, i := range s.Components { if d, ok := i.(Drawer); ok { d.Draw(screen, opts) @@ -34,7 +35,7 @@ func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) { } // Prepare does an initial Z-order sort. -func (s *Scene) Prepare(*Game) { s.sortByDrawOrder() } +func (s *Scene) Prepare(game *Game) { s.sortByDrawOrder() } // sortByDrawOrder sorts the components by Z position. // Everything without a Z sorts first. Stable sort is used to avoid Z-fighting diff --git a/engine/sprite.go b/engine/sprite.go index c9ae4d1..26b55d0 100644 --- a/engine/sprite.go +++ b/engine/sprite.go @@ -14,12 +14,12 @@ func init() { // Sprite combines an Actor with the ability to Draw from a single spritesheet. type Sprite struct { Actor + DrawOrder FrameSize image.Point FrameOffset image.Point Hidden bool ID Src ImageRef - DrawOrder anim *Anim } diff --git a/engine/tiles.go b/engine/tiles.go index a25ea5d..dcea813 100644 --- a/engine/tiles.go +++ b/engine/tiles.go @@ -16,14 +16,14 @@ func init() { // Tilemap renders a grid of tiles. type Tilemap struct { Disabled bool - Hidden bool + DrawOrder + Hidden bool ID Map map[image.Point]Tile Ersatz bool // "fake wall" Offset image.Point // world coordinates Src ImageRef TileSize int - DrawOrder } // CollidesWith implements Collider. diff --git a/main.go b/main.go index b05e5a9..9f0cf95 100644 --- a/main.go +++ b/main.go @@ -51,15 +51,15 @@ func main() { ID: "level_1", Components: []interface{}{ &engine.Fill{ - Color: color.Gray{100}, - DrawOrder: 0, + Color: color.Gray{100}, + DrawOrder: 0, }, &engine.Tilemap{ - ID: "terrain", - Map: tiles, - Src: engine.ImageRef{Path: "assets/boxes.png"}, - TileSize: 16, - DrawOrder: 1, + ID: "terrain", + DrawOrder: 1, + Map: tiles, + Src: engine.ImageRef{Path: "assets/boxes.png"}, + TileSize: 16, }, &engine.SolidRect{ ID: "ceiling", @@ -74,7 +74,7 @@ func main() { Rect: image.Rect(320, 0, 321, 240), }, &game.Awakeman{ - CameraID: "level_1_camera", + CameraID: "game_camera", Sprite: engine.Sprite{ ID: "awakeman", Actor: engine.Actor{ @@ -82,10 +82,10 @@ func main() { Pos: image.Pt(100, 100), Size: image.Pt(8, 16), }, + DrawOrder: 2, FrameOffset: image.Pt(-1, 0), FrameSize: image.Pt(10, 16), Src: engine.ImageRef{Path: "assets/aw.png"}, - DrawOrder: 2, }, }, }, @@ -101,8 +101,8 @@ func main() { KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD}, }, &engine.Camera{ - ID: "level_1_camera", Bounds: image.Rect(-32, -32, 320+32, 240+32), + ID: "game_camera", Scene: level1, }, engine.PerfDisplay{},