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 { func (a *Actor) CollidesAt(p image.Point) bool {
// TODO: more efficient test?
hit := false hit := false
Walk(a.collisionDomain, func(c interface{}) bool { Walk(a.collisionDomain, func(c interface{}) bool {
if coll, ok := c.(Collider); ok { if coll, ok := c.(Collider); ok {

View file

@ -20,15 +20,20 @@ type Camera struct {
Filter ebiten.Filter Filter ebiten.Filter
game *Game game *Game
zoomBound float64
} }
// Draw applies transformations to opts, then calls c.Scene.Draw with it. // Draw applies transformations to opts, then calls c.Scene.Draw with it.
func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) { 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 zoom := c.Zoom
if zoom < c.zoomBound { sz := c.Bounds.Size()
zoom = c.zoomBound 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. // 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). // Scan returns the only child (c.Scene).
func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} } func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} }
// Prepare, among other things, computes the lower bound for Zoom based on // Prepare grabs a copy of game.
// c.Bounds and game.ScreenWidth/Height. func (c *Camera) Prepare(game *Game) { c.game = game }
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
}
}

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())) 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 // Always draw on top
return math.MaxFloat64 return math.MaxFloat64
} }

View file

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

View file

@ -16,9 +16,9 @@ func init() {
type Scene struct { type Scene struct {
Components []interface{} Components []interface{}
Disabled bool Disabled bool
Hidden bool
ID
DrawOrder DrawOrder
Hidden bool
ID
} }
// Draw draws all components in order. // Draw draws all components in order.
@ -26,6 +26,7 @@ func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
if s.Hidden { if s.Hidden {
return return
} }
// Draw everything.
for _, i := range s.Components { for _, i := range s.Components {
if d, ok := i.(Drawer); ok { if d, ok := i.(Drawer); ok {
d.Draw(screen, opts) 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. // 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. // sortByDrawOrder sorts the components by Z position.
// Everything without a Z sorts first. Stable sort is used to avoid Z-fighting // 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. // Sprite combines an Actor with the ability to Draw from a single spritesheet.
type Sprite struct { type Sprite struct {
Actor Actor
DrawOrder
FrameSize image.Point FrameSize image.Point
FrameOffset image.Point FrameOffset image.Point
Hidden bool Hidden bool
ID ID
Src ImageRef Src ImageRef
DrawOrder
anim *Anim anim *Anim
} }

View file

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

20
main.go
View file

@ -51,15 +51,15 @@ func main() {
ID: "level_1", ID: "level_1",
Components: []interface{}{ Components: []interface{}{
&engine.Fill{ &engine.Fill{
Color: color.Gray{100}, Color: color.Gray{100},
DrawOrder: 0, DrawOrder: 0,
}, },
&engine.Tilemap{ &engine.Tilemap{
ID: "terrain", ID: "terrain",
Map: tiles, DrawOrder: 1,
Src: engine.ImageRef{Path: "assets/boxes.png"}, Map: tiles,
TileSize: 16, Src: engine.ImageRef{Path: "assets/boxes.png"},
DrawOrder: 1, TileSize: 16,
}, },
&engine.SolidRect{ &engine.SolidRect{
ID: "ceiling", ID: "ceiling",
@ -74,7 +74,7 @@ func main() {
Rect: image.Rect(320, 0, 321, 240), Rect: image.Rect(320, 0, 321, 240),
}, },
&game.Awakeman{ &game.Awakeman{
CameraID: "level_1_camera", CameraID: "game_camera",
Sprite: engine.Sprite{ Sprite: engine.Sprite{
ID: "awakeman", ID: "awakeman",
Actor: engine.Actor{ Actor: engine.Actor{
@ -82,10 +82,10 @@ func main() {
Pos: image.Pt(100, 100), Pos: image.Pt(100, 100),
Size: image.Pt(8, 16), Size: image.Pt(8, 16),
}, },
DrawOrder: 2,
FrameOffset: image.Pt(-1, 0), FrameOffset: image.Pt(-1, 0),
FrameSize: image.Pt(10, 16), FrameSize: image.Pt(10, 16),
Src: engine.ImageRef{Path: "assets/aw.png"}, Src: engine.ImageRef{Path: "assets/aw.png"},
DrawOrder: 2,
}, },
}, },
}, },
@ -101,8 +101,8 @@ func main() {
KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD}, KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD},
}, },
&engine.Camera{ &engine.Camera{
ID: "level_1_camera",
Bounds: image.Rect(-32, -32, 320+32, 240+32), Bounds: image.Rect(-32, -32, 320+32, 240+32),
ID: "game_camera",
Scene: level1, Scene: level1,
}, },
engine.PerfDisplay{}, engine.PerfDisplay{},