comments, cleanups

This commit is contained in:
Josh Deprez 2021-09-11 17:39:52 +10:00
parent f8dd02c072
commit a5083b4eac
5 changed files with 39 additions and 72 deletions

View file

@ -40,8 +40,9 @@ func (d *DebugToast) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
ebitenutil.DebugPrintAt(screen, d.Text, d.Pos.X, d.Pos.Y) ebitenutil.DebugPrintAt(screen, d.Text, d.Pos.X, d.Pos.Y)
} }
func (DebugToast) DrawAfter(x Drawer) bool { return x != Tombstone{} } // Draw last, but sort tombstones later.
func (DebugToast) DrawBefore(x Drawer) bool { return x == Tombstone{} } func (DebugToast) DrawAfter(x Drawer) bool { return x != tombstone{} }
func (DebugToast) DrawBefore(x Drawer) bool { return x == tombstone{} }
func (d *DebugToast) Toast(text string) { func (d *DebugToast) Toast(text string) {
d.Text = text d.Text = text
@ -65,5 +66,6 @@ func (p PerfDisplay) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS())) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
} }
func (PerfDisplay) DrawAfter(x Drawer) bool { return x != Tombstone{} } // Draw last, but sort tombstones later.
func (PerfDisplay) DrawBefore(x Drawer) bool { return x == Tombstone{} } func (PerfDisplay) DrawAfter(x Drawer) bool { return x != tombstone{} }
func (PerfDisplay) DrawBefore(x Drawer) bool { return x == tombstone{} }

View file

