experiment continues

This commit is contained in:
Josh Deprez 2021-08-31 18:19:14 +10:00
parent d3284f808d
commit 933340ea8d
2 changed files with 65 additions and 13 deletions

View file

@ -13,6 +13,8 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)
const gameDoesEverything = false
var _ interface {
Disabler
Hider
@ -71,13 +73,55 @@ func (g *Game) Draw(screen *ebiten.Image) {
if g.Hidden {
return
}
//g.Root.Draw(screen, ebiten.DrawImageOptions{})
// draw everything
for _, d := range g.drawList {
if h, ok := d.(Hider); ok && h.IsHidden() {
continue
if !gameDoesEverything {
g.Root.Draw(screen, ebiten.DrawImageOptions{})
}
if gameDoesEverything {
hidden := map[interface{}]bool{
g: false, // g.Hidden checked above
}
transform := map[interface{}]ebiten.DrawImageOptions{
g: {},
}
// draw everything
for _, d := range g.drawList {
// directly hidden?
if h, ok := d.(Hider); ok && h.IsHidden() {
hidden[d] = true
continue
}
// hidden by parent?
// walk up the parents to find the lowest known
hid := false
stack := []interface{}{d}
for p := g.par[d]; p != nil; p = g.par[p] {
if h, found := hidden[p]; found {
hid = h
break
}
stack = append(stack, p)
}
// unwind the stack, setting known values
for len(stack) > 0 {
l1 := len(stack) - 1
p := stack[l1]
stack = stack[:l1]
if h, ok := p.(Hider); ok {
hid = hid || h.IsHidden()
hidden[p] = hid
}
}
// now...skip if hidden
if hid {
continue
}
if t, ok := d.(Transformer); ok {
transform[t] = t.Transform() // concatted with parent transform...
}
d.Draw(screen, ebiten.DrawImageOptions{})
}
d.Draw(screen, ebiten.DrawImageOptions{})
}
}
@ -95,13 +139,14 @@ func (g *Game) Update() error {
if err := g.Root.Update(); err != nil {
return err
}
// sort the draw list (yes, on every frame)
sort.Stable(g.drawList)
// slice out any tombstones
for i := len(g.drawList) - 1; i >= 0; i-- {
if g.drawList[i] == (tombstone{}) {
g.drawList = g.drawList[:i]
if gameDoesEverything {
// sort the draw list (yes, on every frame)
sort.Stable(g.drawList)
// slice out any tombstones
for i := len(g.drawList) - 1; i >= 0; i-- {
if g.drawList[i] == (tombstone{}) {
g.drawList = g.drawList[:i]
}
}
}
return nil

View file

@ -26,6 +26,7 @@ var (
ScannerType = reflect.TypeOf((*Scanner)(nil)).Elem()
ScenerType = reflect.TypeOf((*Scener)(nil)).Elem()
SaverType = reflect.TypeOf((*Saver)(nil)).Elem()
TransformerType = reflect.TypeOf((*Transformer)(nil)).Elem()
UpdaterType = reflect.TypeOf((*Updater)(nil)).Elem()
// Behaviours lists all the behaviours that can be queried with Game.Query.
@ -44,6 +45,7 @@ var (
ScannerType,
ScenerType,
SaverType,
TransformerType,
UpdaterType,
}
)
@ -162,6 +164,11 @@ type Saver interface {
Save() error
}
// Transformer components can transform their child components.
type Transformer interface {
Transform() ebiten.DrawImageOptions
}
// Updater components can update themselves. Update is called repeatedly.
// Each component is responsible for calling Update on its child components
// (so that disabling the parent prevents updates to the children, etc).