tidyups and exporting ZPositioner

This commit is contained in:
Josh Deprez 2021-09-11 17:53:31 +10:00
parent 7c16de018d
commit dd889e17a0
4 changed files with 41 additions and 36 deletions

View file

@ -127,3 +127,11 @@ type Transformer interface {
type Updater interface { type Updater interface {
Update() error Update() error
} }
// ZPositioner components opt into a simpler method of determining draw order
// than DrawAfter/DrawBefore. Drawer implementations should handle the
// ZPositioner case as though the component were a flat infinite plane given by
// z = ZPos().
type ZPositioner interface {
ZPos() int
}

View file

@ -40,33 +40,30 @@ func (h *Hidden) Hide() { *h = true }
// Show sets h to false. // Show sets h to false.
func (h *Hidden) Show() { *h = false } func (h *Hidden) Show() { *h = false }
// ZPosition implements DrawAfter and DrawPosition as a simple Z coordinate. // ZPosition implements DrawAfter and DrawBefore using only Z information.
type ZPosition int type ZPosition int
// DrawAfter reports if z >= x.Max.Z. // DrawAfter reports if z >= x.Max.Z.
func (z ZPosition) DrawAfter(x Drawer) bool { func (z ZPosition) DrawAfter(x Drawer) bool {
switch d := x.(type) { switch x := x.(type) {
case BoundingBoxer: case BoundingBoxer:
return int(z) >= d.BoundingBox().Max.Z return int(z) >= x.BoundingBox().Max.Z
case zpositioner: case ZPositioner:
return z.zposition() > d.zposition() return z.ZPos() > x.ZPos()
} }
return false return false
} }
// DrawBefore reports if z < x.Min.Z. // DrawBefore reports if z < x.Min.Z.
func (z ZPosition) DrawBefore(x Drawer) bool { func (z ZPosition) DrawBefore(x Drawer) bool {
switch d := x.(type) { switch x := x.(type) {
case BoundingBoxer: case BoundingBoxer:
return int(z) < d.BoundingBox().Min.Z return int(z) < x.BoundingBox().Min.Z
case zpositioner: case ZPositioner:
return z.zposition() < d.zposition() return z.ZPos() < x.ZPos()
} }
return false return false
} }
func (z ZPosition) zposition() int { return int(z) } // ZPos returns itself.
func (z ZPosition) ZPos() int { return int(z) }
type zpositioner interface {
zposition() int
}

View file

@ -148,15 +148,15 @@ func (p *Prism) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
// DrawAfter reports if the prism should be drawn after x. // DrawAfter reports if the prism should be drawn after x.
func (p *Prism) DrawAfter(x Drawer) bool { func (p *Prism) DrawAfter(x Drawer) bool {
pb := p.BoundingBox() pb := p.BoundingBox()
switch d := x.(type) { switch x := x.(type) {
case *Prism: case *Prism:
// Fast path for other prisms // Fast path for other prisms
if p.pos.Z == d.pos.Z { if p.pos.Z == x.pos.Z {
return p.pos.Y < d.pos.Y return p.pos.Y < x.pos.Y
} }
return p.pos.Z > d.pos.Z return p.pos.Z > x.pos.Z
case BoundingBoxer: case BoundingBoxer:
xb := d.BoundingBox() xb := x.BoundingBox()
if pb.Max.Z <= xb.Min.Z { // p is behind x if pb.Max.Z <= xb.Min.Z { // p is behind x
return false return false
} }
@ -183,8 +183,8 @@ func (p *Prism) DrawAfter(x Drawer) bool {
return true return true
} }
case zpositioner: case ZPositioner:
return pb.Min.Z > int(d.zposition()) // p is after x return pb.Min.Z > int(x.ZPos()) // p is after x
} }
return false return false
} }
@ -192,15 +192,15 @@ func (p *Prism) DrawAfter(x Drawer) bool {
// DrawBefore reports if the prism should be drawn before x. // DrawBefore reports if the prism should be drawn before x.
func (p *Prism) DrawBefore(x Drawer) bool { func (p *Prism) DrawBefore(x Drawer) bool {
pb := p.BoundingBox() pb := p.BoundingBox()
switch d := x.(type) { switch x := x.(type) {
case *Prism: case *Prism:
// Fast path for other prisms // Fast path for other prisms
if p.pos.Z == d.pos.Z { if p.pos.Z == x.pos.Z {
return p.pos.Y > d.pos.Y return p.pos.Y > x.pos.Y
} }
return p.pos.Z < d.pos.Z return p.pos.Z < x.pos.Z
case BoundingBoxer: case BoundingBoxer:
xb := d.BoundingBox() xb := x.BoundingBox()
if pb.Min.Z >= xb.Max.Z { // p is in front of x if pb.Min.Z >= xb.Max.Z { // p is in front of x
return false return false
} }
@ -225,8 +225,8 @@ func (p *Prism) DrawBefore(x Drawer) bool {
if pb.Min.Z+threshold <= xb.Min.Z { // x is in front of the front half of p if pb.Min.Z+threshold <= xb.Min.Z { // x is in front of the front half of p
return true return true
} }
case zpositioner: case ZPositioner:
return pb.Max.Z < int(d.zposition()) // p is before x return pb.Max.Z < int(x.ZPos()) // p is before x
} }
return false return false
} }

View file

@ -42,9 +42,9 @@ func (s *Sprite) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
// DrawAfter reports if the sprite should be drawn after x. // DrawAfter reports if the sprite should be drawn after x.
func (s *Sprite) DrawAfter(x Drawer) bool { func (s *Sprite) DrawAfter(x Drawer) bool {
sb := s.BoundingBox() sb := s.BoundingBox()
switch d := x.(type) { switch x := x.(type) {
case BoundingBoxer: case BoundingBoxer:
xb := d.BoundingBox() xb := x.BoundingBox()
if sb.Max.Z <= xb.Min.Z { // s is behind x if sb.Max.Z <= xb.Min.Z { // s is behind x
return false return false
} }
@ -57,8 +57,8 @@ func (s *Sprite) DrawAfter(x Drawer) bool {
if sb.Max.Y <= xb.Min.Y { // s is above x if sb.Max.Y <= xb.Min.Y { // s is above x
return true return true
} }
case zpositioner: case ZPositioner:
return sb.Min.Z > int(d.zposition()) // s is after return sb.Min.Z > int(x.ZPos()) // s is after
} }
return false return false
} }
@ -66,9 +66,9 @@ func (s *Sprite) DrawAfter(x Drawer) bool {
// DrawBefore reports if the sprite should be drawn before x. // DrawBefore reports if the sprite should be drawn before x.
func (s *Sprite) DrawBefore(x Drawer) bool { func (s *Sprite) DrawBefore(x Drawer) bool {
sb := s.BoundingBox() sb := s.BoundingBox()
switch d := x.(type) { switch x := x.(type) {
case BoundingBoxer: case BoundingBoxer:
xb := d.BoundingBox() xb := x.BoundingBox()
if sb.Min.Z >= xb.Max.Z { // s is in front of x if sb.Min.Z >= xb.Max.Z { // s is in front of x
return false return false
} }
@ -81,8 +81,8 @@ func (s *Sprite) DrawBefore(x Drawer) bool {
if sb.Min.Y >= xb.Max.Y { // s is below x if sb.Min.Y >= xb.Max.Y { // s is below x
return true return true
} }
case zpositioner: case ZPositioner:
return sb.Max.Z < int(d.zposition()) // s is before return sb.Max.Z < int(x.ZPos()) // s is before
} }
return false return false
} }