This commit is contained in:
Josh Deprez 2021-08-25 17:22:22 +10:00
parent 71a89bd9a2
commit c69256ccec
3 changed files with 60 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package engine
import (
"encoding/gob"
"io/fs"
"log"
"sync"
"github.com/hajimehoshi/ebiten/v2"
@ -59,6 +60,9 @@ func (g *Game) RegisterComponent(c interface{}) {
return
}
g.dbmu.Lock()
if _, exists := g.db[id]; exists {
log.Printf("duplicate id %q", id)
}
g.db[id] = c
g.dbmu.Unlock()
}

View file

@ -10,11 +10,24 @@ import (
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
var (
_ engine.Identifier = &Awakeman{}
_ engine.Drawer = &Awakeman{} // provided by Sprite
_ engine.DrawOrderer = &Awakeman{} // provided by Sprite
_ engine.Disabler = &Awakeman{}
_ engine.Hider = &Awakeman{} // provided by Sprite
_ engine.Prepper = &Awakeman{}
_ engine.Scanner = &Awakeman{}
_ engine.Updater = &Awakeman{}
)
func init() {
gob.Register(&Awakeman{})
}
type Awakeman struct {
engine.ID
engine.Disabled
engine.Sprite
CameraID string
@ -32,6 +45,10 @@ type Awakeman struct {
}
func (aw *Awakeman) Update() error {
if aw.Disabled {
return nil
}
// TODO: better cheat for noclip
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
aw.noclip = !aw.noclip

39
main.go
View file

@ -94,6 +94,45 @@ func repl(g *engine.Game) {
g.Prepare()
g.Enable()
g.Show()
case "tree":
var c interface{} = g
if len(tok) == 2 {
// subtree
id := tok[1]
c = g.Component(id)
if c == nil {
log.Printf("Component %q not found", id)
break
}
}
type item struct {
c interface{}
depth int
}
stack := []item{{c, 0}}
for len(stack) > 0 {
tail := len(stack) - 1
x := stack[tail]
stack = stack[:tail]
c := x.c
indent := ""
if x.depth > 0 {
indent = strings.Repeat(" ", x.depth-1) + "↳ "
}
i, ok := c.(engine.Identifier)
if ok {
log.Printf("%s%T %s", indent, c, i.Ident())
} else {
log.Printf("%s%T", indent, c)
}
if s, ok := c.(engine.Scanner); ok {
for _, y := range s.Scan() {
stack = append(stack, item{y, x.depth + 1})
}
}
}
}
}
if err := sc.Err(); err != nil {