dupes now an error
This commit is contained in:
parent
6e43d600b5
commit
6fcf57fad1
2 changed files with 11 additions and 39 deletions
|
@ -2,8 +2,8 @@ package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
@ -23,13 +23,8 @@ type Game struct {
|
||||||
ScreenHeight int
|
ScreenHeight int
|
||||||
Root DrawUpdater // typically a *Scene or SceneRef though
|
Root DrawUpdater // typically a *Scene or SceneRef though
|
||||||
|
|
||||||
// Components by ID
|
|
||||||
dbmu sync.RWMutex
|
dbmu sync.RWMutex
|
||||||
db map[string]interface{}
|
db map[string]interface{} // Components by ID
|
||||||
|
|
||||||
// Collision domains - all Collider subcomponents
|
|
||||||
cdmu sync.RWMutex
|
|
||||||
cd map[string]map[Collider]struct{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw draws the entire thing, with default draw options.
|
// Draw draws the entire thing, with default draw options.
|
||||||
|
@ -53,39 +48,22 @@ func (g *Game) Update() error {
|
||||||
return g.Root.Update()
|
return g.Root.Update()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterComponent tells the game there is a new component. Currently this is
|
func (g *Game) registerComponent(c interface{}, p []interface{}) error {
|
||||||
// only necessary for components with IDs.
|
|
||||||
func (g *Game) RegisterComponent(c interface{}) {
|
|
||||||
i, ok := c.(Identifier)
|
i, ok := c.(Identifier)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
id := i.Ident()
|
id := i.Ident()
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
g.dbmu.Lock()
|
g.dbmu.Lock()
|
||||||
if _, exists := g.db[id]; exists {
|
if _, exists := g.db[id]; exists {
|
||||||
log.Printf("duplicate id %q", id)
|
return fmt.Errorf("duplicate id %q", id)
|
||||||
}
|
}
|
||||||
g.db[id] = c
|
g.db[id] = c
|
||||||
g.dbmu.Unlock()
|
g.dbmu.Unlock()
|
||||||
}
|
return nil
|
||||||
|
|
||||||
// UnregisterComponent tells the game the component is no more.
|
|
||||||
// Note this does not remove any references held by other components.
|
|
||||||
func (g *Game) UnregisterComponent(c interface{}) {
|
|
||||||
i, ok := c.(Identifier)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
id := i.Ident()
|
|
||||||
if id == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
g.dbmu.Lock()
|
|
||||||
delete(g.db, id)
|
|
||||||
g.dbmu.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Component returns the component with a given ID, or nil if there is none.
|
// Component returns the component with a given ID, or nil if there is none.
|
||||||
|
@ -137,19 +115,12 @@ func (g *Game) LoadAndPrepare(assets fs.FS) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
g.cdmu.Lock()
|
|
||||||
g.cd = make(map[string]map[Collider]struct{})
|
|
||||||
g.cdmu.Unlock()
|
|
||||||
|
|
||||||
g.dbmu.Lock()
|
g.dbmu.Lock()
|
||||||
g.db = make(map[string]interface{})
|
g.db = make(map[string]interface{})
|
||||||
g.dbmu.Unlock()
|
g.dbmu.Unlock()
|
||||||
|
|
||||||
// -> here <- is the moment in time where db is empty.
|
// -> here <- is the moment in time where db is empty.
|
||||||
Walk(g.Root, func(c interface{}, p []interface{}) error {
|
Walk(g.Root, g.registerComponent)
|
||||||
g.RegisterComponent(c)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
Walk(g.Root, func(c interface{}, _ []interface{}) error {
|
Walk(g.Root, func(c interface{}, _ []interface{}) error {
|
||||||
if p, ok := c.(Prepper); ok {
|
if p, ok := c.(Prepper); ok {
|
||||||
p.Prepare(g)
|
p.Prepare(g)
|
||||||
|
|
|
@ -12,7 +12,8 @@ import (
|
||||||
// REPL runs a read-evaluate-print-loop. Commands are taken from src and output
|
// REPL runs a read-evaluate-print-loop. Commands are taken from src and output
|
||||||
// is written to dst. assets is needed for commands like reload.
|
// is written to dst. assets is needed for commands like reload.
|
||||||
func (g *Game) REPL(src io.Reader, dst io.Writer, assets fs.FS) error {
|
func (g *Game) REPL(src io.Reader, dst io.Writer, assets fs.FS) error {
|
||||||
fmt.Fprint(dst, "game>")
|
const prompt = "game> "
|
||||||
|
fmt.Fprint(dst, prompt)
|
||||||
sc := bufio.NewScanner(src)
|
sc := bufio.NewScanner(src)
|
||||||
for sc.Scan() {
|
for sc.Scan() {
|
||||||
argv := strings.Split(sc.Text(), " ")
|
argv := strings.Split(sc.Text(), " ")
|
||||||
|
@ -33,7 +34,7 @@ func (g *Game) REPL(src io.Reader, dst io.Writer, assets fs.FS) error {
|
||||||
case "tree":
|
case "tree":
|
||||||
g.cmdTree(dst, argv)
|
g.cmdTree(dst, argv)
|
||||||
}
|
}
|
||||||
fmt.Fprint(dst, "game>")
|
fmt.Fprint(dst, prompt)
|
||||||
}
|
}
|
||||||
return sc.Err()
|
return sc.Err()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue