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