@ -99,7 +99,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
} }
// p is not hidden, so compute its cumulative opts. // p is not hidden, so compute its cumulative opts.
if tf, ok := p.(Transformer); ok { if tf, ok := p.(Transformer); ok {
st.opts = ConcatOpts(tf.Transform(), st.opts) st.opts = concatOpts(tf.Transform(), st.opts)
} }
accum[p] = st accum[p] = st
} }
@ -176,11 +176,10 @@ func (g *Game) Update() error {
} }
// Sort the draw list (on every frame - this isn't as bad as it sounds) // Sort the draw list (on every frame - this isn't as bad as it sounds)
//sort.Stable(g.drawList) sort.Stable(g.drawList)
sort.Sort(g.drawList)
// Truncate tombstones from the end. // Truncate tombstones from the end.
for i := len(g.drawList) - 1; i >= 0; i-- { for i := len(g.drawList) - 1; i >= 0; i-- {
if g.drawList[i] == (Tombstone{}) { if g.drawList[i] == (tombstone{}) {
g.drawList = g.drawList[:i] g.drawList = g.drawList[:i]
} }
} }
@ -408,7 +407,7 @@ func (g *Game) unregister(component interface{}) {
// unregister from g.drawList // unregister from g.drawList
for i, d := range g.drawList { for i, d := range g.drawList {
if d == component { if d == component {
g.drawList[i] = Tombstone{} g.drawList[i] = tombstone{}
} }
} }
@ -425,14 +424,14 @@ type abKey struct {
behaviour reflect.Type behaviour reflect.Type
} }
var _ Drawer = Tombstone{} var _ Drawer = tombstone{}
type Tombstone struct{} type tombstone struct{}
func (Tombstone) Draw(*ebiten.Image, *ebiten.DrawImageOptions) {} func (tombstone) Draw(*ebiten.Image, *ebiten.DrawImageOptions) {}
func (Tombstone) DrawAfter(x Drawer) bool { return x != Tombstone{} } func (tombstone) DrawAfter(x Drawer) bool { return x != tombstone{} }
func (Tombstone) DrawBefore(Drawer) bool { return false } func (tombstone) DrawBefore(Drawer) bool { return false }
type drawList []Drawer type drawList []Drawer
@ -442,8 +441,8 @@ func (d drawList) Less(i, j int) bool {
func (d drawList) Len() int { return len(d) } func (d drawList) Len() int { return len(d) }
func (d drawList) Swap(i, j int) { d[i], d[j] = d[j], d[i] } func (d drawList) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
// ConcatOpts returns the combined options (as though a was applied and then b). // concatOpts returns the combined options (as though a was applied and then b).
func ConcatOpts(a, b ebiten.DrawImageOptions) ebiten.DrawImageOptions { func concatOpts(a, b ebiten.DrawImageOptions) ebiten.DrawImageOptions {
a.ColorM.Concat(b.ColorM) a.ColorM.Concat(b.ColorM)
a.GeoM.Concat(b.GeoM) a.GeoM.Concat(b.GeoM)
if b.CompositeMode != 0 { if b.CompositeMode != 0 {

View file

@ -46,6 +46,7 @@ type PrismMap struct {
game *Game game *Game
pwinverse geom.RatMatrix3 pwinverse geom.RatMatrix3
topext [4]image.Point
} }
// CollidesWith checks if the box collides with any prism. // CollidesWith checks if the box collides with any prism.
@ -106,6 +107,7 @@ func (m *PrismMap) Prepare(g *Game) error {
p.pos = m.PosToWorld.Apply(v) p.pos = m.PosToWorld.Apply(v)
p.m = m p.m = m
} }
m.topext = geom.PolygonExtrema(m.PrismTop)
return nil return nil
} }
@ -148,6 +150,7 @@ func (p *Prism) DrawAfter(x Drawer) bool {
pb := p.BoundingBox() pb := p.BoundingBox()
switch d := x.(type) { switch d := x.(type) {
case *Prism: case *Prism:
// Fast path for other prisms
if p.pos.Z == d.pos.Z { if p.pos.Z == d.pos.Z {
return p.pos.Y < d.pos.Y return p.pos.Y < d.pos.Y
} }
@ -155,34 +158,23 @@ func (p *Prism) DrawAfter(x Drawer) bool {
case BoundingBoxer: case BoundingBoxer:
xb := d.BoundingBox() xb := d.BoundingBox()
if pb.Max.Z <= xb.Min.Z { // p is behind x if pb.Max.Z <= xb.Min.Z { // p is behind x
/*if _, ok := x.(*Sprite); ok && pb.Min.Y < 0 {
log.Print("p.DrawAfter: prism is behind sprite")
}*/
return false return false
} }
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
/*if _, ok := x.(*Sprite); ok && pb.Min.Y < 0 {
log.Print("p.DrawAfter: prism is in front of sprite")
}*/
return true return true
} }
if pb.Min.Y >= xb.Max.Y { // p is below x if pb.Min.Y >= xb.Max.Y { // p is below x
/*if _, ok := x.(*Sprite); ok && pb.Min.Y < 0 {
log.Print("p.DrawAfter: prism is below sprite")
}*/
return false return false
} }
if pb.Max.Y <= xb.Min.Y { // p is above x if pb.Max.Y <= xb.Min.Y { // p is above x
/*if _, ok := x.(*Sprite); ok && pb.Min.Y < 0 {
log.Print("p.DrawAfter: prism is above sprite")
}*/
return true return true
} }
// The hexagon special // The hexagon special
if pb.Min.Z+8 <= xb.Min.Z { if pb.Min.Z+8 <= xb.Min.Z { // x is in front of the front half of p
return false return false
} }
if pb.Min.Z+8 >= xb.Max.Z { if pb.Min.Z+8 >= xb.Max.Z { // x is behind the front half of p
return true return true
} }
@ -197,6 +189,7 @@ func (p *Prism) DrawBefore(x Drawer) bool {
pb := p.BoundingBox() pb := p.BoundingBox()
switch d := x.(type) { switch d := x.(type) {
case *Prism: case *Prism:
// Fast path for other prisms
if p.pos.Z == d.pos.Z { if p.pos.Z == d.pos.Z {
return p.pos.Y > d.pos.Y return p.pos.Y > d.pos.Y
} }
@ -204,34 +197,22 @@ func (p *Prism) DrawBefore(x Drawer) bool {
case BoundingBoxer: case BoundingBoxer:
xb := d.BoundingBox() xb := d.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
/*if _, ok := x.(*Sprite); ok && pb.Min.Y < 0 {
log.Print("p.DrawBefore: prism is in front of sprite")
}*/
return false return false
} }
if pb.Max.Z <= xb.Min.Z { // p is behind x if pb.Max.Z <= xb.Min.Z { // p is behind x
/*if _, ok := x.(*Sprite); ok && pb.Min.Y < 0 {
log.Print("p.DrawBefore: prism is behind sprite")
}*/
return true return true
} }
if pb.Max.Y <= xb.Min.Y { // p is above x if pb.Max.Y <= xb.Min.Y { // p is above x
/*if pb.Min.Y < 0 {
log.Print("p.DrawBefore: prism is above sprite")
}*/
return false return false
} }
if pb.Min.Y >= xb.Max.Y { // p is below x if pb.Min.Y >= xb.Max.Y { // p is below x
/*if pb.Min.Y < 0 {
log.Print("p.DrawBefore: prism is below sprite")
}*/
return true return true
} }
// The hexagon special // The hexagon special
if pb.Min.Z+8 >= xb.Max.Z { if pb.Min.Z+8 >= xb.Max.Z { // x is behind the front half of p
return false return false
} }
if pb.Min.Z+8 <= xb.Min.Z { if pb.Min.Z+8 <= xb.Min.Z { // x is in front of the front half of p
return true return true
} }
case zpositioner: case zpositioner:

