make billboard more complicated

This commit is contained in:
Josh Deprez 2021-09-15 11:42:07 +10:00
parent 146e713fd7
commit 0981350993
2 changed files with 48 additions and 8 deletions

View file

@ -2,7 +2,6 @@ package engine
import ( import (
"encoding/gob" "encoding/gob"
"image"
"drjosh.dev/gurgle/geom" "drjosh.dev/gurgle/geom"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@ -10,6 +9,7 @@ import (
// Ensure Billboard satisfies interfaces. // Ensure Billboard satisfies interfaces.
var _ interface { var _ interface {
BoundingBoxer
Identifier Identifier
Drawer Drawer
Scanner Scanner
@ -24,9 +24,19 @@ func init() {
type Billboard struct { type Billboard struct {
ID ID
Hides Hides
Pos image.Point Pos geom.Int3
Src ImageRef Src ImageRef
ZPosition
game *Game
}
// BoundingBox returns a 0-depth box incorporating the image size.
func (b *Billboard) BoundingBox() geom.Box {
sx, sy := b.Src.Image().Size()
return geom.Box{
Min: b.Pos,
Max: b.Pos.Add(geom.Pt3(sx, sy, 0)),
}
} }
// Draw draws the image. // Draw draws the image.
@ -34,10 +44,41 @@ func (b *Billboard) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
screen.DrawImage(b.Src.Image(), opts) screen.DrawImage(b.Src.Image(), opts)
} }
// DrawAfter reports if b.Pos.Z >= x.Max.Z.
func (b *Billboard) DrawAfter(x Drawer) bool {
switch x := x.(type) {
case BoundingBoxer:
return b.Pos.Z >= x.BoundingBox().Max.Z
case ZPositioner:
return b.Pos.Z > x.ZPos()
}
return false
}
// DrawBefore reports if b.Pos.Z < x.Min.Z.
func (b *Billboard) DrawBefore(x Drawer) bool {
switch x := x.(type) {
case BoundingBoxer:
return b.Pos.Z < x.BoundingBox().Min.Z
case ZPositioner:
return b.Pos.Z < x.ZPos()
}
return false
}
// Prepare saves the reference to Game.
func (b *Billboard) Prepare(g *Game) error {
b.game = g
return nil
}
// Scan returns a slice containing Src. // Scan returns a slice containing Src.
func (b *Billboard) Scan() []interface{} { return []interface{}{&b.Src} } func (b *Billboard) Scan() []interface{} { return []interface{}{&b.Src} }
// Transform returns a translation by the projected position.
func (b *Billboard) Transform() (opts ebiten.DrawImageOptions) { func (b *Billboard) Transform() (opts ebiten.DrawImageOptions) {
opts.GeoM.Translate(geom.CFloat(b.Pos)) opts.GeoM.Translate(geom.CFloat(
b.game.Projection.Project(b.Pos),
))
return opts return opts
} }

View file

@ -23,8 +23,7 @@ func Level1() *engine.Scene {
CameraID: "game_camera", CameraID: "game_camera",
Child: &engine.Billboard{ Child: &engine.Billboard{
ID: "bg_image", ID: "bg_image",
ZPosition: -900, Pos: geom.Pt3(-160, -120, -1),
Pos: image.Pt(-160, -120),
Src: engine.ImageRef{Path: "assets/space.png"}, Src: engine.ImageRef{Path: "assets/space.png"},
}, },
Factor: 0.5, Factor: 0.5,