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 { 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{}) { if d.list[i] == (tombstone{}) {
return false return false
} }
if d.list[j] == (tombstone{}) { if d.list[j] == (tombstone{}) {
return true 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]) return d.list[i].DrawBefore(d.list[j]) || d.list[j].DrawAfter(d.list[i])
} }

View file

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

View file

@ -41,48 +41,52 @@ 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() if false {
switch x := x.(type) { sb := s.BoundingBox()
case BoundingBoxer: switch x := x.(type) {
xb := x.BoundingBox() case BoundingBoxer:
if sb.Max.Z <= xb.Min.Z { // s is behind x xb := x.BoundingBox()
return false if sb.Max.Z <= xb.Min.Z { // s is behind x
return false
}
if sb.Min.Z >= xb.Max.Z { // s is in front of x
return true
}
if sb.Min.Y >= xb.Max.Y { // s is below x
return false
}
if sb.Max.Y <= xb.Min.Y { // s is above x
return true
}
case ZPositioner:
return sb.Min.Z > x.ZPos() // s is after
} }
if sb.Min.Z >= xb.Max.Z { // s is in front of x
return true
}
if sb.Min.Y >= xb.Max.Y { // s is below x
return false
}
if sb.Max.Y <= xb.Min.Y { // s is above x
return true
}
case ZPositioner:
return sb.Min.Z > int(x.ZPos()) // s is after
} }
return false return false
} }
// 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() if false {
switch x := x.(type) { sb := s.BoundingBox()
case BoundingBoxer: switch x := x.(type) {
xb := x.BoundingBox() case BoundingBoxer:
if sb.Min.Z >= xb.Max.Z { // s is in front of x xb := x.BoundingBox()
return false if sb.Min.Z >= xb.Max.Z { // s is in front of x
return false
}
if sb.Max.Z <= xb.Min.Z { // s is behind x
return true
}
if sb.Max.Y <= xb.Min.Y { // s is above x
return false
}
if sb.Min.Y >= xb.Max.Y { // s is below x
return true
}
case ZPositioner:
return sb.Max.Z < x.ZPos() // s is before
} }
if sb.Max.Z <= xb.Min.Z { // s is behind x
return true
}
if sb.Max.Y <= xb.Min.Y { // s is above x
return false
}
if sb.Min.Y >= xb.Max.Y { // s is below x
return true
}
case ZPositioner:
return sb.Max.Z < int(x.ZPos()) // s is before
} }
return false return false
} }