ZPositioner -> DrawOrderer
This commit is contained in:
parent
c6d6536e49
commit
2f372d10d4
8 changed files with 28 additions and 27 deletions
|
@ -1,2 +1,3 @@
|
|||
# gurgle
|
||||
|
||||
Game development in progress.
|
|
@ -11,7 +11,7 @@ type Fill struct {
|
|||
Color color.Color
|
||||
Hidden bool
|
||||
ID
|
||||
ZPos
|
||||
DrawOrder
|
||||
}
|
||||
|
||||
func (f *Fill) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
||||
|
|
|
@ -45,7 +45,7 @@ type Updater interface {
|
|||
Update() error
|
||||
}
|
||||
|
||||
// ZPositioner is used to reorder layers.
|
||||
type ZPositioner interface {
|
||||
Z() float64
|
||||
// DrawOrderer is used to reorder layers.
|
||||
type DrawOrderer interface {
|
||||
DrawOrder() float64
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ type ID string
|
|||
// Ident returns id as a string.
|
||||
func (id ID) Ident() string { return string(id) }
|
||||
|
||||
// ZPos implements ZPositioner directly (as a float64 value).
|
||||
type ZPos float64
|
||||
// DrawOrder implements DrawOrderer directly (as a float64 value).
|
||||
type DrawOrder float64
|
||||
|
||||
// Z returns z as a float64.
|
||||
func (z ZPos) Z() float64 { return float64(z) }
|
||||
// DrawOrder returns z as a float64.
|
||||
func (z DrawOrder) DrawOrder() float64 { return float64(z) }
|
||||
|
|
|
@ -18,7 +18,7 @@ type Scene struct {
|
|||
Disabled bool
|
||||
Hidden bool
|
||||
ID
|
||||
ZPos
|
||||
DrawOrder
|
||||
}
|
||||
|
||||
// Draw draws all components in order.
|
||||
|
@ -34,17 +34,17 @@ func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
|||
}
|
||||
|
||||
// Prepare does an initial Z-order sort.
|
||||
func (s *Scene) Prepare(*Game) { s.sortByZ() }
|
||||
func (s *Scene) Prepare(*Game) { s.sortByDrawOrder() }
|
||||
|
||||
// sortByZ sorts the components by Z position.
|
||||
// sortByDrawOrder sorts the components by Z position.
|
||||
// Everything without a Z sorts first. Stable sort is used to avoid Z-fighting
|
||||
// (among layers without a Z, or those with equal Z).
|
||||
func (s *Scene) sortByZ() {
|
||||
func (s *Scene) sortByDrawOrder() {
|
||||
sort.SliceStable(s.Components, func(i, j int) bool {
|
||||
a, aok := s.Components[i].(ZPositioner)
|
||||
b, bok := s.Components[j].(ZPositioner)
|
||||
a, aok := s.Components[i].(DrawOrderer)
|
||||
b, bok := s.Components[j].(DrawOrderer)
|
||||
if aok && bok {
|
||||
return a.Z() < b.Z()
|
||||
return a.DrawOrder() < b.DrawOrder()
|
||||
}
|
||||
return !aok && bok
|
||||
})
|
||||
|
@ -68,18 +68,18 @@ func (s *Scene) Update() error {
|
|||
}
|
||||
}
|
||||
// Check if the updates put the components out of order; if so, sort
|
||||
curZ := -math.MaxFloat64 // fun fact: this is min float64
|
||||
cz := -math.MaxFloat64 // fun fact: this is min float64
|
||||
for _, c := range s.Components {
|
||||
z, ok := c.(ZPositioner)
|
||||
z, ok := c.(DrawOrderer)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
t := z.Z()
|
||||
if t < curZ {
|
||||
s.sortByZ()
|
||||
return nil
|
||||
if t := z.DrawOrder(); t > cz {
|
||||
cz = t
|
||||
continue
|
||||
}
|
||||
curZ = t
|
||||
s.sortByDrawOrder()
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ type Sprite struct {
|
|||
Hidden bool
|
||||
ID
|
||||
Src ImageRef
|
||||
ZPos
|
||||
DrawOrder
|
||||
|
||||
anim *Anim
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ type Tilemap struct {
|
|||
Offset image.Point // world coordinates
|
||||
Src ImageRef
|
||||
TileSize int
|
||||
ZPos
|
||||
DrawOrder
|
||||
}
|
||||
|
||||
// CollidesWith implements Collider.
|
||||
|
|
6
main.go
6
main.go
|
@ -52,14 +52,14 @@ func main() {
|
|||
Components: []interface{}{
|
||||
&engine.Fill{
|
||||
Color: color.Gray{100},
|
||||
ZPos: 0,
|
||||
DrawOrder: 0,
|
||||
},
|
||||
&engine.Tilemap{
|
||||
ID: "terrain",
|
||||
Map: tiles,
|
||||
Src: engine.ImageRef{Path: "assets/boxes.png"},
|
||||
TileSize: 16,
|
||||
ZPos: 1,
|
||||
DrawOrder: 1,
|
||||
},
|
||||
&engine.SolidRect{
|
||||
ID: "ceiling",
|
||||
|
@ -85,7 +85,7 @@ func main() {
|
|||
FrameOffset: image.Pt(-1, 0),
|
||||
FrameSize: image.Pt(10, 16),
|
||||
Src: engine.ImageRef{Path: "assets/aw.png"},
|
||||
ZPos: 2,
|
||||
DrawOrder: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue