some tidyups

This commit is contained in:
Josh Deprez 2021-08-18 15:23:02 +10:00
parent f8665c82be
commit cdcad14245
8 changed files with 31 additions and 37 deletions

View file

@ -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 {

View file

@ -21,14 +21,19 @@ type Camera struct {
Filter ebiten.Filter
game *Game
zoomBound float64
}
// 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 }

View file

@ -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
}

View file

@ -9,9 +9,9 @@ import (
// Fill fills the screen with a colour.
type Fill struct {
Color color.Color
DrawOrder
Hidden bool
ID
DrawOrder
}
func (f *Fill) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {

View file

@ -16,9 +16,9 @@ func init() {
type Scene struct {
Components []interface{}
Disabled bool
DrawOrder
Hidden bool
ID
DrawOrder
}
// 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

View file

@ -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
}

View file

@ -16,6 +16,7 @@ func init() {
// Tilemap renders a grid of tiles.
type Tilemap struct {
Disabled bool
DrawOrder
Hidden bool
ID
Map map[image.Point]Tile
@ -23,7 +24,6 @@ type Tilemap struct {
Offset image.Point // world coordinates
Src ImageRef
TileSize int
DrawOrder
}
// CollidesWith implements Collider.

View file

@ -56,10 +56,10 @@ func main() {
},
&engine.Tilemap{
ID: "terrain",
DrawOrder: 1,
Map: tiles,
Src: engine.ImageRef{Path: "assets/boxes.png"},
TileSize: 16,
DrawOrder: 1,
},
&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{},