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

View file

@ -10,11 +10,24 @@ import (
"github.com/hajimehoshi/ebiten/v2/inpututil" "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() { func init() {
gob.Register(&Awakeman{}) gob.Register(&Awakeman{})
} }
type Awakeman struct { type Awakeman struct {
engine.ID
engine.Disabled
engine.Sprite engine.Sprite
CameraID string CameraID string
@ -32,6 +45,10 @@ type Awakeman struct {
} }
func (aw *Awakeman) Update() error { func (aw *Awakeman) Update() error {
if aw.Disabled {
return nil
}
// TODO: better cheat for noclip // TODO: better cheat for noclip
if inpututil.IsKeyJustPressed(ebiten.KeyN) { if inpututil.IsKeyJustPressed(ebiten.KeyN) {
aw.noclip = !aw.noclip aw.noclip = !aw.noclip

39
main.go
View file

@ -94,6 +94,45 @@ func repl(g *engine.Game) {
g.Prepare() g.Prepare()
g.Enable() g.Enable()
g.Show() 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 { if err := sc.Err(); err != nil {