View file

@ -46,27 +46,15 @@ func (s *Sprite) DrawAfter(x Drawer) bool {
case BoundingBoxer: case BoundingBoxer:
xb := d.BoundingBox() xb := d.BoundingBox()
if sb.Max.Z <= xb.Min.Z { // s is behind x if sb.Max.Z <= xb.Min.Z { // s is behind x
/*if xb.Min.Y < 0 {
log.Print("s.DrawAfter: sprite is behind prism")
}*/
return false return false
} }
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
/*if xb.Min.Y < 0 {
log.Print("s.DrawAfter: sprite is in front of prism")
}*/
return true return true
} }
if sb.Min.Y >= xb.Max.Y { // s is below x if sb.Min.Y >= xb.Max.Y { // s is below x
/*if xb.Min.Y < 0 {
log.Print("s.DrawAfter: sprite is below prism")
}*/
return false return false
} }
if sb.Max.Y <= xb.Min.Y { // s is above x if sb.Max.Y <= xb.Min.Y { // s is above x
/*if xb.Min.Y < 0 {
log.Print("s.DrawAfter: sprite is above prism")
}*/
return true return true
} }
case zpositioner: case zpositioner:
@ -82,27 +70,15 @@ func (s *Sprite) DrawBefore(x Drawer) bool {
case BoundingBoxer: case BoundingBoxer:
xb := d.BoundingBox() xb := d.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
/*if xb.Min.Y < 0 {
log.Print("s.DrawBefore: sprite is in front of prism")
}*/
return false return false
} }
if sb.Max.Z <= xb.Min.Z { // s is behind x if sb.Max.Z <= xb.Min.Z { // s is behind x
/*if xb.Min.Y < 0 {
log.Print("s.DrawBefore: sprite is behind prism")
}*/
return true return true
} }
if sb.Max.Y <= xb.Min.Y { // s is above x if sb.Max.Y <= xb.Min.Y { // s is above x
/*if xb.Min.Y < 0 {
log.Print("s.DrawBefore: sprite is above prism")
}*/
return false return false
} }
if sb.Min.Y >= xb.Max.Y { // s is below x if sb.Min.Y >= xb.Max.Y { // s is below x
/*if xb.Min.Y < 0 {
log.Print("s.DrawBefore: sprite is below prism")
}*/
return true return true
} }
case zpositioner: case zpositioner:

View file

@ -5,9 +5,18 @@ import (
"math" "math"
) )
// PolygonExtrema returns the most northerly, easterly, southerly, and westerly const (
// points (north is in the -Y direction, east is in the +X direction). East = iota
func PolygonExtrema(polygon []image.Point) (e, n, w, s image.Point) { North
West
South
)
// PolygonExtrema returns the most easterly, northerly, westerly, and southerly
// points (north is in the -Y direction, east is in the +X direction, etc). If
// there are multiple points furthest in any direction, the first one is used.
func PolygonExtrema(polygon []image.Point) [4]image.Point {
var e, n, w, s image.Point
e.X = math.MinInt e.X = math.MinInt
n.Y = math.MaxInt n.Y = math.MaxInt
w.X = math.MaxInt w.X = math.MaxInt
@ -26,7 +35,7 @@ func PolygonExtrema(polygon []image.Point) (e, n, w, s image.Point) {
n = p n = p
} }
} }
return e, n, w, s return [4]image.Point{East: e, North: n, West: w, South: s}
} }
// PolygonContains reports if a convex polygon contains a point. The polygon // PolygonContains reports if a convex polygon contains a point. The polygon