WIP: refactoring into Less

This commit is contained in:
Josh Deprez 2021-09-15 17:41:23 +10:00
parent a3646ca385
commit 261e1a8ac0
3 changed files with 105 additions and 60 deletions

View file

@ -17,12 +17,49 @@ type drawList struct {
}
func (d drawList) Less(i, j int) bool {
// Deal with tombstones first, in case anything else thinks it
// needs to go last.
if d.list[i] == (tombstone{}) {
return false
}
if d.list[j] == (tombstone{}) {
return true
}
// Common logic for known interfaces (BoundingBoxer, ZPositioner), to
// simplify Draw{Before,After} implementations.
switch x := d.list[i].(type) {
case BoundingBoxer:
xb := x.BoundingBox()
switch y := d.list[j].(type) {
case BoundingBoxer:
yb := y.BoundingBox()
if xb.Min.Z >= yb.Max.Z { // x is in front of y
return false
}
if xb.Max.Z <= yb.Min.Z { // x is behind y
return true
}
if xb.Max.Y <= yb.Min.Y { // x is above y
return false
}
if xb.Min.Y >= yb.Max.Y { // x is below y
return true
}
case ZPositioner:
return xb.Max.Z < y.ZPos() // x is before y
}
case ZPositioner:
switch y := d.list[j].(type) {
case BoundingBoxer:
return x.ZPos() < y.BoundingBox().Min.Z
case ZPositioner:
return x.ZPos() < y.ZPos()
}
}
// Fallback case: ask the components themselves
return d.list[i].DrawBefore(d.list[j]) || d.list[j].DrawAfter(d.list[i])
}

View file

@ -158,6 +158,7 @@ func (p *Prism) DrawAfter(x Drawer) bool {
case BoundingBoxer:
xb := x.BoundingBox()
if false {
if pb.Max.Z <= xb.Min.Z { // p is behind x
return false
}
@ -170,6 +171,7 @@ func (p *Prism) DrawAfter(x Drawer) bool {
if pb.Max.Y <= xb.Min.Y { // p is above x
return true
}
}
// The prism special
split := p.m.topext[geom.North].X
threshold := p.m.topext[geom.East].Y
@ -183,8 +185,8 @@ func (p *Prism) DrawAfter(x Drawer) bool {
return true
}
case ZPositioner:
return pb.Min.Z > int(x.ZPos()) // p is after x
//case ZPositioner:
// return pb.Min.Z > x.ZPos() // p is after x
}
return false
}
@ -202,6 +204,7 @@ func (p *Prism) DrawBefore(x Drawer) bool {
case BoundingBoxer:
xb := x.BoundingBox()
if false {
if pb.Min.Z >= xb.Max.Z { // p is in front of x
return false
}
@ -214,6 +217,7 @@ func (p *Prism) DrawBefore(x Drawer) bool {
if pb.Min.Y >= xb.Max.Y { // p is below x
return true
}
}
// The prism special
split := p.m.topext[geom.North].X
threshold := p.m.topext[geom.East].Y
@ -227,8 +231,8 @@ func (p *Prism) DrawBefore(x Drawer) bool {
return true
}
case ZPositioner:
return pb.Max.Z < int(x.ZPos()) // p is before x
//case ZPositioner:
// return pb.Max.Z < x.ZPos() // p is before x
}
return false
}

View file

@ -41,6 +41,7 @@ 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 {
if false {
sb := s.BoundingBox()
switch x := x.(type) {
case BoundingBoxer:
@ -58,13 +59,15 @@ func (s *Sprite) DrawAfter(x Drawer) bool {
return true
}
case ZPositioner:
return sb.Min.Z > int(x.ZPos()) // s is after
return sb.Min.Z > x.ZPos() // s is after
}
}
return false
}
// DrawBefore reports if the sprite should be drawn before x.
func (s *Sprite) DrawBefore(x Drawer) bool {
if false {
sb := s.BoundingBox()
switch x := x.(type) {
case BoundingBoxer:
@ -82,7 +85,8 @@ func (s *Sprite) DrawBefore(x Drawer) bool {
return true
}
case ZPositioner:
return sb.Max.Z < int(x.ZPos()) // s is before
return sb.Max.Z < x.ZPos() // s is before
}
}
return false
}