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 {
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.
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
// DrawAfter reports if z >= x.Max.Z.
func (z ZPosition) DrawAfter(x Drawer) bool {
switch d := x.(type) {
switch x := x.(type) {
case BoundingBoxer:
return int(z) >= d.BoundingBox().Max.Z
case zpositioner:
return z.zposition() > d.zposition()
return int(z) >= x.BoundingBox().Max.Z
case ZPositioner:
return z.ZPos() > x.ZPos()
}
return false
}
// DrawBefore reports if z < x.Min.Z.
func (z ZPosition) DrawBefore(x Drawer) bool {
switch d := x.(type) {
switch x := x.(type) {
case BoundingBoxer:
return int(z) < d.BoundingBox().Min.Z
case zpositioner:
return z.zposition() < d.zposition()
return int(z) < x.BoundingBox().Min.Z
case ZPositioner:
return z.ZPos() < x.ZPos()
}
return false
}
func (z ZPosition) zposition() int { return int(z) }
type zpositioner interface {
zposition() int
}
// ZPos returns itself.
func (z ZPosition) ZPos() int { return int(z) }

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.
func (p *Prism) DrawAfter(x Drawer) bool {
pb := p.BoundingBox()
switch d := x.(type) {
switch x := x.(type) {
case *Prism:
// Fast path for other prisms
if p.pos.Z == d.pos.Z {
return p.pos.Y < d.pos.Y
if p.pos.Z == x.pos.Z {
return p.pos.Y < x.pos.Y
}
return p.pos.Z > d.pos.Z
return p.pos.Z > x.pos.Z
case BoundingBoxer:
xb := d.BoundingBox()
xb := x.BoundingBox()
if pb.Max.Z <= xb.Min.Z { // p is behind x
return false
}
@ -183,8 +183,8 @@ func (p *Prism) DrawAfter(x Drawer) bool {
return true
}
case zpositioner:
return pb.Min.Z > int(d.zposition()) // p is after x
case ZPositioner:
return pb.Min.Z > int(x.ZPos()) // p is after x
}
return false
}
@ -192,15 +192,15 @@ func (p *Prism) DrawAfter(x Drawer) bool {
// DrawBefore reports if the prism should be drawn before x.
func (p *Prism) DrawBefore(x Drawer) bool {
pb := p.BoundingBox()
switch d := x.(type) {
switch x := x.(type) {
case *Prism:
// Fast path for other prisms
if p.pos.Z == d.pos.Z {
return p.pos.Y > d.pos.Y
if p.pos.Z == x.pos.Z {
return p.pos.Y > x.pos.Y
}
return p.pos.Z < d.pos.Z
return p.pos.Z < x.pos.Z
case BoundingBoxer:
xb := d.BoundingBox()
xb := x.BoundingBox()
if pb.Min.Z >= xb.Max.Z { // p is in front of x
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
return true
}
case zpositioner:
return pb.Max.Z < int(d.zposition()) // p is before x
case ZPositioner:
return pb.Max.Z < int(x.ZPos()) // p is before x
}
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.
func (s *Sprite) DrawAfter(x Drawer) bool {
sb := s.BoundingBox()
switch d := x.(type) {
switch x := x.(type) {
case BoundingBoxer:
xb := d.BoundingBox()
xb := x.BoundingBox()
if sb.Max.Z <= xb.Min.Z { // s is behind x
return false
}
@ -57,8 +57,8 @@ func (s *Sprite) DrawAfter(x Drawer) bool {
if sb.Max.Y <= xb.Min.Y { // s is above x
return true
}
case zpositioner:
return sb.Min.Z > int(d.zposition()) // s is after
case ZPositioner:
return sb.Min.Z > int(x.ZPos()) // s is after
}
return false
}
@ -66,9 +66,9 @@ func (s *Sprite) DrawAfter(x Drawer) bool {
// DrawBefore reports if the sprite should be drawn before x.
func (s *Sprite) DrawBefore(x Drawer) bool {
sb := s.BoundingBox()
switch d := x.(type) {
switch x := x.(type) {
case BoundingBoxer:
xb := d.BoundingBox()
xb := x.BoundingBox()
if sb.Min.Z >= xb.Max.Z { // s is in front of x
return false
}
@ -81,8 +81,8 @@ func (s *Sprite) DrawBefore(x Drawer) bool {
if sb.Min.Y >= xb.Max.Y { // s is below x
return true
}
case zpositioner:
return sb.Max.Z < int(d.zposition()) // s is before
case ZPositioner:
return sb.Max.Z < int(x.ZPos()) // s is before
}
return false
}